ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 25 Feb 2021 20:00:22 +0100Evaluate constants in symbolic expression?https://ask.sagemath.org/question/55911/evaluate-constants-in-symbolic-expression/ Hello. If I have a symbolic expression where all of the variables are known, I can use `numerical_approx` to evaluate it:
var('a b')
x = a / b
x(a=1, b=3)
> 1/3
x(a=1, b=3).numerical_approx()
> 0.333333333333333
However, if there is an unknown variable, `numerical_approx` fails:
var('a b c')
x = a / b * c
x(a=1, b=3)
> 1/3*c
x(a=1, b=3).numerical_approx()
> TypeError: cannot evaluate symbolic expression numerically
Is there a way to approximate the values of all constants / coefficients in a symbolic expression? Essentially, something like
var('a b c')
x = a / b * c
x(a=1, b=3).numerical_approx_constants()
> 0.33333333333*c
Thank you in advance.MaybE_TreeThu, 25 Feb 2021 20:00:22 +0100https://ask.sagemath.org/question/55911/Memory increasing when using constant pihttps://ask.sagemath.org/question/48763/memory-increasing-when-using-constant-pi/ Hello everybody,
I have some memory problems when executing the following for cycle in SageMath (version 8.9 on Linux Mint 19.2):
sage: precision = 165
sage: FFF = RealField(prec = precision)
sage: MAX = 32
sage: gpi = FFF.pi()
sage: trange = [2..555]
....: for t in trange:
....: d = t*[exp (2*gpi*I*i/MAX) for i in range(MAX)]
....: print "memory usage: " + str(get_memory_usage())
which outputs
memory usage: 2283.20703125
memory usage: 2283.20703125
memory usage: 2283.20703125
...
memory usage: 2292.2421875
memory usage: 2292.4921875
memory usage: 2292.4921875
memory usage: 2292.4921875
I found that the situation improves if I remove the gpi variable from the for cycle, obtaining
memory usage: 2274.86328125
memory usage: 2274.86328125
memory usage: 2274.86328125
...
memory usage: 2275.12109375
memory usage: 2275.12109375
Is there some way to avoid this increment in memory?
Thank you in advance
fabio_fcFri, 15 Nov 2019 18:55:10 +0100https://ask.sagemath.org/question/48763/Protect built-in variables?https://ask.sagemath.org/question/27020/protect-built-in-variables/ Is there a way to protect built-in variables? This means that I get a warning if I use code like this:
pi = 5
var('I')OderynThu, 04 Jun 2015 05:26:27 +0200https://ask.sagemath.org/question/27020/Groebner basis computation with symbolic constantshttps://ask.sagemath.org/question/26748/groebner-basis-computation-with-symbolic-constants/Hello! If I have a system of polynomials in $CC[x, y, z]$ or any other field, is there a way to create constants that are in that field in a way that makes Groebner basis computation still work? For example, if I want to compute the Groebner basis for the ideal generated by
y^2 + z - c1
x*y^2 - c2 - 2
Is there a way to indicate that the $c1$ and $c2$ are in $CC$ or whatever field I'm in? I've figured out how to get them to not be indeterminates (over the symbolic ring),
Ideal (y^2 + z - c1, x*y^2 - c2 - 2) of Multivariate Polynomial Ring in x, y, z over Symbolic Ring
but then the polynomials containing them don't have division.
AttributeError: 'MPolynomialRing_polydict_with_category' object has no attribute 'monomial_divides'
Thank you!jooyousWed, 06 May 2015 21:56:22 +0200https://ask.sagemath.org/question/26748/Numerical values VS symbolic values ?https://ask.sagemath.org/question/23678/numerical-values-vs-symbolic-values/ This question might be related with [link text](http://ask.sagemath.org/question/8588/pi-and-e-not-evaluated-when-i-use-my-own-classes/?comment=23671#comment-23671)
I have to perform some numerical calculation using constants like pi and e. What happens is that computing a simple expression of pi is evaluated numerically (eg, cos(pi) returns -1) but when I use a random function (eg, random()*pi), I have a symbolic expression like "0.123456789*pi". In a for loop with this expression I obtain at the end something like "0.123456789*pi + 0.987654321*pi + ..." and so on.
My question aims to clarify the way to use symbolic expression (SE) and/or numerical values (NV) within a code (either in sage shell or script file). I think we have different cases to think about :
1. I want to use only NV in my code, how can I specify once for all that constants I will use will be evaluated numerically ?
2. I want to use only SE in my code, this one seems straightforward as Sage uses a preparser structure with symbolic expression.
3. I want to use both in my code, a function using constants need to return NV but also SE. Of course calculation will use NV from this function and analysis will use SE (eg, derivation, integration, series expansion, etc...).
I hope this thread will be useful. I think I know how to use case 1, for example with NV of pi as PI=RDF.pi() or PI=pi.n(). In case of random()*PI, we have indeed a numerical result, as wanted.
Case 3 is more interesting, I remember having a lots of problem with python.sympy with SE and NV. I struggled to use SE for analysis then trying to obtain NV. I'd like to see what you think about this, Sage seems more powerful than sympy about that. I read documentation but maybe I missed something. I'm not working on this case for now (so no code example...) but if needed for clarity I can dig one of my old sympy code.
(sorry for my english, it is sometimes "random" )
bigdukeSun, 03 Aug 2014 13:05:57 +0200https://ask.sagemath.org/question/23678/Difference Between Constants and Variableshttps://ask.sagemath.org/question/10583/difference-between-constants-and-variables/Hello all. I'm working with variational calculus in sage and working with
complicate expression `F` of a lot of variables declared with `var`.
I used `var` to declare both variables and constants. My problem is that I have
to know the exact number of variables which some generic expression depends. By
this reason the member function `variables` of an expression does not work, i.e.,
it returs both variables and constants because them was declared with `var` function.
I'm wondering if there is some type of variable in sage that can act as a variable to construct an expression and don't be returned by the `variables` member function.Rafael RojasTue, 08 Oct 2013 12:33:02 +0200https://ask.sagemath.org/question/10583/Write function for use by both mpmath and sage typeshttps://ask.sagemath.org/question/9489/write-function-for-use-by-both-mpmath-and-sage-types/I've got the following function:
def area2(d, r):
x = (sqrt(2*r^2 - d^2) - d)/2
a2 = 2*arccos((d + x)/r)
a2 = (a2 - sin(a2))/2*r^2
return ((2*x)^2 + 4*a2) - d.parent().pi()/9
I'd like to use this function with various argument typs, among them `RealField`, `RealIntervalField` and the `mpf` type from `mpmath`. The latter appears to be a requirement for using `mpmath.findroot`, as suggested by [this answer](http://ask.sagemath.org/question/1934/find-roots-with-arbitrary-precision?answer=2812#2812).
However, the code currently fails to work with `mpf`, as these numbers don't interact nicely with sage constants:
sage: area2(mpmath.mpf(4.44), mpmath.mpf(4.74))
[ ]
a2 = _sage_const_2 *arccos((d + x)/r)
[ ]
TypeError: cannot coerce arguments: no canonical coercion from <type 'sage.libs.mpmath.ext_main.mpf'> to Symbolic Ring
So my question is this: is there a way to write this function so that it will allow operation on all the mentioned number types, without case distinctions to cater for the different types, and without loosing precision to casts?
If this is not possible, do you know of any existing bug report asking for such a feature, should I open a new ticket, or is there any reason why such an interoperability is not technically possible?MvGWed, 31 Oct 2012 13:03:05 +0100https://ask.sagemath.org/question/9489/how to define a constant in sage?https://ask.sagemath.org/question/8805/how-to-define-a-constant-in-sage/Hello, I just started using sage. I am having some difficulties in declaring constants. I am trying to calculate derivatives of functions but I don't want to put values for the constant but rather define constants in the function. Somehow I am not able to find any literature on that.
I would be really grateful if someone can help me with this issue.
Cheers,
SanyogSanyogMon, 19 Mar 2012 17:51:49 +0100https://ask.sagemath.org/question/8805/integrating with unspecified constantshttps://ask.sagemath.org/question/8652/integrating-with-unspecified-constants/in the online sage I typed:
var('x,a,b')
integrate(1/sqrt(b+a/x), x)
And it answered:
Traceback (click to the left of this block for traceback)
...
Is b positive or negative?
Of course b is positive but there is no way to answer it's question or to predefine b as positive, so far as I know.
so I used b^2 and got a question "Is b > 0?". Well yes b>0. So is a.
a and b are part of a physics problem. They change with the initial conditions.
This is my first day with Sage. I have no idea what trace back is good for.
I don't know how to use this thing so send a copy of the answer to me at august04@frontier.com.
WVPhysicistSat, 21 Jan 2012 15:49:12 +0100https://ask.sagemath.org/question/8652/Physics constants in sage.https://ask.sagemath.org/question/8082/physics-constants-in-sage/Hi,
Are Physics constants like h (planks constant) , k (Boltzmann constant) etc.. available in sage?
If yes, how can i use them? When i searched I found only math constants Pi, e etc..
-Thanks
indiajoeindiajoeTue, 19 Apr 2011 09:00:14 +0200https://ask.sagemath.org/question/8082/Structure constants for unitary groupshttps://ask.sagemath.org/question/7661/structure-constants-for-unitary-groups/I want to define a generalized cross product in sage such the a_i=f^{ijk} b_j c_k, where f^{ijk} are the structure constants of the SU(3) group. Are the structure constants for unitary group predefined is sage. If not what is the best way to define such a generalized cross product?
Thanks in advance.
Edit:
Sorry for not being clear about the question. As Mitesh rightly pointed out I am trying to do a High energy calculation. I have two eight dimensional vectors (say b and c). I want to define a generalized product of these two vectors as described in the original post. Here f are structure constants of SU(3) Lie algebra.
When I try the series of commands suggested by niles
gap> e6 := SimpleLieAlgebra("E",6,Rationals);
gap> StructureConstantsTable(Basis(e6));
I get a error in sage but it works if I open gap in a terminal. I think I can manage by copy pasting the results in sage.
Thanks a lot again for the help
ShashankWed, 01 Sep 2010 20:09:04 +0200https://ask.sagemath.org/question/7661/