Passing expressions by reference inside expressions

I am new to Julia, so I apologize for any misunderstandings of the language that I may have. I recently used Python mostly and heavily used Sympy and its code generation functions, and it seems that Julia with metaprogramming functions is built to write code in exactly the style I like.

In particular, I want to build block matrices in Julia from a set of smaller building blocks with several different operations between them. For debugging purposes and because various intermediate matrices are used in other calculations, I want to save them as expressions containing variables so that I can quickly iterate and test different inputs without wrapping everything in a function.

Now, for a minimal study of the situation, let's say I have two expressions mat1 = :aand mat2 = :bthat I want to combine to form a new third expression:

mat3 = :($mat1 + $mat2)

The above method works fine until I change mat1and mat2, in which case I need to reevaluate mat3to reflect this update. This is due, I believe, that $mat1 + $mat2does not pass mat1and mat2the link, but rather to interpolate expressions inside during the evaluation of this line. The behavior that I want to achieve is that mat1it is mat2not inserted until I name it eval(mat3), preferably with a minimal template.

Is it possible to achieve this in a convenient syntax?

+4
1

mat3 mat1 mat2, mat1 mat2. .

, . , , . , , push! , A[1] = 5.

, :

A = [1, 2, 3]
A[1] = 4

A ; . , A, .

A = :(f(x))
A.args[1] = :g

A ; . , A, .

mat1 = :(f(x))
mat2 = :(f(y))
mat3 = :($mat1 + $mat2)
mat1.args[1] = :g

mat1 ; . . mat3 , , . , mat3 :(g(x) + f(y)).

( )

, , . =, - .

x = 2
x = 3

x 2 3. 2. , 2 , 2. , x , , : 3.

A = [1, 2, 3]
A = [4, 2, 3]

A; , A . . .

mat1 = :x
mat2 = :y
mat3 = :($mat1 + $mat2)
mat1 = :z

, :x, mat1 ; mat1 :z. , mat3, :x, .

, Symbol , . , , .

, , - . , .

mat1 = :x
mat2 = :y
mat3() = :($mat1 + $mat2)  # function definition
mat3()  # :(x + y)
mat1 = :z
mat3()  # :(z + y)
+9

Source: https://habr.com/ru/post/1677320/


All Articles