# How to convert the python code below to julia?

**URL:** <https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 26, 2022, 4:49pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151 "2022-12-26T16:49:58Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [December 26, 2022, 4:49pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/1 "2022-12-26T16:49:58Z")

</div>

Hello, good afternoon,

How to convert the below code from python to julia?

```python
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]

```

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 26, 2022, 5:02pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/2 "2022-12-26T17:02:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Hawkes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hawkes/32/22365_2.png) [@Hawkes](https://discourse.julialang.org/u/Hawkes)\
**Post date:** [December 26, 2022, 5:17pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/3 "2022-12-26T17:17:17Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [December 26, 2022, 5:18pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/4 "2022-12-26T17:18:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [December 26, 2022, 5:20pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/5 "2022-12-26T17:20:19Z")

</div>

I will try it. Did transform1 = [i\_hat1, j\_hat1]’ but got issues as the result is a Adjoint.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [December 26, 2022, 5:23pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/6 "2022-12-26T17:23:55Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Hawkes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hawkes/32/22365_2.png) [@Hawkes](https://discourse.julialang.org/u/Hawkes)\
**Post date:** [December 26, 2022, 5:24pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/7 "2022-12-26T17:24:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [December 26, 2022, 5:27pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/8 "2022-12-26T17:27:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [December 26, 2022, 5:47pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/9 "2022-12-26T17:47:37Z")

</div>

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.

```julia
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

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 26, 2022, 5:54pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/10 "2022-12-26T17:54:08Z")

</div>

Try

```julia
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.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [December 26, 2022, 6:03pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/11 "2022-12-26T18:03:35Z")

</div>

thats nice solution.

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 26, 2022, 8:50pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/12 "2022-12-26T20:50:28Z")

</div>

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

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

```

or in one line with

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

```

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [December 26, 2022, 10:05pm UTC](https://discourse.julialang.org/t/how-to-convert-the-python-code-below-to-julia/92151/13 "2022-12-26T22:05:56Z")

</div>

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