Ask Your Question

heatkernel's profile - activity

2020-11-19 19:38:38 +0200 received badge  Famous Question (source)
2017-07-06 05:06:47 +0200 received badge  Notable Question (source)
2017-02-11 04:10:04 +0200 received badge  Famous Question (source)
2016-06-03 09:10:22 +0200 received badge  Popular Question (source)
2016-02-06 04:46:45 +0200 received badge  Notable Question (source)
2016-02-02 10:31:31 +0200 received badge  Nice Question (source)
2015-06-08 21:15:36 +0200 received badge  Notable Question (source)
2015-01-11 14:41:38 +0200 received badge  Popular Question (source)
2014-06-29 06:56:37 +0200 received badge  Notable Question (source)
2014-06-29 06:56:37 +0200 received badge  Popular Question (source)
2014-06-29 06:56:37 +0200 received badge  Famous Question (source)
2013-11-09 14:50:54 +0200 received badge  Popular Question (source)
2013-03-24 00:08:40 +0200 received badge  Taxonomist
2012-09-07 01:29:13 +0200 marked best answer Extract terms from a sum

Look at the operator() and operands() built-in methods of the Symbolic Expression class:

sage: F.operator()
<built-in function add>
sage: F.operands()
[A*B, C]
sage: F == F.operator()(*F.operands())
A*B + C == A*B + C
sage: bool(F == F.operator()(*F.operands()))
True
2012-09-05 00:05:14 +0200 asked a question Extract terms from a sum

Is there some way to programmatically extract the first, second, third, and so on terms from a sum of symbolic terms (or equivalently, turn such a sum into the list of its summands)? For example, after

sage: A,B,C = var('A'), var('B'), var('C') 
sage: F = A*B+C; F
A*B + C

Is there some method you can call on F to extract A*B (or C)? This would be useful especially for displaying sage in LaTeX via sageTeX when a formula that is the sum of 4 terms (say) runs over the margin and has to be split up somehow. I would also be interested in a workaround in sageTeX that would allow you to flexibly insert a linebreak in a formula under the circumstances that the formula generated by Sage got too long for a line.

2012-09-04 23:53:27 +0200 commented answer Some equations fail to solve even with to_poly_solve

thanks, that works.

2012-09-04 23:53:15 +0200 marked best answer Some equations fail to solve even with to_poly_solve

Maybe sympy can be helpful?

sage: from sympy import *            
sage: y,z=symbols('y z')             
sage: from sympy.solvers import solve
sage: solve(log(z)+y*z, z)           
[LambertW(y)/y]
2012-09-03 01:02:34 +0200 asked a question Some equations fail to solve even with to_poly_solve

In the following, I would expect to obtain [z == 1/y*lambda_w(y)]:

sage: z=var('z')
sage: y=var('y')
sage: solve(log(z)+3*z==0, z, to_poly_solve=True)
[z == 1/3*lambert_w(3)]
sage: solve(log(z)+y*z==0, z, to_poly_solve=True)
[z == -log(z)/y]

But Sage doesn't want to solve the equation for z. Perhaps, in view of the fact that it solves the equation when 3 is in place of z, it doesn't realize that y is supposed to represent a real number. I tried assume(y,'real') but that command did not help.

Does anyone know a workaround?

2012-06-16 20:21:32 +0200 marked best answer Substitution using Dictionary with Matrix as Value

Symbolic expressions do not support working with Sage matrices. There are different approaches to do "linear algebra" in this context though.

  • Indexed expressions

    This is GiNaC's solution. Our symbolics library pynac is based on GiNaC, so we just inherit this functionality. Although it hasn't been wrapped for easy use from Python.

    There is an experimental patch at #11576, but the GiNaC tutorial is the best reference.

  • SymbolicMatrixAlgebra as Nicolas Thiery implemented in this thread.

Any help to get these things in a polished state so they can be included in Sage is welcome.

2012-06-16 07:33:45 +0200 received badge  Student (source)
2012-06-15 01:15:33 +0200 received badge  Supporter (source)
2012-06-14 17:58:48 +0200 asked a question Substitution using Dictionary with Matrix as Value

As a newcomer to SAGE, trying to use it to do symbolic linear algebra, I am wondering why substitution of a variable using a dictionary doesn't work in this case:

sage: aMatrix = matrix(SR,1,1)
sage: var('aVariable')
aVariable
sage: aDict = {}
sage: aDict[aVariable] = aMatrix
sage: aDict[aVariable]
[0]

but:

sage: aVariable.subs(aDict)
....
/Applications/sage/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:16025)()

/Applications/sage/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.coerce_in (sage/symbolic/expression.cpp:11265)()

/Applications/sage/local/lib/python2.7/site-packages/sage/structure/parent_old.so in sage.structure.parent_old.Parent._coerce_ (sage/structure/parent_old.c:3369)()

/Applications/sage/local/lib/python2.7/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.coerce (sage/structure/parent.c:8912)()

TypeError: no canonical coercion from Full MatrixSpace of 1 by 1 dense matrices over Symbolic Ring to Symbolic Ring

Functionality to substitute matrices for variables seems to be indispensable to doing symbolic linear algebra, so I am sure there is a proper way to do this.

2012-06-14 01:35:10 +0200 commented answer Variable assignment in list

Thanks, DSM, I think the point you are making, if I may try to rephrase it, is that in SAGE, if you want to implement something close to (or the same as(?)) mathematical equality of a variable "A" with a more complicated expression "f(x)", you had better do it through a dictionary (which is the same as hashtable, right?). If you make the definition of "A" as "f(x)" with "=" instead of a dictionary, this just binds the local name "A" to "f(x)" and this this definition of "A" won't pass through to other expressions defined in terms of "A", such as the "expr" in your example. Anyway, I will try to work with dictionaries instead of "="'s for assignment and post other questions as they arise.

2012-06-14 01:29:18 +0200 marked best answer Variable assignment in list

Unlike many languages, Python doesn't really have variables: only objects and names. (And namespaces, I guess.) For example,

sage: a = [1,2]
sage: a
[1, 2]

doesn't declare a variable a and set it to the list [1,2]. We sometimes talk like this, because it's often close enough for government work, but it's not really accurate. What happens is that a list object is created and the name a is defined to refer to it (a process often called 'binding'). We can make other names refer to the same object:

sage: b = a
sage: b
[1, 2]
sage: b[0] = 17
sage: b
[17, 2]
sage: a
[17, 2]
sage: a is b
True

So in the case of a loop like this:

sage: a = [1,2]
sage: for item in a:
....:     print item
....:     item = 4
....:     
1
2
sage: a                                           
[1, 2]

a doesn't change because item = 4 isn't the equivalent of (*item) = 4. item isn't a pointer, it's just a name. item = 4 means "take the name 'item' and bind it to the integer object 4". From Python's perspective, why should the a list care that you've decided to start using "item" to refer to the number 4?

If you want to affect a, you have to grasp one of the elements, say by getting an index:

sage: for i, item in enumerate(a):
....:     print i, item
....:     a[i] = item * 10
....:     
0 1
1 2
sage: a
[10, 20]

Note that you can modify what item refers to directly, if the object referred to by item is mutable. For example:

sage: a = [[1,2], [3,4]]
sage: for item in a:
....:     print item
....:     item += [99]
....:     
[1, 2]
[3, 4]
sage: a
[[1, 2, 99], [3, 4, 99]]

and so on.

UPDATE:

Here's an example of how we would handle things like this in Python via a dictionary, without dynamically creating local variables:

sage: set_random_seed(3)
sage: vv = [var("x%d" % i) for i in [0..5]]
sage: vv
[x0, x1, x2, x3, x4, x5]
sage: vals = [random() for v in vv]
sage: vals
[0.43088005103813976, 0.8358366908518607, 0.8023253357960184, 0.4891524489091327, 0.7822037482240924, 0.7607638369816612]
sage: vdict = dict(zip(vv, vals))
sage: vdict
{x5: 0.7607638369816612, x3: 0.4891524489091327, x2: 0.8023253357960184, x0: 0.43088005103813976, x4: 0.7822037482240924, x1: 0.8358366908518607}
sage: some_expr = sum(random() * v**i for v in vv)
sage: some_expr
0.6925472846314387*x0^5 + 0.19968616753011514*x1^5 + 0.9416651257671081*x2^5 + 0.9556540407131406*x3^5 + 0.7656492351180494*x4^5 + 0.584583199716608*x5^5
sage: some_expr.subs(vdict)
0.804750970712524

Regarding the comments:

I'm afraid I don't understand what is meant by 'manually referring to variable names'. As opposed to what other methods of referring to them?

When the variables (or names) are kept in a dictionary I can do all sorts of nice things to them as a collection. Not so much if I rebind the local names to something else ... (more)

2012-06-14 01:29:18 +0200 received badge  Scholar (source)
2012-06-13 00:38:15 +0200 commented answer Variable assignment in list

If the program/function has n (the length of the list) as an integer parameter, so that there are n variables o have values assigned to them, and the expressions you want to assign also depend on n (so a previous part of the program produces both the list of assignee variables and assignment expressions), you can't hard code in the assignment. I'm afraid I don't understand what is meant by 'manually referring to variable names'. As opposed to what other methods of referring to them? Not trying to be tendentious, I am just too new to SAGE to understand what you mean here. A dictionary might work as a substitute for assignment, but I still prefer the variable to be assigned the values.

2012-06-13 00:27:25 +0200 commented answer Variable assignment in list

Thanks for your very clear and detailed answer. But I realized I wasn't unambiguous about what I wanted to do. I actually wanted to assign the value 'B' to every variable in the list, not change the contents of the list, which I am just using as a vehicle for doing the assignment. I have added a clarification in the original question above.

2012-06-12 22:24:16 +0200 edited question Variable assignment in list

This is a very basic question because I am a new user of SAGE with a background mostly in Java and C++. But in the following session:

sage: var('A')
A
sage: var('B')
B
sage: for item in alist:
    item = B
    print item
....:     
B
sage: for item in alist:
    print item
....:     
A

I am not sure why the value of the item in the list is printed as 'A' instead of 'B'. During the previous loop it seems like it assigned the value 'B' and printed out that the item was 'B. This may be an issue of the line 'item = B' returning a transient copy of the item, instead of a reference to the item. Can someone explain what is going on in programming language terms and tell me how to fix this so that the last line would print 'B'?

EDIT: I should explain that what I want to do is not just change the items in the list, so I don't want to just make alist conatin the variable 'B'. I actually want to assign the value 'B' to the variable 'A', so that when I type 'A' into SAGE, the output is 'B'. This can be accomplished in this example with the one command:

sage: A = B

but the point is that if I have two lists

  1. listVariables, e.g. [Y_1,...Y_n]
  2. listExpressions, e.g. [X^2, X^3, Z^4, ..., XZ]

of unknown length and contents, except that the two lists have the same number of elements, I want to assign the value contained in listExpressions[i] to the variable in listVariables[i] by looping over the lists.

2012-06-12 22:24:16 +0200 received badge  Editor (source)