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.Tue, 02 Apr 2013 12:42:35 +0200Convert a text file into a matrixhttps://ask.sagemath.org/question/9973/convert-a-text-file-into-a-matrix/I have a file with a list of elements inside the square of a Quadratic field, i.e. `QQ(sqrt[13])^2`. The file looks like `[[2 + sqrt[13],4 - 2*sqrt[13]],[2,sqrt[13]],...]`. I want to import this file into a single matrix over the field, in the previous example `F.<a> = QuadraticField(13)`. Currently, I read everything in as a string then convert to the matrix, but there must be an easier way to do this. I am somewhat new to python programming in general.jmcshan1Tue, 02 Apr 2013 12:42:35 +0200https://ask.sagemath.org/question/9973/PicklingError while defining some functional operatorshttps://ask.sagemath.org/question/9781/picklingerror-while-defining-some-functional-operators/Using Sage the following simplified code is to recursively compute two functions starting from their initial estimations. When I try to introduce a "numerical integral operator" to the code, instead of using the built-in functions, it gives an error:
> PicklingError: Can't pickle <type 'function'>: attribute lookup __ builtin__.function failed
the code is:
reset()
forget()
from multiprocessing import Pool, cpu_count
#variables
var('x,y, x1,y1')
N=5 #number of iterations
var('q')
#functions' name definition (without specifying the rules) # n is the level of estimation
U0=[]
U1=[]
for n in range(N+1):
U0.append(function('U0_%s' %i, x,y))
U1.append(function('U1_%s' %i, x,y, x1,y1))
#initial estimation of the functions
U0[0]=sin(x+y)
U1[0]=sin(x1+y1)*cos(x-y)
#numerical integrator
num=20.0 # at each call of the int function h is assigned (b-a)/num, so assigned to num here
def numint(f, x, a, b, h):
#pickle_function(f)
integ = 0.5*h*(f(x=a) + f(x=b))
for i in range(1,num):
integ = integ + h * f(x=a+i*h)
return integ
#the integral operators
def Ix(f,x):
return pool.apply( numint, (f,x,-1.0,+1.0,2.0/num) )
def Iy(f,y):
return pool.apply_async( numint, (f,y,-1.0,+1.0,2.0/num) )
def IR(f,x,y):
return Iy(Ix(f,x),y)
def N0(n,f0,f1):
return f0[n] + IR(f1[n],x1,y1) + IR(IR(f1[n],x,y),x1,y1)
def N1(n,f0,f1):
return f1[n] + IR(f0[n],x,y) - IR(IR(f1[n],x,y),x1,y1)
#the calculations
pool = Pool(processes=cpu_count())
for n in range(N):
worker0 = N0(n,U0,U1)
worker1 = N1(n,U0,U1)
U0[n+1] = U0[n] - worker0.get()
U1[n+1] = U1[n] - worker1.get()
show(U0[n+1])
show(U1[n+1])
As far as I have understood from searching the web and reading the documents this pickling error is either due to the integral operator (a function anyway) not being Picklable or due to its arguments not being picklable. I tried to make the operator itself picklable at no success, but I guess the problem should be due to the integral operator itself being a functional of its integrand, and as it is an *operator* the function in its integrand is not to be determined since the very beginning, so maybe the first argument of the operator (which itself is a function) not being clearly defined at top level is the reason for the operator not being in overall picklable? Any idea how to overcome this error?
**NOTE**. the main code is much more complex than the minimalistic code provided here, so I rather need to define an integral operator to prevent the code's readability.
owariTue, 12 Feb 2013 16:16:51 +0100https://ask.sagemath.org/question/9781/Object Persistence db_save errorhttps://ask.sagemath.org/question/9598/object-persistence-db_save-error/Take the following class. Why can't I use `db_save` to store an instance of this class? The reason I am using this class instead of just a set is because I will eventually derive this class from RingElement rather than SageObject, because I'm looking to create a Ring of Sets.
class FakeSet(SageObject):
def __init__(self,_set):
self.values = _set
self.iterator = self.__iter__()
if self.values != set():
self.next = self.iterator.next()
def __iter__(self):
for this_entry in self.values:
yield this_entry
def get_set(self):
return self.values
def __repr__(self):
return str(list(self.values))
def __getattr__(self,attr):
return getattr(self.values,attr)
Then I enter:
sage: fake = FakeSet([1,2,3])
sage: print fake
sage: db_save(fake,"test")
to which the interpreter responds:
Traceback (click to the left of this block for traceback)
...
cPickle.PicklingError: Can't pickle <type 'generator'>: attribute
lookup __builtin__.generator failed
SLOtoSFSun, 02 Dec 2012 20:06:17 +0100https://ask.sagemath.org/question/9598/How to save a list to a file, with sage objects in it?https://ask.sagemath.org/question/8560/how-to-save-a-list-to-a-file-with-sage-objects-in-it/Hi, I am trying to save intermediate step of my calculation into a file, so that next time I can read the file to resume the calculation. Say, I need to save
f = function('a',var('t'))
s = [f] # together with other things in the list
How to save s to a file?
I tried two ways:
(1) Standard python module pickle cannot save sage objects, thus fails
(2) I converted s to a python object:
s = Sequence([f])
Now I can use s.save('file_name'). However, there is a bug that I cannot load using load('file_name')
RuntimeError: unknown function 'a' in archive
It is a known bug: http://trac.sagemath.org/sage_trac/ticket/11919 .
Unfortunately I am not expert enough to fix that bug, and I really want to get my work done before waiting for the fix. Is there any other way to save the list s currently?
In the bug report it is mentioned that an older version of Pynac doesn't have this bug. If there is no other work around, is it possible to downgrade Pynac within sage or I have to downgrade the whole sage?
Thanks!
tririverThu, 15 Dec 2011 19:59:22 +0100https://ask.sagemath.org/question/8560/pickling extension classeshttps://ask.sagemath.org/question/8376/pickling-extension-classes/What is the right way to pickle a cython extension class? Consider the example
cdef class Stuff:
def __init__(self):
pass
then
a = Stuff()
a == loads(dumps(a))
gives the result **False** because the address of the new object generated by *loads* is different from the original object *a*.
Hartmut MonienFri, 14 Oct 2011 00:47:27 +0200https://ask.sagemath.org/question/8376/UnpicklingError when trying to run notebook()https://ask.sagemath.org/question/7752/unpicklingerror-when-trying-to-run-notebook/When I first downloaded and installed Sage (several versions ago) to try it out on Ubuntu, everything worked without error. I didn't do much with it, time passed, and when I came back to Sage, I saw 4.6 was there. I completely erased the folder that Sage was in, downloaded and untarred 4.6, and ran Sage. That's fine--commands input in the terminal work fine. But when I try to run notebook(), I get
<code>
UnpicklingError Traceback (most recent call last)
/home/me/.sage/<ipython console> in <module>()
/home/me/Math/sage46/devel/sagenb/sagenb/notebook/notebook_object.pyc in __call__(self, *args, **kwds)
215 """
216 def __call__(self, *args, **kwds):
--> 217 return self.notebook(*args, **kwds)
218
219 notebook = run_notebook.notebook_twisted
.
.
.
UnpicklingError: invalid load key, 'n'.
</code>
I know how to spell Python, but not much more. Any idea what got messed up and suggestions on how to fix it?PeterFri, 05 Nov 2010 21:15:58 +0100https://ask.sagemath.org/question/7752/What is the standard pickle jar for and how do I update ithttps://ask.sagemath.org/question/7692/what-is-the-standard-pickle-jar-for-and-how-do-i-update-it/I started Trac #9907, which basically consists of a relocation of some general decorators into `sage.misc.decorators`. However, after applying the patch, the doctest for `sage.structure.sage_object.unpickle_all` fails. It seems to be because there is a standard pickle jar which still contains the information that the decorators are located in `sage.plot.misc`.
Is it correct that there is such a pickle jar? In that case, what for, and how do I get about updating it and fixing my patch?
Cheers, JohanjsrnFri, 17 Sep 2010 05:32:18 +0200https://ask.sagemath.org/question/7692/