@sym
: x =
sym (y)
¶@sym
: x =
sym (y, assumestr)
¶@sym
: x =
sym (y, assumestr1, assumestr2, …)
¶@sym
: x =
sym (A, [n, m])
¶@sym
: x =
sym (y, ratflag)
¶@sym
: x =
sym (handle)
¶Define symbols and numbers as symbolic expressions.
y can be an integer, a string or one of several special double values. It can also be a double matrix or a cell array.
Examples:
x = sym ('x') ⇒ x = (sym) x y = sym ('2') ⇒ y = (sym) 2 y = sym (3) ⇒ y = (sym) 3 y = sym (inf) ⇒ y = (sym) ∞ y = sym (pi) ⇒ y = (sym) π y = sym (1i) ⇒ y = (sym) ⅈ
A sym of a sym is a sym (idempotence):
sym (sym (pi)) ⇒ (sym) π
A matrix of integers can be input:
sym ([1 2; 3 4]) ⇒ (sym 2×2 matrix) ⎡1 2⎤ ⎢ ⎥ ⎣3 4⎦
However, if the entries are not simply integers, its better to call
sym
inside the matrix:
[sym(pi) sym(3)/2; sym(1) 0] ⇒ (sym 2×2 matrix) ⎡π 3/2⎤ ⎢ ⎥ ⎣1 0 ⎦
(Careful: at least one entry per row must be sym
to workaround
a GNU Octave bug https://savannah.gnu.org/bugs/?42152.)
Passing double values to sym is not recommended and will give a warning:
sym(0.1) -| warning: passing floating-point values to sym is -| dangerous, see "help sym"... ⇒ ans = (sym) 1/10
In this particular case, the warning is easy to avoid:
sym(1)/10 ⇒ (sym) 1/10
The “danger” here is that typing 0.1
gives a double-precision
floating-point value which differs slightly from the fraction
sym(1)/10
(and this is true for most decimal expressions).
It is generally impossible to determine which exact symbolic value the
user intended.
The warning indicates that some heuristics have been applied
(namely a preference for “small” fractions, small fractions
of π and square roots of integers).
Further examples include:
y = sym(pi/100) -| warning: passing floating-point values to sym is -| dangerous, see "help sym"... ⇒ y = (sym) π ─── 100
y = sym(pi)/100 ⇒ y = (sym) π ─── 100
(sym(pi)
is a special case; it does not raise the warning).
There is an additional reason for the float-point warning,
relevant if you are doing something like sym(1.23456789012345678)
.
In many cases, floating-point numbers should be thought of as
approximations (with about 15 decimal digits of relative accuracy).
This means that mixing floating-point values and symbolic computations
with the goal of obtaining exact results is often a fool’s errand.
Compounding this, symbolic computations may not always use numerically
stable algorithms (as their inputs are assumed exact) whereas a
floating-point input is effectively perturbed in the 15th digit.
If what you really want is higher-precision floating-point computations, see ‘vpa’.
If having read the above, you still want to do something
symbolic with floating-point inputs, you can use the ratflag
argument; by setting it to 'f'
, you will obtain the precise
rational number which is equal to the floating-point value:
sym(0.1, 'f') ⇒ (sym) 3602879701896397 ───────────────── 36028797018963968
The default heuristic rational behaviour can be obtained by passing
ratflag as 'r'
; this avoids the floating-point warning:
sym(0.1, 'r') ⇒ (sym) 1/10
For symbols, a second (and further) arguments can provide assumptions or restrictions on the type of the symbol:
x = sym ('x', 'positive') ⇒ x = (sym) x x = sym ('x', 'positive', 'integer') ⇒ x = (sym) x
See ‘assumptions’, for the list of supported assumptions.
Caution: it is possible to create multiple variants of the same symbol with different assumptions.
x1 = sym('x') ⇒ x1 = (sym) x x2 = sym('x', 'positive') ⇒ x2 = (sym) x x1 == x2 ⇒ (sym) x = x isAlways(x1 == x2) ⇒ 0 logical(x1 == x2) ⇒ 0
The second argument can also specify the size of a matrix:
A = sym('a', [2 3]) ⇒ A = (sym 2×3 matrix) ⎡a₁₁ a₁₂ a₁₃⎤ ⎢ ⎥ ⎣a₂₁ a₂₂ a₂₃⎦
or even with symbolic size:
syms m n positive integer B = sym('B', [m n]) ⇒ B = (sym) B (m×n matrix expression)
Anonymous functions can be converted to symbolic expressions by passing their function handle:
f = @(n, x) sin (pi*besselj (n, x)/2) ⇒ f = @(n, x) sin (pi * besselj (n, x) / 2) class (f) ⇒ function_handle sym(f) ⇒ (sym) ⎛π⋅besselj(n, x)⎞ sin⎜───────────────⎟ ⎝ 2 ⎠
It is also possible to save sym objects to file and then load them when
needed in the usual way with the save
and load
commands.
The underlying SymPy string representation (“srepr”) can usually be passed
directly to sym
: see ‘@sym/char’ for discussion of the details.
See also: syms, assumptions, @sym/assume, @sym/assumeAlso.
Package: symbolic