In [1]:
require 'symengine'
Out[1]:
Go ahead and try a function
In [2]:
print SymEngine.ascii_art
This shows that we have successfully loaded the module.
In [3]:
puts x = SymEngine::Symbol.new("x")
puts y = SymEngine::Symbol.new("y")
puts z = SymEngine::Symbol.new("z")
Then we can construct expressions out of them
In [4]:
e = (x-y)*(x**y/z)
e.to_s
Out[4]:
In SymEngine, every object is an instance of Basic or its subclasses. So, even an instance of SymEngine::Symbol is a Basic object.
In [5]:
x.class
Out[5]:
In [6]:
x.is_a? SymEngine::Basic
Out[6]:
Now that we have an expression, we would like to see it's expanded form using #expand
In [7]:
f = e.expand()
f.to_s
Out[7]:
Or check if two expressions are same
In [8]:
f == - (x**y*y/z) + (x**y*x/z)
Out[8]:
But e and f are not equal since they are only mathematically equal, not structurally
In [9]:
e == f
Out[9]:
Let us suppose you want to know what variables/symbols your expression has. You can do that with the #free_symbols method. The method #free_symbols returns a Set of the symbols that are in an expression.
In [10]:
f.free_symbols
Out[10]:
Let us use #map method to see the elements of the Set.
In [11]:
f.free_symbols.map { |x| x.to_s }
Out[11]:
#args returns the terms of the expression,
In [12]:
f.args.map { |x| x.to_s }
Out[12]:
or if it is a single term it breaks down the elements
In [13]:
f.args[0].args.map { |k| k.to_s }
Out[13]:
You can make objects of class SymEngine::Integer. It's like regular Integer in ruby kernel, except it can do all the operations a Basic object can like arithmetic operations, etc.
In [14]:
a = SymEngine::Integer.new(12)
b = SymEngine::Integer.new(64)
a**b
Out[14]:
And yes it can support numbers of arbitrarily large length.
In [15]:
(a**x).to_s
Out[15]:
You can also make objects of class SymEngine::Rational that is the SymEngine counterpart for Rationals in Ruby.
In [16]:
c = Rational('2/3')
d = SymEngine(c)
Out[16]:
Like any other Basic object arithmetic operations can be done on this one too.
In [17]:
(a-d).to_s
Out[17]:
You need not create an instance of SymEngine::Integer or SymEngine::Rational, every time you want to use them in an expression that uses many Integers. Let us say you already have Integer/Rational object. Even then you can use them without having to create a new SymEngine object.
In [18]:
k = (1 / (x * y) - x * y + 2) * (c + x * y) # c is a Rational object, not SymEngine::Rational
k.to_s
Out[18]:
As you can see, ruby kernel Integers and Rationals interoperate seamlessly with the SymEngine objects.
In [19]:
k.expand.to_s
Out[19]:
In [20]:
d = SymEngine(1.2)
Out[20]:
In [21]:
c = SymEngine(Complex(2.3, 3.2))
Out[21]:
In [22]:
require 'bigdecimal'
r1 = SymEngine(BigDecimal("12.3"))
r2 = SymEngine::RealMPFR.new(12.3, 200)
Out[22]:
In [23]:
i = SymEngine::I
c1 = r1 + i * r2
Out[23]:
In [24]:
i = SymEngine::I
e = SymEngine::E
eg = SymEngine::EULER_GAMMA
pi = SymEngine::PI
i.inspect + e.inspect + eg.inspect + pi.inspect
Out[24]:
In [25]:
i1 = SymEngine::sin(pi)
i2 = SymEngine::cos(0.2)
i3 = SymEngine::tan(pi/4)
i4 = SymEngine::csc(pi/2)
i5 = SymEngine::sec(0.2)
i6 = SymEngine::cot(pi/4)
print "sin(pi): ", i1,"\ncos(0.2): ", i2, "\ntan(pi/4): ", i3, "\ncsc(pi/2): ", i4, "\nsec(0.2): ", i5,"\ncot(pi/4): ", i6, "\n"
In [26]:
i1 = SymEngine::asin(1)
i2 = SymEngine::acos(0)
i3 = SymEngine::atan(5)
i4 = SymEngine::acsc(1)
i5 = SymEngine::asec(0.2)
i6 = SymEngine::acot(0.5)
print "i1: ", i1,"\ni2: ", i2, "\ni3: ", i3, "\ni4: ", i4, "\ni5: ", i5,"\ni6: ", i6, "\n"
In [27]:
i1 = SymEngine::sinh(pi)
i2 = SymEngine::cosh(0.2)
i3 = SymEngine::tanh(pi/4)
i4 = SymEngine::csch(pi/2)
i5 = SymEngine::sech(0.2)
i6 = SymEngine::coth(pi/4)
print "sinh(pi): ", i1,"\ncosh(0.2): ", i2, "\ntanh(pi/4): ", i3, "\ncsch(pi/2): ", i4, "\nsech(0.2): ", i5,"\ncoth(pi/4): ", i6, "\n"
In [28]:
i1 = SymEngine::asinh(1)
i2 = SymEngine::acosh(0)
i3 = SymEngine::atanh(5)
i4 = SymEngine::acsch(1)
i5 = SymEngine::asech(0.2)
i6 = SymEngine::acoth(0.5)
print "i1: ", i1,"\ni2: ", i2, "\ni3: ", i3, "\ni4: ", i4, "\ni5: ", i5,"\ni6: ", i6, "\n"
In [29]:
gcd = SymEngine::gcd(45, 40)
lcm = SymEngine::lcm(45, 40)
print "for 45 and 40,\ngcd is: ", gcd, "\nlcm is: ",lcm, "\n"
Next Prime
In [30]:
np = SymEngine::nextprime(5)
Out[30]:
Quotient
In [31]:
q = SymEngine::quotient(5, 2)
Out[31]:
Lucas and Fibonacci series
In [32]:
l = SymEngine::lucas(3)
f = SymEngine::fibonacci(3)
p l, f
Out[32]:
Binomials
In [33]:
b = SymEngine::binomial(5, 2)
Out[33]: