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.Fri, 25 Jan 2013 00:10:27 +0100Passing params to cythonized ode_system()https://ask.sagemath.org/question/9738/passing-params-to-cythonized-ode_system/Hi, I am trying to code a batch process to solve an ODE many times over a range of parameters. I am using a modified version of an example in the sage documentation:
%cython
cimport sage.gsl.ode
import sage.gsl.ode
include 'gsl.pxi'
cdef class duffing_osc(sage.gsl.ode.ode_system):
cdef int c_f(self,double t, double *y,double *dydt):
dydt[0]=y[1]
dydt[1]=y[0]-params[0]*y[1]-y[0]*y[0]*y[0]+params[1]*cos(params[2]*t)
return GSL_SUCCESS
cdef int c_j(self, double t,double *y,double *dfdy,double *dfdt):
dfdy[0]=0
dfdy[1]=1.0
dfdy[2]=-3*y[0]*y[0]+1
dfdy[3]=-params[0]*y[1]
dfdt[0]=0
dfdt[1]=-params[1]*params[2]*sin(params[2]*t)
return GSL_SUCCESS
Now, the problem of course is that when trying to generate the .spyx file, param is undeclared, since I can't overload the c_f and c_j functions. Is there a way I can further modify my derived class (duffing_osc) so that when I instantiate an object I can pass in the parameters, such as d = duffing_osc(params). Then I suppose I would have to change params[i] to self.params[i] in my definitions for c_f and c_j.gitanoFri, 25 Jan 2013 00:10:27 +0100https://ask.sagemath.org/question/9738/method overloading error?https://ask.sagemath.org/question/8645/method-overloading-error/Hi all:
I made a class called RationalExpression that encodes a rational expression (ratio of polynomials). It contains a class method called 'numerator' that returns the numerator of a RationalExpression instance, but the method doesn't work when i work with a multivariate polynomial ring. The 'numerator' method appears to clash with another 'numerator' method. Do you know how i can fix my bug?
Here's the relevant code snippet from /Users/arai021/Dropbox/sage_projects/pfd.py:
class RationalExpression(object):
r"""
Represents a rational expression `P/(Q_1^e_1 \cdots Q_m^e_m)` by storing the
parts `P` and `[[Q_1,e_1], \ldots, [Q_m,e_m]]`, where
`P, Q_1, \ldots, Q_m` are elements of a common polynomial ring, and
`e_1, \ldots, e_m` are positive integers.
"""
def __init__(self, num, *denoms):
self.numerator = num
self.base_ring = num.parent()
self.denominators = sorted(denoms)
def __str__(self):
return str([self.numerator, self.denominators])
def numerator(self):
return self.numerator
And here's a failing example:
sage: attach("/Users/arai021/Dropbox/sage_projects/pfd.py")
sage: R.<x,y>= PolynomialRing(QQ)
sage: P = x*y**2
sage: Qs = [x*y,3], [x,1]
sage: f = RationalExpression(P, *Qs)
sage: print f
[x*y^2, [[x, 1], [x*y, 3]]]
sage: type(f)
<class '__main__.RationalExpression'>
sage: f.numerator()
Traceback (most recent call last): print f
File "", line 1, in <module>
File "/private/var/folders/bc/bc2aJOjCE20MqpWiFIh58++++TQ/-Tmp-/tmpcbitNd/___code___.py", line 10, in <module>
exec compile(u'print f.numerator()' + '\n', '', 'single')
File "", line 1, in <module>
File "multi_polynomial_libsingular.pyx", line 2025, in sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular.__call__ (sage/rings/polynomial/multi_polynomial_libsingular.cpp:14808)
TypeError: number of arguments does not match number of variables in parentaraichevWed, 18 Jan 2012 20:51:49 +0100https://ask.sagemath.org/question/8645/a fast function taking either variable or collection as argumenthttps://ask.sagemath.org/question/8536/a-fast-function-taking-either-variable-or-collection-as-argument/I want to create a function which takes either variable or collection as argument.
If the input is any kind of collection, function should be taken of every element in it and return a numpy array of results.
Currently, I do in this way:
def simplefunc(x):
<some code here> # it is assumed that x is a single variable
return <some single result>
def arrayfunc(x):
try: #check if we have a collection
x_iterator = iter(x)
except TypeError: #this is a single expression
return simplefunc(x)
else: # iterate through iterable x
ret=[]
for xx in x:
ret.append(simplefunc(xx))
return numpy.array(ret)
It works as is, however, I do not think this is the fastest way possible,especially the method to figure out if the input is a collection. I also do not like that strings are considered as collections and split to chars (I can stand it though). Is there a more elegant way?
I tried also to call arrayfunc() recursively on each element of collection (instead of simplefunc()), to handle collections of collections in the same way, however, it runs into infinite loop on strings. I have to check if the input is a string explicitly.
ADuC812Wed, 07 Dec 2011 12:58:12 +0100https://ask.sagemath.org/question/8536/