I am looking for a way to represent a symbolic expression such as sqrt(3)*x so as to avoid sqrt(3) being calculated upfront.
Sample code:
using Symbolics
#variables x
y = sqrt(3)*x
Showing y, we can see that sqrt(3) has become a floating point.
Is there a mechanism to keep the sqrt in symbolic form?
is there a mechanism to keep the sqrt in symbolic form?
julia> using Symbolics
julia> #variables x
(x,)
julia> y = Symbolics.Term(sqrt,[3])*x
x*sqrt(3)
I cannot reproduce:
julia> using Symbolics
julia> #variables x
(x,)
julia> y = 1//3*x
(1//3)*x
julia> y
(1//3)*x
My config:
[0c5d862f] Symbolics v0.1.21
and
julia> versioninfo()
Julia Version 1.6.0
Commit f9720dc2eb (2021-03-24 12:55 UTC)
Platform Info:
OS: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core(TM) i7-9850H CPU # 2.60GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-11.0.1 (ORCJIT, skylake)
Funny, y = 1//3 * x works, but not y = sqrt(3) * x as according to the orignal question. So, the answer from user Nasser is really helpful thanks.
Related
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
I would like to declare the multivariate Ring $R[x_{1},x_{2},x_{3}]/(x_{2}^2-1, x_{1}^{3}x_{3}-2)$. It would be very helpful if there is some method to do this for an arbitrary number of variables.
Thank you so much,
I think AbstractAlgebra.jl is a very good fit for this. You can call it like:
julia> using AbstractAlgebra
julia> ring = ZZ
Integers
julia> S, (x, y, z) = PolynomialRing(ring, ["x", "y", "z"])
(Multivariate Polynomial Ring in x, y, z over Integers, AbstractAlgebra.Generic.MPoly{BigInt}[x, y, z])
julia> p1 = y^2 - 1
xy^2 - 1
julia> p2 = x^3 * z - 2
x^3*z - 2
julia> div(3 * (x^2 + y^3) * p1 + p2, p1)
3*x^2 + 3*y^3
julia> div(3 * (x^2 + y^3) * p1 + p2, p2)
1
And sorry if this doesn't answer your question, my mathematics is not that good. For more information, please visit the documentation.
According to my colleagues this is available in Oscar.jl [1] (which is built on Singular and AbstractAlgebra.jl, mentioned by #aahlback and myself in another answer).
The documentation is in the process of being written right now, so keep an eye on the package to see how this can be done in the very near future.
The implementation is in [2] if you want to take a look.
Edit: there looks to be a relevant example here [3]. Note the ring R in that example is a polynomial ring and does not correspond to your R.
For the coefficient ring (your R) you can currently use any field, Z or Z/nZ, according to the maintainer of the Singular project (which Oscar.jl uses for these computations). It's very, very hard to extend this to other rings.
[1] https://github.com/oscar-system/Oscar.jl/
[2] https://github.com/oscar-system/Oscar.jl/blob/master/src/Rings/MPolyQuo.jl
[3] https://oscar-system.github.io/Oscar.jl/dev/CommutativeAlgebra/ca/#Example-24
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?
I'm learning Julia by working through Chris Rackauckas' Introduction and I've come across a task that requires me to plot some data. I couldn't manage to get the Plots module to import so I tried a simple test:
using Plots
x = 1:10
y = 0.5*x + 3
plot(x, y)
When I first run this piece of code using the Juno IDE I get an error:
LoadError: LoadError: LoadError: syntax: unhandled expr (error #<julia: Main.Base.MethodError(f=FixedPointNumbers.#floattype(), args=(Main.FixedPointNumbers.FixedPoint{UInt8, 8},))>)
in include_from_node1(::String) at .\loading.jl:488 (repeats 2 times)
in eval(::Module, ::Any) at .\boot.jl:234
in require(::Symbol) at .\loading.jl:415
in include_string(::String, ::String) at .\loading.jl:441
in include_string(::Module, ::String, ::String) at 2
This refers to the using statement in my snippet. This error does not appear when I run from the REPL. The version info is as follows:
Julia Version 0.5.0
Commit 3c9d753 (2016-09-19 18:14 UTC)
Platform Info:
System: NT (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i7-4810MQ CPU # 2.80GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
LAPACK: libopenblas64_
LIBM: libopenlibm
LLVM: libLLVM-3.7.1 (ORCJIT, haswell)
I currently have version 0.10.3 of Plots installed.
If you provide some version/platform information by sharing the output of versioninfo(), one can help you better.
For instance, the below excerpt
Pkg.add("Plots")
using Plots
plotly() # this backend is installed by default
x = 1:10
y = 0.5*x + 3
plot(x, y)
works well under
Julia Version 0.5.0
Commit 3c9d753* (2016-09-19 18:14 UTC)
Platform Info:
System: Linux (x86_64-pc-linux-gnu)
CPU: Intel(R) Core(TM) i7-3720QM CPU # 2.60GHz
WORD_SIZE: 64
BLAS: libopenblas (NO_LAPACK NO_LAPACKE NO_AFFINITY SANDYBRIDGE)
LAPACK: liblapack
LIBM: libm
LLVM: libLLVM-3.7.1 (ORCJIT, ivybridge)
Maybe you should conisder Pkg.add("PyPlot") or a similar backend, and try it again later?
To work with the Run File command using the Juno package in the Atom IDE the plot has to be assigned to a variable and passed to the display function.
using Plots
pyplot()
x = 1:100
y = 0.5*x + 10
println(y)
graph = plot(x, y)
display(graph)
This will display the graph in Juno's Plots window. In the comments Arda Aytekin suggested that pyplot(display=true) or graph = plot(x, y, display=true) could be used, which leads to the graph displaying in a separate pyplot window.
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