ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 24 Apr 2017 09:30:12 -0500How to create a Matrix with real numbers?http://ask.sagemath.org/question/37388/how-to-create-a-matrix-with-real-numbers/ I try to create a matrix with real number. When I try to create one, I get a TypeError message
Example:
sage: Z = matrix(2,2)
sage: Z[0,0] = sqrt(2)
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-29-94541bcbfa2b> in <module>()
----> 1 Z[Integer(0),Integer(0)] = sqrt(Integer(2))
sage/matrix/matrix0.pyx in sage.matrix.matrix0.Matrix.__setitem__ (/build/sagemath/src/sage-
7.6/src/src/build/cythonized/sage/matrix/matrix0.c:8316)()
sage/matrix/matrix0.pyx in sage.matrix.matrix0.Matrix._coerce_element (/build/sagemath/src/sage-
7.6/src/src/build/cythonized/sage/matrix/matrix0.c:9795)()
sage/structure/parent.pyx in sage.structure.parent.Parent.__call__ (/build/sagemath/src/sage-
7.6/src/src/build/cythonized/sage/structure/parent.c:9844)()
sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.NamedConvertMap._call_
(/build/sagemath/src/sage-7.6/src/src/build/cythonized/sage/structure/coerce_maps.c:6402)()
sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._integer_ (/build/sagemath/src/sage-
7.6/src/src/build/cythonized/sage/symbolic/expression.cpp:8607)()
TypeError: unable to convert sqrt(2) to an integer
Since I couldn't really find a way to create one in the documentation (at least I didn't find one), I would like to ask, if there is a easy solution to this.
Thank you very much
SelfesMon, 24 Apr 2017 09:30:12 -0500http://ask.sagemath.org/question/37388/help with simple integration of piecewise function?http://ask.sagemath.org/question/24887/help-with-simple-integration-of-piecewise-function/ r = var('r')
Piecewise([[(1,2), 1/floor(r)]]).integral(r,1,2)
Gives an error:
Error in lines ...
AttributeError: 'sage.rings.integer.Integer' object has no attribute 'variables
What am I doing wrong? I note that
Piecewise([[(1,2), 1/floor(r)]]).integral(r)
gives output ``Piecewise defined function with 1 parts, [[(1, 2), r1 |--> integrate(1/floor(r1), r1, 1, r1)]]''.
And that
integrate(1/floor(r1), r1, 1, r1)(2)
gives the same error as the first attempt. Here's the full stacktrace of the error:
EDIT: Thanks. Here's the full stack trace for the error:
File "/Applications/Sage-6.3.app/Contents/Resources/sage/local/lib/python2.7/site-packages/sage/functions/piecewise.py", line 833, in integral
return F(b) - F(a)
File "/Applications/Sage-6.3.app/Contents/Resources/sage/local/lib/python2.7/site-packages/sage/functions/piecewise.py", line 665, in __call__
return self.functions()[n-1](x0)
File "expression.pyx", line 4391, in sage.symbolic.expression.Expression.__call__ (build/cythonized/sage/symbolic/expression.cpp:21933)
File "/Applications/Sage-6.3.app/Contents/Resources/sage/local/lib/python2.7/site-packages/sage/symbolic/callable.py", line 477, in _call_element_
return SR(_the_element.substitute(**d))
File "expression.pyx", line 4242, in sage.symbolic.expression.Expression.substitute (build/cythonized/sage/symbolic/expression.cpp:21183)
File "/Applications/Sage-6.3.app/Contents/Resources/sage/local/lib/python2.7/site-packages/sage/symbolic/integration/integral.py", line 161, in _eval_
if len(x.variables()) == 1:
File "element.pyx", line 344, in sage.structure.element.Element.__getattr__ (build/cythonized/sage/structure/element.c:4022)
File "misc.pyx", line 257, in sage.structure.misc.getattr_from_other_class (build/cythonized/sage/structure/misc.c:1775)
AttributeError: 'sage.rings.integer.Integer' object has no attribute 'variables'NealSun, 16 Nov 2014 16:35:36 -0600http://ask.sagemath.org/question/24887/Working with complex symbolic expressionshttp://ask.sagemath.org/question/8716/working-with-complex-symbolic-expressions/I am a beginner at Sage so my questions may not be well informed, so bear with me.
The context: I am trying to use sage to explore the exponential function assuming all I know about it is that it is its own derivative and has the value 1 at z = 0. It is then easy to develop the Taylor series to any degree using formula like:
expp2( z ) = 1 + ( 1/ factorial( 1 ) )* z + ( 1/ factorial( 2 ) ) * ( z ^ 2 )
You can take 2 of these for z = a and z = b and multiply them together in Sage. Getting something like:
1/4*(b^2 + 2*b + 2)*(a^2 + 2*a + 2)
Now I have done the algebra by hand and know this reduces to the Taylor expansion for z = a + b.
What I do not get is how to show this part in Sage in a nice clean way ( I have some ways I do not like so much ).
Here I have 2 questions one specific, and one general ( I am interested in the answer to either one or both):
1) If this exponential question interests anyone, could you offer some tips? I have tried various ( but not all ) applications of expand and simplify. I am still plugging away.
2) Is there a guide that would help me learn how to carry out algebraic operations over complex expressions. I have looked at several basic tutorials, but they do not have much detail. The most useful single resource I have found is
http://www.sagemath.org/doc/reference/sage/symbolic/expression.html
and the pages linking from it.
russ_henselMon, 13 Feb 2012 08:07:47 -0600http://ask.sagemath.org/question/8716/built in cython integration [beginner]http://ask.sagemath.org/question/11360/built-in-cython-integration-beginner/When I run a built-in sage function that is defined as cython code, does it automatically run compiled code, or do I have to activate cython or something?
For example:
When I run the code
g = graphs.CompleteGraph(10)
g.traveling_salesman_problem()
in a notebook, without anything else, the second command contains instances of 'MixedIntegerLinearProgram', which appears to be a cdef from a pyx file. I've learnt today that this means it is written in cython, and I would like to confirm that if MixedIntegerLinearProgram gets called in g.traveling_salesman_problem(), that it does indeed run compiled code, and that I don't have to do anything special to activate it.alejandroericksonThu, 24 Apr 2014 01:54:25 -0500http://ask.sagemath.org/question/11360/A (beginner) problem with Cython, IIhttp://ask.sagemath.org/question/10507/a-beginner-problem-with-cython-ii/Why the following code generates a problem in Sage-Cython ?
(note the recursion and the global variable bb)
cpdef lintsolve(list T, list A, int m, int ep):
global bb;
cdef int t0,t,n,i,m1
cdef list A1,T1
if not A:
bb=[]
t0=T[0]
t=len(T)
if t==1 and m%t0==0:
A1=[ep]
A1.extend(A)
A1.append(m//t0)
bb.extend([A1])
if t>1:
n=m//t0
for i in range(n+1):
T1=T[1:]
m1=m-i*t0
A1=A[:]
A1.append(i)
lintsolve(T1,A1,m1,ep)
return bb
while the following code runs very well in Sage-Python :
def lintsolve(T,A,m,ep):
global bb;
if not A:
bb=[]
t0=T[0]
t=len(T)
if t==1 and m%t0==0:
A1=[ep]
A1.extend(A)
A1.append(m//t0)
bb.extend([A1])
if t>1:
n=m//t0
for i in range(n+1):
T1=T[1:]
m1=m-i*t0
A1=A[:]
A1.append(i)
lintsolve(T1,A1,m1,ep)
return bbSébastien PalcouxThu, 05 Sep 2013 00:07:44 -0500http://ask.sagemath.org/question/10507/A (beginner) problem with Cythonhttp://ask.sagemath.org/question/10504/a-beginner-problem-with-cython/Why the following small code generates a problem in Sage-Cython ?
from sage.all import *
cpdef acceptablenumbers(int n, int m):
cdef int i,a,s
cdef list S,l
S=[]
for i in range(n,m+1):
l=list(factor(i))
a=len(l)
s=sum(l[j][1] for j in range(a))
if ((a==3 and s>3) or a>3):
S.append(i)
return S
while the following code runs very well in Sage-Python :
def acceptablenumbers(n,m):
S=[]
for i in range(n,m+1):
l=list(factor(i))
a=len(l)
s=sum(l[j][1] for j in range(a))
if ((a==3 and s>3) or a>3):
S.append(i)
return SSébastien PalcouxWed, 04 Sep 2013 10:48:34 -0500http://ask.sagemath.org/question/10504/networkx beginnerhttp://ask.sagemath.org/question/10467/networkx-beginner/Hi,
I installed networkx package along with imagemagick( i am required to use it in my project). While going through the tutorial pdfs i got stuck at the second line. The first line [ import networkx as nx ] works fine but on executing the second line:
[ G=nx.Graph() ], i get an error as:
**bash: syntax error near unexpected token `('**
Please help me in this regard. I am a complete newbie to networkx as well as python. Thank You.
Prashant JyotiSat, 24 Aug 2013 05:59:44 -0500http://ask.sagemath.org/question/10467/simple(?) exponentiation simplificationhttp://ask.sagemath.org/question/10345/simple-exponentiation-simplification/I'm beginning with <code>sage</code>. It sure looks great but I still have to understand what is the actual meaning of what I type in :-(
This is where I am currently stuck:
<pre>
<code>
sage: simplify( (a/b)^c - (a^c)/(b^c) )
-a^c/b^c + (a/b)^c
</code>
</pre>
I would have loved to get a nice clean **$0$**.
What do I need to tell <code>sage</code> so that it sees that $(a/b)^c$ and $a^c/b^c$ are the same?phsThu, 11 Jul 2013 08:36:09 -0500http://ask.sagemath.org/question/10345/Loading and analyzing data, Datamininghttp://ask.sagemath.org/question/8077/loading-and-analyzing-data-datamining/I am considering using Sage as a data-mining type tool. Is this way off base? If so where should I head? I do not see much in the reference docs on loading and interfacing with large-ish datafiles? As well as cleaning, standardizing and faceting the data.
In particular I have a 18mg compressed CSV file that expands to at least 512 mg.. It has about 20 fields per record and 400,000 or so records (it is Residential Real Estate listing data) I need to summarize, average and count by area, subdivision and dates (and more) I had been using An Access database, but am looking to move to Python, and avoid an SQL structure if possible, since now it seems we have enough memory to hold the data in memory..
My brief search has not turned up any similar examples??
At the least any pointers to loading the compressed CSV? and guidance on the right Python/Sage Data structures to start off with?dartdogFri, 15 Apr 2011 07:11:22 -0500http://ask.sagemath.org/question/8077/Is n() the same as N() if not why nothttp://ask.sagemath.org/question/8635/is-n-the-same-as-n-if-not-why-not/I am trying to use Sage as a simple numerical calculator but sometimes have to fight with more advanced features, things I think are number are not. I starting using n() to fix this up, but in some cases it did not work. Bug big N N() did. Is this the way it is supposed to be ( if so why ). Identical commands ( symbols..... ) except for capitilization seems like a bad idea. Here is the code:
# Is n() the same as N() if not why not
xll = 1. / ( pi * ( 2. ^ .5 ) )
xll = 22. * 2
print "xll is of type", type( xll )
print ""
print "Big N next"
print N( ll )
print ""
print "Little n next"
print n( xll )
produced output:
xll is of type <type 'sage.rings.real_mpfr.RealNumber'>
Big N next
0.0000120042175487614
Little N next
Traceback (click to the left of this block for traceback) ...
TypeError: 'sage.rings.real_mpfr.RealLiteral' object is not callableruss_henselTue, 17 Jan 2012 11:29:55 -0600http://ask.sagemath.org/question/8635/Solving a System of Differential Equationshttp://ask.sagemath.org/question/8239/solving-a-system-of-differential-equations/I am attempting to solve and graph the solution to an initial value problem containing a system of differential equations. If I am remembering calculus correctly, its properties (nonlinear, ordinary, no explicit appearance of the independent variable time) classify it as a 'time-invariant autonomous system'.<br>
I want to model a situation in which a microorganism in a bioreactor at some concentration 'X' is growing exponentially 'dX/dt = u*X' by consuming a substrate at some concentration 'S'.<br>
Substrate consumption '-dS/dt' and microorganism growth rate 'dX/dt' are related by a constant representing the yield of biomass on substrate, Y. If I stop here and leave the specific growth rate 'u' a constant, I can evaluate this just fine with **desolve_system()**:<br>
![image description](https://lh6.googleusercontent.com/-YrUX99Qnkkg/TidnGlC0d3I/AAAAAAAAABE/rME_HWK4sfM/s912/Screenshot-3.png)
The problems start when I make 'u' (specific growth rate) a function of 'S' (substrate concentration):<br> u = u_max*S/(K_m+S).<br>
I make the assumption that this must be evaluated numerically, and switch to **desolve_system_rk4()**. I get an "Error executing code in Maxima", which also states "in definition f_rk_4, found bad argument 'X(t)"
![image description](https://lh6.googleusercontent.com/-bGVea6dD6tc/Tig4WzV6qvI/AAAAAAAAABc/Bg-0WHwFTWc/Screenshot-4.png)
Where am I going wrong? Can I use desolve_system_rk4() to evaluate this system, and I'm just making a syntax error?
Thank you for your patience - I just started using SAGE yesterday.
- BrandonBrandon CurtisWed, 20 Jul 2011 13:44:36 -0500http://ask.sagemath.org/question/8239/How to use R with Sage?http://ask.sagemath.org/question/7805/how-to-use-r-with-sage/Just loaded the Sage system, went through tutorial (very quickly) searched around, I see it says it has R but I don't see any reference to it in any docs?? I was hoping to use Sage as A notebook type interface to R? mainly for doing exploratory data analysis or something like that...dartdogSun, 16 Jan 2011 11:50:16 -0600http://ask.sagemath.org/question/7805/Why does almost everything return "<built-in method METHOD of sage.symbolic.expression.Expression object at SOME_ADDRESS>"?http://ask.sagemath.org/question/7781/why-does-almost-everything-return-built-in-method-method-of-sagesymbolicexpressionexpression-object-at-some_address/Maybe the answer is obvious, but I couldn't find anything.
Let's say I have a variable that contains a symbolic expression. I can type a "." (dot) at the end and press TAB and I get a huge list of methods I can choose from. So I choose simplify_full or trig_reduce or something similar (I tried about 8 different methods). Now comes the thing I don't understand: Almost anything returns:
> built-in method METHODNAME of
> sage.symbolic.expression.Expression
> object at SOME_ADDRESS
For example:
> built-in method reduce_trig of
> sage.symbolic.expression.Expression
> object at 0x73b5ab8
Is there something wrong with my sage configuration? I'm running Ubuntu 10.10 64-bit.
Taylor SeriesWed, 01 Dec 2010 00:47:42 -0600http://ask.sagemath.org/question/7781/where to get python coding tutorial for beginners link PDFhttp://ask.sagemath.org/question/7663/where-to-get-python-coding-tutorial-for-beginners-link-pdf/ reading a tutorial on writing Python code
Link to PDF filesriramSat, 18 Sep 2010 00:58:20 -0500http://ask.sagemath.org/question/7663/