### Install SymEngine.jl Source: https://symengine.org/SymEngine.jl/index Installs the SymEngine.jl package and its dependencies using Julia's built-in package manager. ```Julia julia> Pkg.add("SymEngine") ``` -------------------------------- ### Symbolic Simplification with SymbolicUtils Source: https://symengine.org/SymEngine.jl/basicUsage Demonstrates using SymbolicUtils for symbolic simplification, specifically simplifying a trigonometric identity. This example requires the SymbolicUtils package and shows how to define and simplify symbolic expressions. ```julia using SymbolicUtils; @vars x simplify(sin(x)^2 + cos(x)^2) ``` -------------------------------- ### SymEngine.jl expand Function Source: https://symengine.org/SymEngine.jl/basicUsage Demonstrates the `expand` function for multiplying out terms and collecting like terms in symbolic expressions. It also includes a performance comparison example against the `Symbolics` library. ```julia expand(a + 2(b+2)^2 + 2a + 3(a+1)) ``` ```julia 11 + 6*a + 8*b + 2*b^2 ``` ```julia expand((x + 1)*(x - 2) - (x - 1)*x) ``` ```julia -2 ``` ```julia function expand_test(a,b,c) x = expand(((a+b+c+1)^20)); y = expand(((a+b+c+1)^15)); z = expand(x*y) end ``` ```julia expand_test (generic function with 1 method) ``` -------------------------------- ### SymEngine.jl diff Function for Differentiation Source: https://symengine.org/SymEngine.jl/basicUsage Explains and demonstrates the `diff` function for symbolic differentiation, including higher-order and mixed derivatives. It also includes a performance comparison example against the `Symbolics` library. ```julia diff(a + 2(b+2)^2 + 2a + 3(a+1), b) ``` ```julia 4*(2 + b) ``` ```julia @vars x ex = sin(sin(x)) diff(ex, x, 3) ``` ```julia -cos(x)*cos(sin(x)) - cos(x)^3*cos(sin(x)) + 3*sin(x)*cos(x)*sin(sin(x)) ``` ```julia @vars a b x y ex = a*x*y^2 + b*x^2*y diff(ex, x, x, y) # also diff(ex, x, 2, y 1) does 2 in x one in y ``` ```julia 2*b ``` ```julia @vars x f() diff(f(x), x) ``` ```julia Derivative(f(x), x) ``` ```julia function diff_test(D, x) expr = sin(sin(sin(sin(x)))); for i = 1:10 expr = D(expr, x) end expr end ``` ```julia diff_test (generic function with 1 method) ``` -------------------------------- ### Example: Replacing Operation Head Source: https://symengine.org/SymEngine.jl/basicUsage Demonstrates the `replace_head` function, which recursively traverses a symbolic expression and replaces occurrences of a specified operation `u` with another operation `v`. This example shows replacing `tan` with `cos` in a nested expression. ```julia replace_head(tan(sin(tan(x))), tan, cos) ``` -------------------------------- ### Example: Replacing Expression Patterns Source: https://symengine.org/SymEngine.jl/basicUsage Illustrates the usage of the `replace_exact` function, which is designed to substitute a specific pattern `p` with a new expression `q` within a given symbolic expression `ex`. This is analogous to the `subs` function in symbolic manipulation libraries. ```julia @vars x u replace_exact(sin(x^2)*cos(x^2), x^2, u) # subs(ex, p => q) ``` -------------------------------- ### SymEngine Utility and Versioning Source: https://symengine.org/SymEngine.jl/apidocs Documentation for utility functions in SymEngine.jl, including checking library components and retrieving the libsymengine version. ```APIDOC SymEngine.get_libversion Get libsymengine version SymEngine.have_component Check whether libsymengine was compiled with comp ``` -------------------------------- ### Creating and Manipulating Symbolic Numbers Source: https://symengine.org/SymEngine.jl/basicUsage Demonstrates the creation of symbolic numbers using the `Basic` constructor and performing basic arithmetic operations. It also shows how to access built-in symbolic constants. ```julia x = Basic(1) x / 2 ``` -------------------------------- ### Load SymEngine.jl Package Source: https://symengine.org/SymEngine.jl/basicUsage Loads the SymEngine.jl package into the current Julia session, making its functionalities available for use. This is the initial step before utilizing any symbolic computation features. ```Julia using SymEngine ``` -------------------------------- ### CDenseMatrix LU Decomposition and Linear Algebra Source: https://symengine.org/SymEngine.jl/basicUsage Demonstrates LU decomposition for matrices of `Basic` values using `CDenseMatrix`. It also shows how to solve linear symbolic equations using the `\` operator and compute matrix inverses, determinants, transposes, and adjoints. ```julia @vars a x A = [x (1 + a*x); x (1+a^2*x)] using LinearAlgebra lu(A) ``` ```julia LinearAlgebra.LU{Basic, Matrix{Basic}} L factor: 2×2 Matrix{Basic}: 1 0 1 1 U factor: 2×2 Matrix{Basic}: x 1 + a*x 0 1 + a^2*x - (1 + a*x) ``` ```julia @vars A[1:2, 1:2] x[1:2] A \ x ``` ```julia 2-element Vector{Basic}: (x₁ - (x₂ - x₁*A₂_₁/A₁_₁)*A₁_₂/(A₂_₂ - A₂_₁*A₁_₂/A₁_₁))/A₁_₁ (x₂ - x₁*A₂_₁/A₁_₁)/(A₂_₂ - A₂_₁*A₁_₂/A₁_₁) ``` ```julia @vars x A = [1 x; 2 x^2] inv(A) ``` ```julia 2×2 Matrix{Basic}: 1 + 2*x/(-2*x + x^2) -x/(-2*x + x^2) -2/(-2*x + x^2) (-2*x + x^2)^(-1) ``` ```julia det(A) ``` ```julia -2*x + x^2 ``` ```julia A = [im x; 2im x^2] transpose(A) ``` ```julia 2×2 Matrix{Basic}: im 2*im x x^2 ``` -------------------------------- ### SymEngine Symbol Creation Source: https://symengine.org/SymEngine.jl/apidocs Documentation for creating symbolic variables in SymEngine.jl. ```APIDOC SymEngine.symbols `symbols(::Symbol)` construct symbolic value Examples: a = symbols(:a) x = symbols("x") x,y = symbols("x y") ``` -------------------------------- ### SymEngine Symbolic Substitution and Series Expansion Source: https://symengine.org/SymEngine.jl/apidocs Documentation for functions that perform substitutions and series expansions on symbolic expressions. ```APIDOC SymEngine.lambdify evaluates a symbolless expression or returns a function SymEngine.series Series expansion to order `n` about point `x0` SymEngine.subs Substitute values into a symbolic expression. Examples: @vars x y ex = x^2 + y^2 subs(ex, x, 1) # 1 + y^2 subs(ex, (x, 1)) # 1 + y^2 subs(ex, (x, 1), (y,x)) # 1 + x^2, values are substituted left to right. subs(ex, x=>1) # alternate to subs(x, (x,1)) subs(ex, x=>1, y=>1) # ditto ``` -------------------------------- ### Define Symbols Source: https://symengine.org/SymEngine.jl/apidocs Demonstrates how to define symbolic variables using the `symbols` function in SymEngine.jl. ```julia x,y,z = symbols("x,y,z") ``` -------------------------------- ### SymEngine Core Types and Methods Source: https://symengine.org/SymEngine.jl/apidocs Documentation for core SymEngine types and methods related to symbolic expression manipulation, including retrieving arguments, names, symbols, and checking expression properties. ```APIDOC SymEngine.AsciiArt Type show symengine logo SymEngine.N Convert a SymEngine numeric value into a Julian number SymEngine.coeff! Return coefficient of `x^n` term, `x` a symbol SymEngine.free_symbols Return free symbols in an expression as a `Set` SymEngine.function_symbols Return function symbols in an expression as a `Set` SymEngine.get_args Return arguments of a function call as a vector of `Basic` objects SymEngine.get_name Return name of function symbol SymEngine.get_symbol Helper function to lookup a symbol from libsymengine SymEngine.get_symengine_class Get SymEngine class of an object (e.g. 1=>:Integer, 1//2 =:Rational, sin(x) => :Sin, ...) SymEngine.has_symbol Does expression contain the symbol SymEngine.is_constant Is expression constant SymEngine.is_symbol Is expression a symbol ``` -------------------------------- ### SymEngine Matrix Decomposition Source: https://symengine.org/SymEngine.jl/apidocs Documentation for LU decomposition of SymEngine's CDenseMatrix. ```APIDOC LinearAlgebra.lu LU decomposition for CDenseMatrix, dense matrices of symbolic values Also: lufact(a, val{:false}) for non-pivoting lu factorization ``` -------------------------------- ### Creating Julia Functions from Symbolic Expressions Source: https://symengine.org/SymEngine.jl/basicUsage Demonstrates how to convert a symbolic expression into a callable Julia function using `lambdify`. This is useful for evaluating expressions with specific numerical inputs and shows the resulting type conversion. ```julia @vars x ex = x * tanh(exp(x)) l = lambdify(ex) typeof(ex(1)), typeof(l(1)) ``` -------------------------------- ### Basic Type Wrapper Source: https://symengine.org/SymEngine.jl/basicUsage Demonstrates the `Basic` type, which wraps the underlying symengine object. It can be used to construct numeric symbols or expressions. ```Julia n = Basic(10) ``` -------------------------------- ### Create Symbolic Expressions Source: https://symengine.org/SymEngine.jl/basicUsage Builds symbolic expressions by combining symbolic variables and numbers using standard Julia arithmetic operations. The library groups like terms but does not automatically expand expressions. ```Julia ex1 = a + 2*(b+2)^2 + 2*a + 3*(a+1) ``` -------------------------------- ### SymEngine.jl as_numer_denom and coeff Functions Source: https://symengine.org/SymEngine.jl/basicUsage Illustrates the `as_numer_denom` function for separating fractional expressions into numerator and denominator, and the `coeff` function for extracting coefficients of specific powers of a symbolic variable. ```julia @vars x y ex = (exp(x) - 1) / x SymEngine.as_numer_denom(ex) ``` ```julia (-1 + exp(x), x) ``` ```julia @vars a b c x p = a + b*x + c*x^2 [SymEngine.coeff(p, x, i) for i in 0:2] ``` ```julia 3-element Vector{Basic}: a b c ``` -------------------------------- ### Mutating Symbolic Operations Source: https://symengine.org/SymEngine.jl/basicUsage Demonstrates how to use mutating versions of operations (e.g., `sin!`) to minimize memory allocations when working with `Basic` symbolic variables. It also mentions other useful types for allocation optimization. ```julia @vars x a = Basic() SymEngine.sin!(a, x) a ``` -------------------------------- ### SymEngine.jl subs Function for Substitution Source: https://symengine.org/SymEngine.jl/basicUsage Explains and demonstrates the `subs` function for substituting parts of an expression tree with other expressions. It covers single and multiple substitutions using `=>` notation and the `call` method for direct value substitution. ```julia subs(a^2 + (b-2)^2, b=>a) ``` ```julia a^2 + (-2 + a)^2 ``` ```julia subs(a^2 + (b-2)^2, b=>2) ``` ```julia a^2 ``` ```julia subs(a^2 + (b-2)^2, a=>2) ``` ```julia 4 + (-2 + b)^2 ``` ```julia subs(a^2 + (b-2)^2, a^2=>2) ``` ```julia 2 + (-2 + b)^2 ``` ```julia subs(a^2 + (b-2)^2, a=>2, b=>3) ``` ```julia 5 ``` ```julia @vars x y z ex = x * y^2 * z^3 ex(2,3,4) == ex(x=>2, y=>3, z=>4) == ex(Pair.(free_symbols(ex), (2,3,4))...) ``` ```julia true ``` -------------------------------- ### Symbolic Mathematical Operations Source: https://symengine.org/SymEngine.jl/basicUsage Applies standard Julia mathematical operations and functions to symbolic expressions. This includes functions like `sin`, `tanh`, and `exp`. ```Julia @vars x ex = sin(x)^2 - x*tanh(exp(x)) ``` -------------------------------- ### Converting Symbolic to Floating Point Source: https://symengine.org/SymEngine.jl/basicUsage Shows how to convert symbolic values to Julia floating-point numbers using the `float` method. This method first attempts to identify an appropriate Julia type via `N` before conversion. ```julia float(x), float(x/2), float(PI) ``` -------------------------------- ### SymEngine.@vars Macro Source: https://symengine.org/SymEngine.jl/apidocs The `@vars` macro is used to define one or more variables or symbolic functions. It supports defining single variables, arrays of variables, and symbolic functions. ```julia @vars x y[1:5] z() ``` ```julia @vars x y z ``` ```julia @vars x[1:4] ``` ```julia @vars u(), x ``` -------------------------------- ### Converting Symbolic to Julia Numeric Types Source: https://symengine.org/SymEngine.jl/basicUsage Illustrates the use of the `N` function to convert symbolic values into their corresponding native Julia numeric types, such as integers, rationals, and floating-point numbers. ```julia N(x), N(x/2), N(PI) ``` -------------------------------- ### Define Array Variables with symbols Source: https://symengine.org/SymEngine.jl/basicUsage Defines symbolic vectors and matrices using list/matrix comprehensions combined with string interpolation and the `symbols` constructor. This allows for runtime determination of array sizes. ```Julia n = 4 [symbols("α_$i") for i in 1:n] ``` ```Julia W = [symbols("W_$(i)_$(j)") for i in 1:3, j in 1:4] ``` -------------------------------- ### Define Scalar Variables with symbols Source: https://symengine.org/SymEngine.jl/basicUsage Defines scalar symbolic variables using the `symbols` constructor. It can take a single symbol or a string with space-separated names to define multiple variables. ```Julia a=symbols(:a); b=symbols(:b) ``` ```Julia a,b = symbols("a b") ``` -------------------------------- ### TermInterface Expression Manipulation Source: https://symengine.org/SymEngine.jl/basicUsage Provides custom functions for symbolic expression manipulation using the TermInterface package. It includes utilities for mapping over expressions, replacing specific patterns, and replacing the operation head of expressions, mimicking `subs` functionality. ```julia using TermInterface function map_matched(x, is_match, f) if SymEngine.is_symbol(x) return is_match(x) ? f(x) : x end is_match(x) && return f(x) iscall(x) || return x children = map_matched.(arguments(x), is_match, f) maketerm(Basic, operation(x), children, nothing) end replace_exact(ex, p, q) = map_matched(ex, ==(p), _ -> q) function replace_head(ex, u, v) !iscall(ex) && return ex args′ = (replace_head(a, u, v) for a ∈ arguments(ex)) op = operation(ex) λ = op == u ? v : op ex = maketerm(Basic, λ, args′, nothing) end ``` -------------------------------- ### Matrix-Vector Multiplication Source: https://symengine.org/SymEngine.jl/basicUsage Performs matrix-vector multiplication using Julia's standard operators on symbolic vectors and matrices created with SymEngine.jl. ```Julia @vars x[1:2] A[1:3,1:2] A * x ``` -------------------------------- ### Define Array Variables with @vars Source: https://symengine.org/SymEngine.jl/basicUsage Uses the `@vars` macro with indexing notation to define symbolic vectors and matrices. Indices can be specified with offsets. ```Julia @vars x[1:2] A[1:2, 1:2] ``` ```Julia @vars y[-2:2] ``` -------------------------------- ### SymEngine.jl Introspection Functions Source: https://symengine.org/SymEngine.jl/basicUsage Provides a collection of functions for inspecting symbolic expressions, including checking for constants, symbols, free symbols, function symbols, arguments, and specific numeric type predicates. ```APIDOC SymEngine.is_constant(ex) Checks if the expression contains any free symbols. Returns `false` if free symbols are present. SymEngine.has_symbol(ex, x) Checks if the symbolic expression `ex` contains the specific symbol `x`. free_symbols(ex) Returns a collection of all free symbols present in the expression `ex`. SymEngine.function_symbols(ex) Returns symbolic functions defined within the expression, such as those defined by `@vars f()`. SymEngine.get_args(ex) Returns the arguments of a given expression `ex` as a vector. Returns an empty vector if the expression has no arguments. SymEngine.is_a_Number(ex) Checks if the expression is a symbolic number. SymEngine.is_a_Integer(ex) Checks if the expression is a symbolic integer. SymEngine.is_a_Rational(ex) Checks if the expression is a symbolic rational number. SymEngine.is_a_RealDouble(ex) Checks if the expression is a symbolic real number stored as a double-precision float. SymEngine.is_a_RealMPFR(ex) Checks if the expression is a symbolic real number stored using MPFR. SymEngine.is_a_Complex(ex) Checks if the expression is a symbolic complex number. SymEngine.is_a_ComplexDouble(ex) Checks if the expression is a symbolic complex number stored using double-precision floats. SymEngine.is_a_ComplexMPC(ex) Checks if the expression is a symbolic complex number stored using MPC. iszero(ex) Checks if the expression evaluates to zero. isone(ex) Checks if the expression evaluates to one. isinteger(ex) Checks if the expression represents an integer. isreal(ex) Checks if the expression represents a real number. isfinite(ex) Checks if the expression is finite. isinf(ex) Checks if the expression is infinite. isnan(ex) Checks if the expression is Not-a-Number (NaN). ``` -------------------------------- ### Define Scalar Variables with @vars Source: https://symengine.org/SymEngine.jl/basicUsage Uses the `@vars` macro to define scalar symbolic variables `a` and `b` directly in the local scope. This is a convenient way for interactive use. ```Julia @vars a b ``` -------------------------------- ### SymEngine.jl Type Inspection Functions Source: https://symengine.org/SymEngine.jl/basicUsage Provides functions to inspect the internal storage type of a symbolic object within SymEngine.jl, including its class representation in SymEngine and Julia. ```APIDOC SymEngine.get_type(ex) Returns an unsigned integer uniquely identifying the internal storage type of the expression `ex`. SymEngine.get_symengine_class(ex) Returns a symbol representing the top-most operation or storage type of the object within SymEngine. SymEngine.get_julia_class(ex) Returns a symbol representing the Julia type of the top-most operation or storage type of the object within SymEngine. operation(ex) (Requires TermInterface loaded) Returns the outermost function or operation for a given expression `ex`. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.