Function symbol Ruby Wrappers #51

Merged
merged 13 commits into from Jun 14, 2016

Projects

None yet

3 participants

@rajithv
Contributor
rajithv commented Jun 9, 2016

Wrappers for the function symbol class

rajithv added some commits Jun 9, 2016
@rajithv rajithv FunctionSymbol class and its methods e8a5670
@rajithv rajithv FunctionSymbol init working
b5a91fd
@abinashmeher999 abinashmeher999 commented on an outdated diff Jun 9, 2016
ext/symengine/ruby_function.c
@@ -37,3 +39,39 @@ IMPLEMENT_ONE_ARG_FUNC(gamma);
#undef IMPLEMENT_ONE_ARG_FUNC
+VALUE cfunction_functionsymbol_init(VALUE self, VALUE args)
+{
+ int argc = NUM2INT(rb_funcall(args, rb_intern("length"), 0, NULL));
+ printf("argc : %d\n", argc);
@abinashmeher999
abinashmeher999 Jun 9, 2016 Contributor

The function shouldn't print anything.

@abinashmeher999 abinashmeher999 commented on an outdated diff Jun 9, 2016
ext/symengine/ruby_function.c
@@ -37,3 +39,39 @@ IMPLEMENT_ONE_ARG_FUNC(gamma);
#undef IMPLEMENT_ONE_ARG_FUNC
+VALUE cfunction_functionsymbol_init(VALUE self, VALUE args)
+{
+ int argc = NUM2INT(rb_funcall(args, rb_intern("length"), 0, NULL));
+ printf("argc : %d\n", argc);
+ VALUE first = rb_ary_shift(args);
+ if( TYPE(first) != T_STRING ){
+ rb_raise(rb_eTypeError, "String expected as first argument");
@abinashmeher999
abinashmeher999 Jun 9, 2016 Contributor

Also raise error, if no arguments are passed.

@abinashmeher999 abinashmeher999 commented on an outdated diff Jun 9, 2016
ext/symengine/ruby_function.c
+{
+ int argc = NUM2INT(rb_funcall(args, rb_intern("length"), 0, NULL));
+ printf("argc : %d\n", argc);
+ VALUE first = rb_ary_shift(args);
+ if( TYPE(first) != T_STRING ){
+ rb_raise(rb_eTypeError, "String expected as first argument");
+ }
+ char *name = StringValueCStr( first );
+ char *c;
+
+ CVecBasic *cargs = vecbasic_new();
+
+ //basic_struct *cargs[argc];
+
+ basic x;
+ basic y;
@abinashmeher999
abinashmeher999 Jun 9, 2016 Contributor

This is not needed.

@abinashmeher999 abinashmeher999 commented on an outdated diff Jun 9, 2016
ext/symengine/ruby_function.c
+ if( TYPE(first) != T_STRING ){
+ rb_raise(rb_eTypeError, "String expected as first argument");
+ }
+ char *name = StringValueCStr( first );
+ char *c;
+
+ CVecBasic *cargs = vecbasic_new();
+
+ //basic_struct *cargs[argc];
+
+ basic x;
+ basic y;
+ basic_new_stack(x);
+ int i;
+ for(i = 0; i < argc-1; i++){
+ printf("%d : ", i);
@abinashmeher999
abinashmeher999 Jun 9, 2016 Contributor

Again this should be removed.

@abinashmeher999 abinashmeher999 commented on an outdated diff Jun 9, 2016
ext/symengine/ruby_function.c
@@ -37,3 +39,39 @@ IMPLEMENT_ONE_ARG_FUNC(gamma);
#undef IMPLEMENT_ONE_ARG_FUNC
+VALUE cfunction_functionsymbol_init(VALUE self, VALUE args)
+{
+ int argc = NUM2INT(rb_funcall(args, rb_intern("length"), 0, NULL));
+ printf("argc : %d\n", argc);
+ VALUE first = rb_ary_shift(args);
+ if( TYPE(first) != T_STRING ){
+ rb_raise(rb_eTypeError, "String expected as first argument");
+ }
+ char *name = StringValueCStr( first );
@abinashmeher999
abinashmeher999 Jun 9, 2016 Contributor

The spaces around first should be removed.

@abinashmeher999 abinashmeher999 commented on an outdated diff Jun 9, 2016
ext/symengine/ruby_function.c
@@ -37,3 +39,39 @@ IMPLEMENT_ONE_ARG_FUNC(gamma);
#undef IMPLEMENT_ONE_ARG_FUNC
+VALUE cfunction_functionsymbol_init(VALUE self, VALUE args)
+{
+ int argc = NUM2INT(rb_funcall(args, rb_intern("length"), 0, NULL));
@abinashmeher999
abinashmeher999 Jun 9, 2016 Contributor

Or you can use rb_ary_length.

@abinashmeher999 abinashmeher999 commented on an outdated diff Jun 9, 2016
ext/symengine/ruby_function.c
@@ -37,3 +39,39 @@ IMPLEMENT_ONE_ARG_FUNC(gamma);
#undef IMPLEMENT_ONE_ARG_FUNC
+VALUE cfunction_functionsymbol_init(VALUE self, VALUE args)
+{
+ int argc = NUM2INT(rb_funcall(args, rb_intern("length"), 0, NULL));
+ printf("argc : %d\n", argc);
+ VALUE first = rb_ary_shift(args);
+ if( TYPE(first) != T_STRING ){
+ rb_raise(rb_eTypeError, "String expected as first argument");
+ }
+ char *name = StringValueCStr( first );
+ char *c;
+
+ CVecBasic *cargs = vecbasic_new();
+
+ //basic_struct *cargs[argc];
@abinashmeher999
abinashmeher999 Jun 9, 2016 Contributor

This line should go too.

@abinashmeher999 abinashmeher999 and 1 other commented on an outdated diff Jun 9, 2016
ext/symengine/ruby_function.c
@@ -37,3 +39,39 @@ IMPLEMENT_ONE_ARG_FUNC(gamma);
#undef IMPLEMENT_ONE_ARG_FUNC
+VALUE cfunction_functionsymbol_init(VALUE self, VALUE args)
+{
+ int argc = NUM2INT(rb_funcall(args, rb_intern("length"), 0, NULL));
+ printf("argc : %d\n", argc);
+ VALUE first = rb_ary_shift(args);
+ if( TYPE(first) != T_STRING ){
+ rb_raise(rb_eTypeError, "String expected as first argument");
+ }
+ char *name = StringValueCStr( first );
+ char *c;
@abinashmeher999
abinashmeher999 Jun 9, 2016 Contributor

This is also not used anywhere I feel.

@rajithv
rajithv Jun 9, 2016 Contributor

@abinashmeher999 all those were used for debugging. I didn't notice that I've added them to the pushed commit. I'll remove them.

rajithv added some commits Jun 9, 2016
@rajithv rajithv Removed commented code and print statements. Added an error for no ar…
…guments
2dd2966
@rajithv rajithv Changed rb_ary_length to RARRAY_LEN 0e92c92
@rajithv rajithv Added Subs class e38cf80
@rajithv rajithv Added specs for FunctionSymbol
310b7bf
@rajithv rajithv SymEngine version changed
4c538ac
@rajithv rajithv changed the title from [WIP] Function symbol Ruby Wrappers to Function symbol Ruby Wrappers Jun 10, 2016
@isuruf isuruf commented on an outdated diff Jun 10, 2016
symengine_version.txt
@@ -1,2 +1,3 @@
-b91c64a3b82f15133f4a7df64749523bb0bed185
+1d80d81096b66c08ad3fe4e43ee586cd91b5ccf8
+
@isuruf
isuruf Jun 10, 2016 Member

No need of these extra lines

@isuruf isuruf commented on an outdated diff Jun 10, 2016
ext/symengine/ruby_function.c
@@ -37,3 +39,36 @@ IMPLEMENT_ONE_ARG_FUNC(gamma);
#undef IMPLEMENT_ONE_ARG_FUNC
+VALUE cfunction_functionsymbol_init(VALUE self, VALUE args)
+{
+ int argc = RARRAY_LEN(args);
+ if(argc == 0){
@isuruf
isuruf Jun 10, 2016 Member

if (argc == 0) {

@isuruf isuruf commented on an outdated diff Jun 10, 2016
ext/symengine/ruby_function.c
@@ -37,3 +39,36 @@ IMPLEMENT_ONE_ARG_FUNC(gamma);
#undef IMPLEMENT_ONE_ARG_FUNC
+VALUE cfunction_functionsymbol_init(VALUE self, VALUE args)
+{
+ int argc = RARRAY_LEN(args);
+ if(argc == 0){
+ rb_raise(rb_eTypeError, "Arguments Expected");
+ }
+
+ VALUE first = rb_ary_shift(args);
+ if( TYPE(first) != T_STRING ){
@isuruf
isuruf Jun 10, 2016 Member

if (TYPE(first) != T_STRING) {

@isuruf isuruf commented on an outdated diff Jun 10, 2016
ext/symengine/ruby_function.c
+ if(argc == 0){
+ rb_raise(rb_eTypeError, "Arguments Expected");
+ }
+
+ VALUE first = rb_ary_shift(args);
+ if( TYPE(first) != T_STRING ){
+ rb_raise(rb_eTypeError, "String expected as first argument");
+ }
+ char *name = StringValueCStr(first);
+
+ CVecBasic *cargs = vecbasic_new();
+
+ basic x;
+ basic_new_stack(x);
+ int i;
+ for(i = 0; i < argc-1; i++){
@isuruf
isuruf Jun 10, 2016 Member

Space after for and before {.

@isuruf
Member
isuruf commented Jun 11, 2016

Python wrappers have a nice syntax for creating FunctionSymbols.

func = Function('f')
Above would create an UndefFunction object which is a Python class, and
func(x, 2*y) would create a FunctionSymbol f(x, 2*y).

What do you think?

@rajithv rajithv Formatting Corrections
ceeb757
@rajithv
Contributor
rajithv commented Jun 11, 2016

@isuruf What are the advantages of doing it in two steps?

What about, haveing SymEngine::Function('f', x, 2*y) as an alias for SymEngine::FunctionSymbol.new(...) ?

@isuruf isuruf commented on an outdated diff Jun 12, 2016
spec/function_symbol_spec.rb
+ end
+
+ context 'with compound arguments' do
+ subject { SymEngine::FunctionSymbol.new('f', 2*x, y, SymEngine::sin(z)) }
+ it { is_expected.to be_a SymEngine::FunctionSymbol }
+ end
+ end
+
+ context '#diff' do
+ let(:fun) { (SymEngine::FunctionSymbol.new('f', 2*x, y, SymEngine::sin(z))) }
+ context 'by variable' do
+ subject { fun.diff(x)/2 }
+ it { is_expected.to be_a SymEngine::Subs }
+ end
+
+ context 'by constant' do
@isuruf
isuruf Jun 12, 2016 Member

Remove this. Differentiation should throw an error if the input is not a Symbol.

@isuruf isuruf commented on an outdated diff Jun 12, 2016
spec/function_symbol_spec.rb
+ subject { SymEngine::FunctionSymbol.new('f', x, y, z) }
+ it { is_expected.to be_a SymEngine::FunctionSymbol }
+ end
+
+ context 'with compound arguments' do
+ subject { SymEngine::FunctionSymbol.new('f', 2*x, y, SymEngine::sin(z)) }
+ it { is_expected.to be_a SymEngine::FunctionSymbol }
+ end
+ end
+
+ context '#diff' do
+ let(:fun) { (SymEngine::FunctionSymbol.new('f', 2*x, y, SymEngine::sin(z))) }
+ context 'by variable' do
+ subject { fun.diff(x)/2 }
+ it { is_expected.to be_a SymEngine::Subs }
+ end
@isuruf
isuruf Jun 12, 2016 Member

Above three lines have to be shifted to the left by 2 spaces

@isuruf
Member
isuruf commented Jun 12, 2016

Advantage is for easy typing when there are multiple function symbols made and also for readability

f = Function('f')
g = f(x, y) - 2 * f(y - x, x)

instead of

g = FunctionSymbol.new("f", x, y) - 2 * FunctionSymbol.new("f", y - x, x)
rajithv added some commits Jun 12, 2016
@rajithv rajithv Merge branch 'master' into function_symbol
6b32db9
@rajithv rajithv Fixing the function_symbol_spec.rb
91b414b
@rajithv rajithv UndefFunction
8932ada
@isuruf isuruf commented on an outdated diff Jun 13, 2016
lib/symengine.rb
@@ -29,6 +28,10 @@ def symbols ary_or_string, *params
end
end
end
+
+ def SymEngine.Function(n)
@isuruf
isuruf Jun 13, 2016 Member

You can move this inside class << self block and then you can remove SymEngine.

@rajithv rajithv Moving Function() inside SymEngine module's self class
8a298f0
@rajithv rajithv Spec for UndefFunction and it's call method
6c0fe5d
@isuruf isuruf merged commit a7b1d13 into symengine:master Jun 14, 2016

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment