Re: [sage-devel] Re: Why SR('expression') fail on some but not others?

2023-05-05 Thread sumaiya qureshi
Hey All!

Please let me know I am included within the accepted organization or not?

According to Karachi, Pakistan timezone last night was my GSoC'23 result.

Please guide me through the procedure how to check my GSoC'23 result.

Name :- Sumaiya Qureshi (Karachi, Pakistan)

I opted for Improvements to mathematics interaction with the desired
organization "Oppia" within Web category.

Let me know if you all sage developers want further information regarding
myself to search my result.

Regards,
Sumaiya.

On Wed, Apr 26, 2023, 9:49 PM William Stein  wrote:

> I put a few remarks related to this in a notebook:
>
> https://cocalc.com/wstein/support/SR-and-strings
>
> Basically, converting to from strings via str hardly works anywhere in
> Sage and that is by design, following the lead of Magma instead of
> Pari.  Instead Pickle is the thing that mostly works for that mapping,
> but is ugly. Then lament no json...
>
> William
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-devel+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sage-devel/CACLE5GB%2BQh3cpCesFtPMs18E3i4AAFvYm3cz3Y7hrsWo45XGWQ%40mail.gmail.com
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/CAOgrLb0BSyWp%2Bxu6at59KUBkQCXYO8QuZHkvZY80wv3s5DgB0w%40mail.gmail.com.


Re: [sage-devel] Re: Why SR('expression') fail on some but not others?

2023-04-26 Thread William Stein
I put a few remarks related to this in a notebook:

https://cocalc.com/wstein/support/SR-and-strings

Basically, converting to from strings via str hardly works anywhere in
Sage and that is by design, following the lead of Magma instead of
Pari.  Instead Pickle is the thing that mostly works for that mapping,
but is ugly. Then lament no json...

William

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/CACLE5GB%2BQh3cpCesFtPMs18E3i4AAFvYm3cz3Y7hrsWo45XGWQ%40mail.gmail.com.


[sage-devel] Re: Why SR('expression') fail on some but not others?

2023-04-26 Thread John H Palmieri
While

sage_eval('sin(x)')

does not work,

sage_eval('sin(x)', {'x': x})

does work. sage_eval needs to know the context (which variables have been 
defined, etc.) in which to evaluate. I am not an expert, but

sage_eval('sin(x)', locals=locals())

might work pretty reliably, without having to specify each previously 
defined variable by hand.


-- John


On Wednesday, April 26, 2023 at 12:05:03 AM UTC-7 Nasser M. Abbasi wrote:

> I can't use  SR(sage_eval(' expression'))  Now all my integral are 
> failing. 
> Here is an example
>
> >sage
> │ SageMath version 9.8, Release Date: 2023-02-11 │
> │ Using Python 3.11.1. Type "help()" for help.   │
>
> sage: var('x')
> x
>
> sage: SR(sage_eval('sin(x)'))
> ---
> NameError Traceback (most recent call last)
> Cell In [3], line 1
> > 1 SR(sage_eval('sin(x)'))
>
> File ~/TMP/sage-9.8/src/sage/misc/sage_eval.py:198, in sage_eval(source, 
> locals, cmds, preparse)
> 196 return locals['_sage_eval_returnval_']
> 197 else:
> --> 198 return eval(source, sage.all.__dict__, locals)
>
> File :1
>
> NameError: name 'x' is not defined
>
>
> But 
>
> sage: SR('sin(x)')
> sin(x)
>
> Works. 
>
> So adding  sage_eval() did not work.
>
> --Nasser
>
>
> On Wednesday, April 26, 2023 at 1:33:26 AM UTC-5 Nils Bruin wrote:
>
>> I think the problem is that the SR exression parses does not know about 
>> python's "(a,b)" tuple notation. If you replace the round brackets with 
>> square brackets, it does seem to work; at least for the example you give:
>>
>> sage: SR('hypergeometric([3/2,], [5/2, 3], -1/4*3^2)')
>>  hypergeometric((3/2,), (5/2, 3), -1/4*3^2)
>>
>> You could use the python parser instead, via something like:
>>
>>  SR(sage_eval(' hypergeometric((3/2,), (5/2, 3), -1/4*3^2)'))
>>
>> but note that SR will happily define symbols it doesn't know, whereas 
>> sage_eval will complain:
>>
>> sage: SR("my_function(var1,var2)")
>> my_function(var1, var2)
>> sage: SR(sage_eval("my_function(var1,var2)"))
>> NameError: name 'my_function' is not defined
>>
>>
>>
>> On Tuesday, 25 April 2023 at 23:08:02 UTC-7 Nasser M. Abbasi wrote:
>>
>>> I read integrals from a file. They all are stored as strings.
>>>
>>> Then use SR('expression') inside sagemath to convert them to sagemath 
>>> symbolic  expression before calling integrate.
>>>
>>> Some give parsing error. 
>>>
>>> Is using SR('expression') not the correct way to convert string to a 
>>> symbolic expression?
>>>
>>> I am using 9.8 on Linux. Here is an example
>>>
>>> >sage
>>> │ SageMath version 9.8, Release Date: 2023-02-11 │
>>> │ Using Python 3.11.1. Type "help()" for help.   │
>>>
>>> sage: hypergeometric((3/2,), (5/2, 3), -1/4*3^2)
>>> hypergeometric((3/2,), (5/2, 3), -9/4)
>>>
>>> You see, there is no error. But now if put the expression inside string 
>>> and use SR, it gives error:
>>>
>>>
>>> sage: SR('hypergeometric((3/2,), (5/2, 3), -1/4*3^2)')
>>>
>>> ---
>>> SyntaxError   Traceback (most recent call 
>>> last)
>>> File ~/TMP/sage-9.8/src/sage/symbolic/expression.pyx:13706, in 
>>> sage.symbolic.expression.new_Expression()
>>>   13705 from sage.calculus.calculus import 
>>> symbolic_expression_from_string
>>> > 13706 return parent(symbolic_expression_from_string(x))
>>>   13707 except SyntaxError as err:
>>>
>>> File ~/TMP/sage-9.8/src/sage/calculus/calculus.py:2578, in 
>>> symbolic_expression_from_string(s, syms, accept_sequence, parser)
>>>2576 parser._callable_constructor().set_names({k[0]: v for k, v in 
>>> syms.items()
>>>2577   if _is_function(v)})
>>> -> 2578 return parse_func(s)
>>>
>>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:556, in 
>>> sage.misc.parser.Parser.parse_expression()
>>> 555 
>>> --> 556 cpdef parse_expression(self, s):
>>> 557 """
>>>
>>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:568, in 
>>> sage.misc.parser.Parser.parse_expression()
>>> 567 cdef Tokenizer tokens = Tokenizer(s)
>>> --> 568 expr = self.p_expr(tokens)
>>> 569 if tokens.next() != EOS:
>>>
>>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:792, in 
>>> sage.misc.parser.Parser.p_expr()
>>> 791 cdef int op
>>> --> 792 operand1 = self.p_term(tokens)
>>> 793 op = tokens.next()
>>>
>>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:826, in 
>>> sage.misc.parser.Parser.p_term()
>>> 825 cdef int op
>>> --> 826 operand1 = self.p_factor(tokens)
>>> 827 op = tokens.next()
>>>
>>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:869, in 
>>> sage.misc.parser.Parser.p_factor()
>>> 868 tokens.backtrack()
>>> --> 869 return self.p_power(tokens)
>>> 870 
>>>
>>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:897, in 
>>> 

[sage-devel] Re: Why SR('expression') fail on some but not others?

2023-04-26 Thread 'Nasser M. Abbasi' via sage-devel
I can't use  SR(sage_eval(' expression'))  Now all my integral are failing. 
Here is an example

>sage
│ SageMath version 9.8, Release Date: 2023-02-11 │
│ Using Python 3.11.1. Type "help()" for help.   │

sage: var('x')
x

sage: SR(sage_eval('sin(x)'))
---
NameError Traceback (most recent call last)
Cell In [3], line 1
> 1 SR(sage_eval('sin(x)'))

File ~/TMP/sage-9.8/src/sage/misc/sage_eval.py:198, in sage_eval(source, 
locals, cmds, preparse)
196 return locals['_sage_eval_returnval_']
197 else:
--> 198 return eval(source, sage.all.__dict__, locals)

File :1

NameError: name 'x' is not defined


But 

sage: SR('sin(x)')
sin(x)

Works. 

So adding  sage_eval() did not work.

--Nasser


On Wednesday, April 26, 2023 at 1:33:26 AM UTC-5 Nils Bruin wrote:

> I think the problem is that the SR exression parses does not know about 
> python's "(a,b)" tuple notation. If you replace the round brackets with 
> square brackets, it does seem to work; at least for the example you give:
>
> sage: SR('hypergeometric([3/2,], [5/2, 3], -1/4*3^2)')
>  hypergeometric((3/2,), (5/2, 3), -1/4*3^2)
>
> You could use the python parser instead, via something like:
>
>  SR(sage_eval(' hypergeometric((3/2,), (5/2, 3), -1/4*3^2)'))
>
> but note that SR will happily define symbols it doesn't know, whereas 
> sage_eval will complain:
>
> sage: SR("my_function(var1,var2)")
> my_function(var1, var2)
> sage: SR(sage_eval("my_function(var1,var2)"))
> NameError: name 'my_function' is not defined
>
>
>
> On Tuesday, 25 April 2023 at 23:08:02 UTC-7 Nasser M. Abbasi wrote:
>
>> I read integrals from a file. They all are stored as strings.
>>
>> Then use SR('expression') inside sagemath to convert them to sagemath 
>> symbolic  expression before calling integrate.
>>
>> Some give parsing error. 
>>
>> Is using SR('expression') not the correct way to convert string to a 
>> symbolic expression?
>>
>> I am using 9.8 on Linux. Here is an example
>>
>> >sage
>> │ SageMath version 9.8, Release Date: 2023-02-11 │
>> │ Using Python 3.11.1. Type "help()" for help.   │
>>
>> sage: hypergeometric((3/2,), (5/2, 3), -1/4*3^2)
>> hypergeometric((3/2,), (5/2, 3), -9/4)
>>
>> You see, there is no error. But now if put the expression inside string 
>> and use SR, it gives error:
>>
>>
>> sage: SR('hypergeometric((3/2,), (5/2, 3), -1/4*3^2)')
>>
>> ---
>> SyntaxError   Traceback (most recent call 
>> last)
>> File ~/TMP/sage-9.8/src/sage/symbolic/expression.pyx:13706, in 
>> sage.symbolic.expression.new_Expression()
>>   13705 from sage.calculus.calculus import 
>> symbolic_expression_from_string
>> > 13706 return parent(symbolic_expression_from_string(x))
>>   13707 except SyntaxError as err:
>>
>> File ~/TMP/sage-9.8/src/sage/calculus/calculus.py:2578, in 
>> symbolic_expression_from_string(s, syms, accept_sequence, parser)
>>2576 parser._callable_constructor().set_names({k[0]: v for k, v in 
>> syms.items()
>>2577   if _is_function(v)})
>> -> 2578 return parse_func(s)
>>
>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:556, in 
>> sage.misc.parser.Parser.parse_expression()
>> 555 
>> --> 556 cpdef parse_expression(self, s):
>> 557 """
>>
>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:568, in 
>> sage.misc.parser.Parser.parse_expression()
>> 567 cdef Tokenizer tokens = Tokenizer(s)
>> --> 568 expr = self.p_expr(tokens)
>> 569 if tokens.next() != EOS:
>>
>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:792, in 
>> sage.misc.parser.Parser.p_expr()
>> 791 cdef int op
>> --> 792 operand1 = self.p_term(tokens)
>> 793 op = tokens.next()
>>
>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:826, in 
>> sage.misc.parser.Parser.p_term()
>> 825 cdef int op
>> --> 826 operand1 = self.p_factor(tokens)
>> 827 op = tokens.next()
>>
>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:869, in 
>> sage.misc.parser.Parser.p_factor()
>> 868 tokens.backtrack()
>> --> 869 return self.p_power(tokens)
>> 870 
>>
>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:897, in 
>> sage.misc.parser.Parser.p_power()
>> 896 """
>> --> 897 operand1 = self.p_atom(tokens)
>> 898 cdef int token = tokens.next()
>>
>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:952, in 
>> sage.misc.parser.Parser.p_atom()
>> 951 func = self.callable_constructor(name)
>> --> 952 args, kwds = self.p_args(tokens)
>> 953 token = tokens.next()
>>
>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:989, in 
>> sage.misc.parser.Parser.p_args()
>> 988 while token == c',':
>> --> 989 arg = self.p_arg(tokens)
>> 990 if isinstance(arg, tuple):
>>
>> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:1039, 

[sage-devel] Re: Why SR('expression') fail on some but not others?

2023-04-26 Thread Nils Bruin
I think the problem is that the SR exression parses does not know about 
python's "(a,b)" tuple notation. If you replace the round brackets with 
square brackets, it does seem to work; at least for the example you give:

sage: SR('hypergeometric([3/2,], [5/2, 3], -1/4*3^2)')
 hypergeometric((3/2,), (5/2, 3), -1/4*3^2)

You could use the python parser instead, via something like:

 SR(sage_eval(' hypergeometric((3/2,), (5/2, 3), -1/4*3^2)'))

but note that SR will happily define symbols it doesn't know, whereas 
sage_eval will complain:

sage: SR("my_function(var1,var2)")
my_function(var1, var2)
sage: SR(sage_eval("my_function(var1,var2)"))
NameError: name 'my_function' is not defined



On Tuesday, 25 April 2023 at 23:08:02 UTC-7 Nasser M. Abbasi wrote:

> I read integrals from a file. They all are stored as strings.
>
> Then use SR('expression') inside sagemath to convert them to sagemath 
> symbolic  expression before calling integrate.
>
> Some give parsing error. 
>
> Is using SR('expression') not the correct way to convert string to a 
> symbolic expression?
>
> I am using 9.8 on Linux. Here is an example
>
> >sage
> │ SageMath version 9.8, Release Date: 2023-02-11 │
> │ Using Python 3.11.1. Type "help()" for help.   │
>
> sage: hypergeometric((3/2,), (5/2, 3), -1/4*3^2)
> hypergeometric((3/2,), (5/2, 3), -9/4)
>
> You see, there is no error. But now if put the expression inside string 
> and use SR, it gives error:
>
>
> sage: SR('hypergeometric((3/2,), (5/2, 3), -1/4*3^2)')
> ---
> SyntaxError   Traceback (most recent call last)
> File ~/TMP/sage-9.8/src/sage/symbolic/expression.pyx:13706, in 
> sage.symbolic.expression.new_Expression()
>   13705 from sage.calculus.calculus import 
> symbolic_expression_from_string
> > 13706 return parent(symbolic_expression_from_string(x))
>   13707 except SyntaxError as err:
>
> File ~/TMP/sage-9.8/src/sage/calculus/calculus.py:2578, in 
> symbolic_expression_from_string(s, syms, accept_sequence, parser)
>2576 parser._callable_constructor().set_names({k[0]: v for k, v in 
> syms.items()
>2577   if _is_function(v)})
> -> 2578 return parse_func(s)
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:556, in 
> sage.misc.parser.Parser.parse_expression()
> 555 
> --> 556 cpdef parse_expression(self, s):
> 557 """
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:568, in 
> sage.misc.parser.Parser.parse_expression()
> 567 cdef Tokenizer tokens = Tokenizer(s)
> --> 568 expr = self.p_expr(tokens)
> 569 if tokens.next() != EOS:
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:792, in 
> sage.misc.parser.Parser.p_expr()
> 791 cdef int op
> --> 792 operand1 = self.p_term(tokens)
> 793 op = tokens.next()
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:826, in 
> sage.misc.parser.Parser.p_term()
> 825 cdef int op
> --> 826 operand1 = self.p_factor(tokens)
> 827 op = tokens.next()
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:869, in 
> sage.misc.parser.Parser.p_factor()
> 868 tokens.backtrack()
> --> 869 return self.p_power(tokens)
> 870 
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:897, in 
> sage.misc.parser.Parser.p_power()
> 896 """
> --> 897 operand1 = self.p_atom(tokens)
> 898 cdef int token = tokens.next()
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:952, in 
> sage.misc.parser.Parser.p_atom()
> 951 func = self.callable_constructor(name)
> --> 952 args, kwds = self.p_args(tokens)
> 953 token = tokens.next()
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:989, in 
> sage.misc.parser.Parser.p_args()
> 988 while token == c',':
> --> 989 arg = self.p_arg(tokens)
> 990 if isinstance(arg, tuple):
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:1039, in 
> sage.misc.parser.Parser.p_arg()
>1038 tokens.backtrack()
> -> 1039 return self.p_expr(tokens)
>1040 
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:792, in 
> sage.misc.parser.Parser.p_expr()
> 791 cdef int op
> --> 792 operand1 = self.p_term(tokens)
> 793 op = tokens.next()
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:826, in 
> sage.misc.parser.Parser.p_term()
> 825 cdef int op
> --> 826 operand1 = self.p_factor(tokens)
> 827 op = tokens.next()
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:869, in 
> sage.misc.parser.Parser.p_factor()
> 868 tokens.backtrack()
> --> 869 return self.p_power(tokens)
> 870 
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:897, in 
> sage.misc.parser.Parser.p_power()
> 896 """
> --> 897 operand1 = self.p_atom(tokens)
> 898 cdef int token = tokens.next()
>
> File ~/TMP/sage-9.8/src/sage/misc/parser.pyx:964, in 
> sage.misc.parser.Parser.p_atom()
> 963 if token != c')':
> --> 964 self.parse_error(tokens, "Mismatched parentheses")
> 965 return expr
>
>