ASKSAGE: Sage Q&A Forum - Latest question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 25 Dec 2019 13:07:33 -0600what is sagemath equivalent to atomic type in Maple?https://ask.sagemath.org/question/49179/what-is-sagemath-equivalent-to-atomic-type-in-maple/I am having hard time finding equivalent to atomic type in Maple to use in Sage for converting a function written in Maple to sagemath.
Here is [description](https://www.maplesoft.com/support/help/Maple/view.aspx?path=type/atomic) of what an atomic type is. Examples will make it more clear
type(1,'atomic');
true
type(1/2,'atomic');
true
type(.5,'atomic');
true
type(3+1/2*I,'atomic');
true
type(x+1/2*I,'atomic');
false
type("String",'atomic');
true
type(Name,'atomic');
true
type(a[b],'atomic');
true
type(a/b,'atomic');
false
In Mathematica, this is called [AtomQ](https://reference.wolfram.com/language/ref/AtomQ.html) "
yields True if expr is an expression which cannot be divided into subexpressions, and yields False otherwise."
Now, in sympy, the closest I found is `is_Atom` even though it is not exactly the same as Maple and Mathematica. In sagemath, the closest I found is `expression.is_symbol()` but this is also not exactly the same. Because I can't use it on variable of numeric type for example.
My question is, how does one check expression is atomic (in the above sense) in sagemath? I googled and not able to find such command. I am sure it is there somewhere since this basic command.
Thank you
--NasserNasserWed, 25 Dec 2019 00:41:47 -0600https://ask.sagemath.org/question/49179/How to check if function name is in a list?https://ask.sagemath.org/question/49185/how-to-check-if-function-name-is-in-a-list/sagemath 8.9
I am still not good in sagemath and learning it. I am trying to convert this code from Maple to sagemath, but I do not know why it is not working in sagemath.
Given a function, I need to find if it is a member of list of known functions. This is easier explained with an example.
Say the input is `gamma(-1,t)` and I need to find if the function, which is `gamma` in this case in member of a list of names of functions, such as `[erf,gamma,cos, etc...]`. I will show the Maple code and how I translated to sagemath. But in sagemath it gives false instead of true.
Maple:
restart;
expr:=gamma(-1,t);
op(0,expr); #extract the head, which is just the name of the function
gamma
member(op(0,expr),[erf,gamma]); #check if it is in the list
true
![image description](/upfiles/15773006766297719.png)
This is in sagemath
sage: var('t')
sage: from sage.all import *
sage: expr=gamma(-1,t)
sage: expr.operator()
gamma
sage: expr.operator() in [erf,gamma]
False
So I must be doing something wrong in sagemath. It might be due to type, because when I do
sage: type(expr.operator())
<class 'sage.functions.gamma.Function_gamma_inc'>
Do I need to import something to make it work? I think the name `gamma` inside the list is not taken as the same as the name `gamma` which results from `expr.operator()` and that is why it gives `False`. But do not know how to fix it.
The idea is to check if the function is among a list of known function names in a list. How to do this in sagemath?
Thanks
--NasserNasserWed, 25 Dec 2019 13:07:33 -0600https://ask.sagemath.org/question/49185/why sagemath says 1.0 is integer?https://ask.sagemath.org/question/49180/why-sagemath-says-10-is-integer/This is really strange. sagemath 8.9
Compare
sage: var('x')
x
sage: x=1
sage: x.is_integer()
True
sage: x=1.0
sage: x.is_integer()
True
In sympy
(base) >python
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy import *
>>> from sympy.abc import x
>>> x=1
>>> isinstance(x,int)
True
>>> x=1.0
>>> isinstance(x,int)
False
>>> isinstance(x,float)
True
Question is: Should sagemath return true for 1.0 being an integer?
NasserWed, 25 Dec 2019 01:01:43 -0600https://ask.sagemath.org/question/49180/Stop Sage from overriding Python number typeshttps://ask.sagemath.org/question/48442/stop-sage-from-overriding-python-number-types/ I'm trying to play around with machine learning inside of the SageMath environment, but since Sage implicitly casts ints, floats, etc. to Integers, RealNumbers, etc., I'm running into problems such as this one:
```
...
--> model.add(Dense(Integer(1), activation="softmax"))
...
ValueError: Can't convert Python sequence with mixed types to Tensor.
```
So far, I've gotten around this by explicitly casting, so my line of code now looks like this:
```
...
model.add(Dense(int(1), activation="softmax"))
...
```
but I would rather not do this every time I create a model. Is there a way to stop Sage from implicitly casting, and instead use the default Python types?eniagaterSun, 20 Oct 2019 11:37:06 -0500https://ask.sagemath.org/question/48442/Is there a way to temporarily turn off sage's type checking?https://ask.sagemath.org/question/47227/is-there-a-way-to-temporarily-turn-off-sages-type-checking/ I'm having some difficulties writing sage code to solve linear programming problems using "natural variables", by substituting for them the variables required for MixedIntegerLinearProgram.
The code for "minimize_desired" is what I'd like to do, but it fails during the substitution with the error message "TypeError: no canonical coercion from Linear functions over Real Double Field to Symbolic Ring". What I'd like is for sage to just do the substitution and ignore the lack of coercion.
The code for "minimize_works" works, but I got past the type checking by changing everything to strings and then back into a sage object, which feels like a terrible kludge. However this does illustrate that, despite sage's worries about coercion, everything ends up fine for MixedIntegerLinearProgram.
Is there a way to turn off sage's type checking and just do the substitution?
Thanks,
Mike
Note: I simplified the examples below by eliminating all the code that dealt with constraints.
def minimize_desired(objective):
p = MixedIntegerLinearProgram(maximization=false)
pvar = p.new_variable(real=True, nonnegative=True)
variables=set(objective.arguments())
translation={v:pvar[v] for v in variables}
objective=objective.subs(translation) # FAILS HERE
p.set_objective(objective)
print p.solve()
def minimize_works(objective):
p = MixedIntegerLinearProgram(maximization=false)
pvar = p.new_variable(real=True, nonnegative=True)
variables=set(str(v) for v in objective.arguments())
translation={v:pvar[v] for v in variables}
objective=sageobj(str(objective), translation)
p.set_objective(objective)
print p.solve()
var('x y')
minimize_works(x+y)
print
minimize_desired(x+y)
millermjSat, 20 Jul 2019 19:24:18 -0500https://ask.sagemath.org/question/47227/