Julia how to eval the sym - julia

Help! I got a result in the form of sym. But it seems eval doesn't work. How can I get a numerical answer? Thanks.
#show BBias
#show eval(BBias)
#show typeof(eval(BBias))
BBias = -213.53387843501*cos(6) + 73.4119295548356*sin(6) - 50*sin(6)*cos(6) + 316.255048160247
eval(BBias) = -213.53387843501*cos(6) + 73.4119295548356*sin(6) - 50*sin(6)*cos(6) + 316.255048160247
typeof(eval(BBias)) = SymPy.Sym

What you have here is a symbolic SymPy expression, not a Julia expression. eval in Julia will only evaluate a Julia expression. The N function from SymPy will evaluate and expression down to it's floating point value.
You can do a typeof(BBias) to see what kind of an object this is. Since I don't know how you have generated that object, I cannot replicate it fully. But here is some simple example showing how to using SymPy from Julia
julia> using SymPy
julia> x=Sym("pi")
pi
julia> typeof(x)
SymPy.Sym
julia> y=sin(x)
0
julia> typeof(y)
SymPy.Sym
julia> typeof(eval(y))
SymPy.Sym
julia> z=N(y)
0
julia> typeof(z)
Int64
A detailed tutorial on using SymPy from Julia is available here: http://nbviewer.jupyter.org/github/jverzani/SymPy.jl/blob/master/examples/tutorial.ipynb

Related

How do I evaluate an expression with trignometric functions & also complex numbers in Sagemath?

I wanted to verify if n-th roots of unity are actually the n-th roots of unity?
i.e. if (root)^n = 1
I was trying to use sagemath to do this.
For e.g. for regular expressions sage seems to evaluate stuff
For e.g.
sage: x = var('x')
sage: f(x) = (x+2)^3
sage: f(5)
343
But I am unable to do this
sage: a = var('a')
sage: b = var('b')
sage: f(a, b) = (a + i*b)^3
sage: f(cos((2*pi)/3) , sin((2*pi)/3))
(1/2*I*sqrt(3) - 1/2)^3
How do I make sage raise it to power 3 & evaluate?
A sage expression has several methods to manipulate it, including expanding, factoring and simplifying:
e = f(cos((2*pi)/3) , sin((2*pi)/3))
e.expand()
e.simplify()
e.full_simplify()
e.factor()
You can see the list of all available methods by typing the name of the variable, followed by a dot, followed by a tabulation: e.<tab>.
In your case, it would appear e.full_simplify() should do the trick.
Relevant documentation:
sage doc: Symbolic Expressions;
sage doc: Tutorial for Symbolics and Plotting

Plotting a surface in Julia

I'd like to plot a surface in Julia, but using this syntax:
f(x)=x[1]-x[2]
not
f(x,y)=x-y
Could you help me do that?
In this case the simplest approach is to use a wrapper anonymous function:
(x...) -> f(x)
that you can pass to the plotting function instead of your original function.
Now you have:
julia> f(x)=x[1]-x[2]
f (generic function with 1 method)
julia> ((x...) -> f(x))(100, 5)
95

Print a docstring of a program to stdout in Julia

Julia accepts ?sin on the command line to display a help text. I believe such help texts are implemented as docstrings. I would like to print such docstrings from my Julia program at runtime. How to do that?
julia> #doc sin
sin(x)
Compute sine of x, where x is in radians.
sin(A::AbstractMatrix)
Compute the matrix sine of a square matrix A.
If A is symmetric or Hermitian, its eigendecomposition (eigen) is used to compute the
sine. Otherwise, the sine is determined by calling exp.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> sin(fill(1.0, (2,2)))
2×2 Matrix{Float64}:
0.454649 0.454649
0.454649 0.454649

SageMath: matrix_plot not returning any output within a subroutine

A=identity_matrix(2)
def f(n):
matrix_plot(n*A)
f(5)
I'm puzzled as the command matrix_plot(5*A) works fine on itself, but it produces no output when it goes inside a subroutine as in the above SageMath code.
I'd appreciate any help with understanding why this is so/resolving this.
PS I have tested this both on Sagecell.sagemath.org and a local installation.
As written, the function computes the matrix plot
but does not return it and does not display it.
To have the function display it:
def f(n, A):
matrix_plot(n*A).show()
To have the function return the plot (probably better):
def f(n, A):
return matrix_plot(n*A)
Then
sage: n = 5
sage: A = identity_matrix(2)
sage: f(n, A)

Logarithmic scale in a negative domain in Plots.jl

Consider this minimal example
julia> using Plots
julia> pyplot()
julia> x = -100:0.01:100
julia> y = x
julia> plot(x, y, xscale = :log10)
This raises the following error:
DomainError:
log10 will only return a complex result if called with a complex argument. Try log10(complex(x)).
Of course, this is because the logarithm of negative numbers is not well defined. However, in python you can overcome this problem using the command plt.xscale('symlog'). Is there an equivalent command for Plots.jl?

Resources