Live data from Hacker News

Einstein Notation applied in programming

hz2.org

11–20 of 28 posts

Re: Einstein Notation applied in programming

#11

Been a while since GR, but I'm pretty sure some of those indices are supposed to be on top. A_{ij} = B_i^k * C_{jk} possibly, but don't quote me on that.

Since these are Euclidean spaces, the difference between contravariant and covariant indices is non-existent. So it doesn't matter whether the indices are upstairs or downstairs.

Re: Einstein Notation applied in programming

#12
In Julia it is easy: https://github.com/Jutho/TensorOperations.jl

In some fields, e.g. condensed matter physics, you need you need to deal with contracting multidimensional tensors all the time, and with out Einstein notation it would be difficult.

The simples example is so-called matrix product state (http://en.wikipedia.org/wiki/Matrix_product_state), which is similar to Markov process. But for more interesting scenarios (e.g. 2d) one needs to use at least 4 dim tensors.

Re: Einstein Notation applied in programming

#14
post #10

What the author call "black magic" I call abstraction. Why would you need to know at all times what summation and multiplications of what rows and columns are going on in your matrices? That's why use matrices, to not have to think about those all the time.

I think you missed the point.

You can write `a mmu b` in KDB, but what the author refers to as no clue on the surface to tell me what is A, B, or C is about the need for improved notation, not an extended maths library.

That said, I think the author's notation is terrible, so I'll use a different one. In K you can write

    +/+*\:
or explicitly

    {+/+x*\:y}
which is a function: sum flip x times eachleft y

This allows me to see that:

    (2 3f;4 0f)*\:(1 2f;5 -1f)
is:

    ((2 4f;15 -3f);(4 8f;0 -0f))
and because I want the sums to go "the other way" I just sum the flip:

    +/+((2 4f;15 -3f);(4 8f;0 -0f))
Now I must prefer reading it this way; my MBA has too small a screen to fit the C code the way the author wrote it, but in just a few characters it is completely obvious what is going on!

This extends to the second example as well: +/1+!x (sum one plus til x) is much more clear than:

    int sum=0,i,a[100];
    for(i=0;i
and while sumcode doesn't take up so much room that I can't fit it on my screen, the K example is much clearer and much more obvious to the point where I might notice that {+/1+!x} is the same as {a+x*a:x%2} -- something I might miss if the two for-loops are far enough away from each other.

Do you still think we need a sumcode abstraction now? Or do you understand the value of better list comprehension?

Re: Einstein Notation applied in programming

#16

Been a while since GR, but I'm pretty sure some of those indices are supposed to be on top. A_{ij} = B_i^k * C_{jk} possibly, but don't quote me on that.

Since these are Euclidean spaces, the difference between contravariant and covariant indices is non-existent. So it doesn't matter whether the indices are upstairs or downstairs.

I would say not 'nonexistent', but rather 'avoidable'; or, even better, that there is an isomorphism in an appropriate sense. (Compare the situation for strings and lists of characters; the types are isomorphic, but I wouldn't say that the difference between them is non-existent!)

Re: Einstein Notation applied in programming

#17

Been a while since GR, but I'm pretty sure some of those indices are supposed to be on top. A_{ij} = B_i^k * C_{jk} possibly, but don't quote me on that.

As cabinpark (https://news.ycombinator.com/item?id=9165144) points out, it is not necessary to make this distinction; but, if you want to do so, canonically a matrix has one covariant and one contravariant index, so you would want something like

    A_i^j = B_i^k*C_k^j .

Re: Einstein Notation applied in programming

#18

Been a while since GR, but I'm pretty sure some of those indices are supposed to be on top. A_{ij} = B_i^k * C_{jk} possibly, but don't quote me on that.

Since these are Euclidean spaces, the difference between contravariant and covariant indices is non-existent. So it doesn't matter whether the indices are upstairs or downstairs.

Yeah, but I find that -- like dimensional analysis -- keeping careful track of what's upstairs and downstairs can help me keep a computation from going off the rails.

Re: Einstein Notation applied in programming

#19
post #16

Earlier quoted context omitted.

Since these are Euclidean spaces, the difference between contravariant and covariant indices is non-existent. So it doesn't matter whether the indices are upstairs or downstairs.

I would say not 'nonexistent', but rather 'avoidable'; or, even better, that there is an isomorphism in an appropriate sense. (Compare the situation for strings and lists of characters; the types are isomorphic, but I wouldn't say that the difference between them is non-existent!)

True. I'm just a lazy relativist though ;)

Re: Einstein Notation applied in programming

#20
Nice post. I studied with this notation too but I was not clever enough to connect it to my programming.

This is a good example of a situation where something like lisp's macro system would be very useful. But I don't understand what the author means by

On the other end of the spectrum, lisp is designed for its upper layer elegance. Its macros are structured all the way to the top. However, to achieve that, it limits its lower level representations. In fact, it takes work-around for simple switches and loops.

What's the work-around? Common Lisp and Clojure have built in conditionals and loops.

Post reply on HN