Edit me
Similar to the sym class in Matlab, all symbolic expression in FROST is an object of SymExpression class. A SymExpression object can be created explicitly, or can be obtained from any symbolic calculation.

Create Symbolic Expression Explicitly

You can create a SymExpression object by explicitly calling the class constructor function. The input argument can be one of the following data types:

  • numeric: create a numeric symbolic expression object, e.g., 1, 3.1415
>> SymExpression(pi)

ans =

3.141593

>> SymExpression(hilb(3))

ans =

{{1, 0.5, 0.333333}, {0.5, 0.333333, 0.25}, {0.333333, 0.25, 0.2}}

  • char: create a single symbolic expression specified by the text, e.g., Cos[x]
>> SymExpression('Cos[x]')

ans =

Cos[x]
  • cell: create a tensor array in Mathematica specified by the input cell.
>> SymExpression({1,hilb(3),'Cos[x]'})

ans =

{1, {{1, 0.5, 0.333333}, {0.5, 0.333333, 0.25}, {0.333333, 0.25, 0.2}}, Cos[x]}

  • struct: create a association in Mathematica specified by the input struct.
>> b = 

  struct with fields:

      text: 'hello world'
    matrix: [3×3 double]
       num: -Inf

>> SymExpression(b)

ans =

<|"text" -> "hello world", "matrix" -> {{1, 0.5, 0.333333}, {0.5, 0.333333, 0.25}, {0.333333, 0.25, 0.2}}, "num" -> Infinity|>

  • SymExpression: generated a copy of the given SymExpression.

>> b_vec = SymExpression(a_vec)

ans =

{{var$1$1}, {var$2$1}, {var$3$1}}

Create with DelayedSet Option

A SymExpression object can also be created with DelayedSet option. If this option is enabled, it will use the SetDelayed operation in Mathematica. For more information, see here.

To use this option, call the constructor function with additional option argument, e.g.,

>> a = SymExpression('RandomReal[10]','DelayedSet',true);

Unlike Set, SetDelayed will only store the expression without evaluating the expression. For example, in the above example, the variable is assigned to be a random number. When you using SetDelayed, the expression RandomReal[10] will be evaluated every time you access the variable. So the actual value of a will change very time.

>> a

ans =

2.604258849577189

>> a

ans =

1.9781292304644342

However, if you use Set, the the expression RandomReal[10] will be evaluated once at the time of assigning. So the value of a stays the same.

>> a = SymExpression('RandomReal[10]')

ans =

4.085516205158337

>> a

ans =

4.085516205158337

Manipulating Symbolic Expression

Once a SymExpression object is created, you can perform many mathematical calculation on the object, just like normal Matlab data types.

Basic Arithmetic Computation

SymExpression class overwrote the basic arithmetic operators, such as +, -, *, /, inv, transpose, etc.

>> a = SymVariable('a');
b = SymVariable('b');
c = a+b

ans =

a + b

>> d = b*c

ans =

b*(a + b)

>> e = d.^2

ans =

b^2*(a + b)^2

Math Functions

You can also perform many math functions on the SymExpression objects. The return value is still a SymExpression object.

>> cos(e)

ans =

Cos[b^2*(a + b)^2]

For a complete list of available math functions, see here.

Data Conversion

The return value of any symbolic calculation is always a SymExpression object. Sometimes, you may want to convert it to other data type, such as double or char.

Convert to Numeric Value

>> a = SymExpression('Cos[Pi]')

ans =

-1

>> b = double(a)

b =

    -1

Here, a is a SymExpression object but b is a numeric data.

Convert to Text

You can also convert the symbolic expression into text, which will print out the Mathematica symbolic expression in Matlab char data.

>> char(cos(e)) % e is from previous example

ans =

Cos[b^2*(a + b)^2]

The output here is a char data, not SymExpression object.

Concatenate

Two or more symbolic expressions can be concatenated (vertical or horizontal) into an array of symbolic expression.

>> [c,d] % c,d are from the previous example

ans =

{{a + b, b*(a + b)}}


>> [c;d]

ans =

{{a + b}, {b*(a + b)}}


You can also call horzcat or vertcat function explicitly, e.g.,


>> horzcat(c,d)

ans =


{{a + b, b*(a + b)}}

>> vertcat(c,d)

ans =

{{a + b}, {b*(a + b)}}

Subscripted Reference and Assign

You can reference or assign sub-element of an array using subscripted indices.

>> vec = [c;d] % c,d are from the previous example
>> vec(2)

ans =

{b*(a + b)}
>> vec(2) = 3

ans =


{{a + b}, {3}}

Find Symbolic Variables in an Expression

Sometimes it is useful to find all symbolic variables in a complicated expression. For this purpose, you can use symvar function:

>> tomatrix(e)*b_vec % tomatrix converts a scalar or 1-D array to a 2-D matrix

ans =

{{b^2*(a + b)^2*var$1$1}, {b^2*(a + b)^2*var$2$1}, {b^2*(a + b)^2*var$3$1}}

>> symvar(ans)

ans =

{{b, a, var$1$1, var$2$1, var$3$1}}

Substitute Symbolic Variables in an Expression

You can also substitute a particular symbolic variable in an expression to something else, such as another variable, a complex expression or a numeric data. This can be done using function subs. The following example replace a and b with numeric value 1 and 2 in the previous example:

>> expr = tomatrix(e)*b_vec;
>> subs(expr, {SymVariable('a'),SymVariable('b')}, {1,2})

ans =


{{36*var$1$1}, {36*var$2$1}, {36*var$3$1}}

Here, the first argument is the original expression, the second argument is the list of symbolic variable to be replaced, and the last argument is the new value.

The symbolic variable can be also an array, e.g.,

>> subs(expr, b_vec, [1,2,3])

ans =

{{b^2*(a + b)^2}, {2*b^2*(a + b)^2}, {3*b^2*(a + b)^2}}

Tags: symbolic