Skip to content

Commit 014a7ee

Browse files
committed
Merge pull request #17 from rajithv/classes
Add classes for Add, Mul and Pow
2 parents 9cfc224 + 7145bea commit 014a7ee

File tree

8 files changed

+50
-21
lines changed

8 files changed

+50
-21
lines changed

ext/symengine/symengine.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,14 @@ void Init_symengine() {
5555
rb_define_const(m_symengine, "PI", cconstant_pi());
5656
rb_define_const(m_symengine, "E", cconstant_e());
5757
rb_define_const(m_symengine, "EULER_GAMMA", cconstant_euler_gamma());
58+
59+
//Add class
60+
c_add = rb_define_class_under(m_symengine, "Add", c_basic);
61+
62+
//Mul class
63+
c_mul = rb_define_class_under(m_symengine, "Mul", c_basic);
64+
65+
//Pow class
66+
c_pow = rb_define_class_under(m_symengine, "Pow", c_basic);
67+
5868
}

ext/symengine/symengine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ VALUE c_symbol;
1212
VALUE c_integer;
1313
VALUE c_rational;
1414
VALUE c_constant;
15+
VALUE c_add;
16+
VALUE c_mul;
17+
VALUE c_pow;
1518

1619
#endif //SYMENGINE_H_

ext/symengine/symengine_macros.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ VALUE Klass_of_Basic(const basic_struct *basic_ptr) {
4545
return c_rational;
4646
case SYMENGINE_CONSTANT:
4747
return c_constant;
48+
case SYMENGINE_ADD:
49+
return c_add;
50+
case SYMENGINE_MUL:
51+
return c_mul;
52+
case SYMENGINE_POW:
53+
return c_pow;
4854
default:
4955
return c_basic;
5056
}

spec/arit_spec.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,32 @@
5454
f = e.expand
5555
assert e == (x + y)**2
5656
assert e != x**2 + 2 * x * y + y**2
57-
expect(e).to be_an_instance_of SymEngine::Basic
57+
expect(e).to be_a SymEngine::Basic
58+
expect(e).to be_an_instance_of SymEngine::Pow
5859
assert f == x**2 + 2 * x * y + y**2
59-
expect(f).to be_an_instance_of SymEngine::Basic
60+
expect(f).to be_a SymEngine::Basic
61+
expect(f).to be_an_instance_of SymEngine::Add
6062
end
6163

6264
it 'test_arit6' do
6365
x = SymEngine::Symbol.new('x')
6466
y = SymEngine::Symbol.new('y')
67+
6568
e = x + y
6669
assert(e.to_s == 'x + y') || 'y + x'
70+
expect(e).to be_an_instance_of SymEngine::Add
71+
6772
e = x * y
6873
assert(e.to_s == 'x*y') || 'y*x'
74+
expect(e).to be_an_instance_of SymEngine::Mul
75+
6976
e = Integer(2) * x
7077
assert e.to_s == '2*x'
78+
expect(e).to be_an_instance_of SymEngine::Mul
79+
7180
e = 2 * x
7281
assert e.to_s == '2*x'
82+
expect(e).to be_an_instance_of SymEngine::Mul
7383
end
7484

7585
it 'test_arit7' do

spec/basic_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
it 'returns a initialised Basic object that is result of
2828
self added to the argument' do
2929
c = @a + @b
30-
expect(c).to be_an_instance_of SymEngine::Basic
30+
expect(c).to be_a SymEngine::Basic
3131
expect(c.to_s).to eql('x + y')
3232
end
3333
end
@@ -37,7 +37,7 @@
3737
it 'returns a initialised Basic object that is result of
3838
argument subtracted from self' do
3939
c = @a - @b
40-
expect(c).to be_an_instance_of SymEngine::Basic
40+
expect(c).to be_a SymEngine::Basic
4141
expect(c.to_s).to eql('x - y')
4242
end
4343
end
@@ -47,7 +47,7 @@
4747
it 'returns a initialised Basic object that is result of
4848
self multiplied by the argument' do
4949
c = @a * @b
50-
expect(c).to be_an_instance_of SymEngine::Basic
50+
expect(c).to be_a SymEngine::Basic
5151
expect(c.to_s).to eql('x*y')
5252
end
5353
end
@@ -57,7 +57,7 @@
5757
it 'returns a initialised Basic object that is result of
5858
self divided by the argument' do
5959
c = @a / @b
60-
expect(c).to be_an_instance_of SymEngine::Basic
60+
expect(c).to be_a SymEngine::Basic
6161
expect(c.to_s).to eql('x/y')
6262
end
6363
end
@@ -67,7 +67,7 @@
6767
it 'returns a initialised Basic object that is result of
6868
self raised to the power of argument' do
6969
c = @a**@b
70-
expect(c).to be_an_instance_of SymEngine::Basic
70+
expect(c).to be_a SymEngine::Basic
7171
expect(c.to_s).to eql('x**y')
7272
end
7373
end
@@ -78,7 +78,7 @@
7878
and returns the result' do
7979
a = @a**3
8080
c = a.diff(@a)
81-
expect(c).to be_an_instance_of SymEngine::Basic
81+
expect(c).to be_a SymEngine::Basic
8282
expect(c).to eq(3 * @a**2)
8383
expect(a.diff(2)).to be_nil
8484
end
@@ -127,7 +127,7 @@
127127
context "doesn't take any argument" do
128128
it 'returns the negation of self' do
129129
p = -@x
130-
expect(p).to be_an_instance_of SymEngine::Basic
130+
expect(p).to be_a SymEngine::Basic
131131
expect(p.to_s).to eql('-a')
132132
end
133133
end
@@ -173,7 +173,7 @@
173173
e = (x + y + z) * (x + y + z)
174174
f = e.expand
175175
expect(e.to_s).to eql('(x + y + z)**2')
176-
expect(f).to be_an_instance_of SymEngine::Basic
176+
expect(f).to be_a SymEngine::Basic
177177
expect(f.to_s).to eql('2*x*y + 2*x*z + 2*y*z + x**2 + y**2 + z**2')
178178
expect(e == f).to be false
179179
end

spec/constant_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
it 'returns an initialised Basic object that is equalent to 1 + pi' do
2020
f = @a + @d
2121
expect(@a).to be_an_instance_of SymEngine::Constant
22-
expect(f).to be_an_instance_of SymEngine::Basic
22+
expect(f).to be_a SymEngine::Basic
2323
expect(f.to_s).to eql('1 + pi')
2424
end
2525
end
@@ -55,7 +55,7 @@
5555
it 'returns an initialised Basic object that is equalent to x + E' do
5656
f = @b + @x
5757
expect(@b).to be_an_instance_of SymEngine::Constant
58-
expect(f).to be_an_instance_of SymEngine::Basic
58+
expect(f).to be_a SymEngine::Basic
5959
expect(f.to_s).to eql('x + E')
6060
end
6161
end

spec/integer_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,27 @@
2727
context 'using a ruby integer as the second operand' do
2828
it 'succeeds with commutative operations' do
2929
b = @a * 2
30-
expect(b).to be_an_instance_of SymEngine::Basic
30+
expect(b).to be_a SymEngine::Basic
3131
expect(b).to eq(SymEngine::Integer.new(2) * @a)
3232
end
3333

3434
it 'succeeds with non commutative operations' do
3535
b = @a / 2
36-
expect(b).to be_an_instance_of SymEngine::Basic
36+
expect(b).to be_a SymEngine::Basic
3737
expect(b).to eq(@a / SymEngine::Integer.new(2))
3838
end
3939
end
4040

4141
context 'using a ruby integer as the first operand' do
4242
it 'succeeds with commutative operations' do
4343
b = 2 * @a
44-
expect(b).to be_an_instance_of SymEngine::Basic
44+
expect(b).to be_a SymEngine::Basic
4545
expect(b).to eq(@a * SymEngine::Integer.new(2))
4646
end
4747

4848
it 'succeeds with non commutative operations' do
4949
b = 2 / @a
50-
expect(b).to be_an_instance_of SymEngine::Basic
50+
expect(b).to be_a SymEngine::Basic
5151
expect(b).to eq(SymEngine::Integer.new(2) / @a)
5252
end
5353
end

spec/rational_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
it 'returns an instance of SymEngine::Rational class' do
88
a = Rational('2/3')
99
a = SymEngine::Rational.new(a)
10-
expect(a).to be_an_instance_of SymEngine::Rational
10+
expect(a).to be_a SymEngine::Rational
1111
expect(a.to_s).to eq('2/3')
1212
end
1313
end
@@ -22,27 +22,27 @@
2222
context 'using a ruby Rational as the second operand' do
2323
it 'succeeds with commutative operations' do
2424
c = @a * @b
25-
expect(c).to be_an_instance_of SymEngine::Basic
25+
expect(c).to be_a SymEngine::Basic
2626
expect(c).to eq(SymEngine::Rational.new(@b) * @a)
2727
end
2828

2929
it 'succeeds with non commutative operations' do
3030
c = @a / @b
31-
expect(c).to be_an_instance_of SymEngine::Basic
31+
expect(c).to be_a SymEngine::Basic
3232
expect(c).to eq(@a / SymEngine::Rational.new(@b))
3333
end
3434
end
3535

3636
context 'using a ruby Rational as the first operand' do
3737
it 'succeeds with commutative operations' do
3838
c = @b * @a
39-
expect(c).to be_an_instance_of SymEngine::Basic
39+
expect(c).to be_a SymEngine::Basic
4040
expect(c).to eq(@a * SymEngine::Rational.new(@b))
4141
end
4242

4343
it 'succeeds with non commutative operations' do
4444
c = @b / @a
45-
expect(c).to be_an_instance_of SymEngine::Basic
45+
expect(c).to be_a SymEngine::Basic
4646
expect(c).to eq(SymEngine::Rational.new(@b) / @a)
4747
end
4848
end

0 commit comments

Comments
 (0)