user2820579
user2820579

Reputation: 3461

Create arbitrary arrays with symbolic variables

Is there a built-in function to create arrays with symbolic variables of arbitrary dimension?

For instance, to create N^2 symbolic variables to fill a corresponding matrix:

[x11, x12,   ,x1N
 ..., ...,   ,..
 XN1, xN2,   ,xNN]

Upvotes: 0

Views: 1955

Answers (2)

Chris Rackauckas
Chris Rackauckas

Reputation: 19162

You just create arrays of symbolic variables. Array{T,N}. Julia's parametric typing handles it.

Example: SymEngine.jl

You can make a Matrix{SymEngine.Basic} (SymEngine.Basic is the type which its symbolic expressions are in). Julia's generic dispatches make things like inv work.

Edit:

Re-reading I think there may be slightly more to answer this question? If you mean symbolic variables a la SymEngine.jl/SymPy.jl, then you can create different ones via interpolating into the string:

symbols("x$i")

Then the first part of the answer holds: just use a Julia array which is typed for this type.

arr[i,j] = symbols("x$i$j")

It's hard to know from the question whether this is what you're looking for or something else.

Upvotes: 3

RedHand
RedHand

Reputation: 309

I'm a bit unclear on the phrase "symbolic variables" since Julia has a specific symbol type.

If a variable is pointer to a value, then your question could be about creating arrays in which each element is also a pointer to some value - this would be an array of variables.

In Julia you could create an array in which each element is a pointer to a variable (pointer to a pointer to a value). This would be an array of Symbols. (If my thinking on this is wrong, somebody please correct me.)

There isn't a single function to create an array of Symbols, but for your example (single prefix, square matrix) it's pretty easy.

  1. Create array of string prefixes
  2. Create array of string element indices
  3. Concatenate prefix and indices
  4. Convert strings to Symbols

function SymArray(N)  
    A = Array{Symbol}(N,N)       # initialize output array  
    prefixes = fill("x", (N,N))  # create array of prefixes  
    rix = collect(1:N)           # create column of numeric row indices  

    for i in collect(1:N)
        tmpCol = string.(rix, i) # add column indices; 'dot' applies string() to each element
        A[:,i] = tmpCol          # add fully indexed column to output array
    end

    A = Symbol.(prefixes, A)     # concatenate & convert strings to symbols

    return(A)
end

julia> S = SymArray(3)
3×3 Array{Symbol,2}:
 :x11  :x12  :x13
 :x21  :x22  :x23
 :x31  :x32  :x33

julia> S[1,1]
:x11

However, we haven't defined x11, so it doesn't point to anything.

julia> eval(S[1,1])
ERROR: UndefVarError: x11 not defined
 in eval(::Module, ::Any) at ./boot.jl:234
 in eval(::Any) at ./boot.jl:233

Any time you store a value into a named variable, you can access the value from the array of Symbols.

julia> x11 = 1.2
1.2

julia> eval( S[1,1] )
1.2

julia> 3*eval(S[1])
3.6

This might be an interesting way to create a lookup table.

Upvotes: 1

Related Questions