# How to convert the python code below to julia?

Hello, good afternoon,

How to convert the below code from python to julia?

``````from numpy import array
# Transformation 1
i_hat1 = array([0, 1])
j_hat1 = array([-1, 0])
transform1 = array([i_hat1, j_hat1]).transpose()
# Transformation 2
i_hat2 = array([1, 0])
j_hat2 = array([1, 1])
transform2 = array([i_hat2, j_hat2]).transpose()
# Combine Transformations
combined = transform2 @ transform1
# Test
print("COMBINED MATRIX:\n {}".format(combined))v = array([1, 2])
print(combined.dot(v))
# [-1, 1]
``````

Could you tell us what this code is meant to accomplish, mathematically? Probably more people here are conversant in math than python.

Does the code form two matrices and then multiply them?

3 Likes

Without knowing what its good for and “best” Julia style, this would be a 1:1 conversion:

``````i_hat1 = [0 1]
j_hat1 = [-1 0]
transform1 = [i_hat1;j_hat1]'
i_hat2 = [1 0]
j_hat2 = [1 1]
transform2 = [i_hat2;j_hat2]'
combined = transform2 * transform1
v = [1, 2]
combined * v
``````
2 Likes

Yes. Is related to algebra linear and matrix multiplication, linear transformation. I know Python, and I’m trying to get the same studies with julia.

I will try it. Did transform1 = [i_hat1, j_hat1]’ but got issues as the result is a Adjoint.

That worked. The result is correct. The issue was in concatenating the two vectors. [x, y] with comas. Using “;” solves the issue. Thank you all The `;` is doing vertical concatenation of the two row vectors `i_hat1` and `j_hat1`, followed by transposition using`'` as a suffix.

Be careful with the last line. You have used numpy’s dot function which does quite a lot of things depending on the input type. In this case it should give the sum product over the last axes. I have just written a standard matrix-vector product.

1 Like

I had read an thread about the dot with python and was aware of the issues you told, but in some calc with vectors, “,” did the trick. For matrix, needed vertical concatenation as you show. We must pay attention in this detail.

From vector to matrix doing the transpose operation, as the version in python did and the book says to do; ihs and jhs are vector, then better implement they as vectors. Python and numpy are hidding lots of things.

``````begin
ih1 = [0, 1];
jh1 = [-1, 0];

ih2 = [1, 0];
jh2 = [1, 1];

transform1 = [ih1'; jh1']';
transform2 = [ih2'; jh2']';

result =  transform2 * transform1;
v1 = [1, 2];
result * v1

end
``````

Try

``````ih1 = [0, 1]
jh1 = [-1, 0]

ih2 = [1, 0]
jh2 = [1, 1]

transform1 = [ih1 jh1]
transform2 = [ih2 jh2]
``````

Should be more efficient and natural for column major arrays.

4 Likes

thats nice solution.

I still think the question and the replies would benefit from an explanation of what the OP is trying to accomplish, mathematically. The way `transform1` and `transform2` are built is unnecessarily complicated. You can get the same in four lines with

``````A = [0 -1; 1 0]
B = [1 1; 0 1]
C = B*A
C*[1;2]
``````

or in one line with

``````[1 1; 0 1] * [0 -1; 1 0] * [1; 2]
``````

The book did this way because we need to observe the basis and the vector that is transformed. Just to be more didact.