pickle for Function and Derivative

27 views
Skip to first unread message

Thomas Ligon

unread,
Jan 28, 2023, 11:38:46 AM1/28/23
to sympy
Should pickle work for Function and Derivative? I found Sympy issue #4297 and the answer seems to be no.
Nevertheless, here is what I have:
I have a private dictionary containing expressions of symbols, and everything works fine.
Then I created a new private dictionary that includes Function and Derivative, and I can't pickle it. Here are some code snippets:

    q1S, q2S = symbols('q1, q2')
    q1F = Function('q1')
...
    varX = globals()['CuspData']
    fileName = os.path.join(config.pickle_dir, 'CuspData.pckl')
    f = open(fileName, "wb")
    varX = {'q1d0': q1F(tS), 'q1d1': Derivative(q1F(tS), tS)} # test
    #with evaluate(False): pickle.dump(varX, f)
    pickle.dump(varX, f)
    f.close()
The error is "Can't pickle q1: attribute lookup q1 on __main__ failed"

Carl K

unread,
Jan 28, 2023, 5:10:51 PM1/28/23
to sy...@googlegroups.com
I've had good luck with CloudPickle, a package, when Pickle doesn't work.

From: sy...@googlegroups.com <sy...@googlegroups.com> on behalf of Thomas Ligon <thomas...@gmail.com>
Sent: Saturday, January 28, 2023 8:38:46 AM
To: sympy <sy...@googlegroups.com>
Subject: [sympy] pickle for Function and Derivative
 
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/6821930c-117b-4b15-abec-f003782dcf85n%40googlegroups.com.

Oscar Benjamin

unread,
Jan 28, 2023, 5:20:04 PM1/28/23
to sy...@googlegroups.com
You need to call the variable the same name as the function:

q1 = Function('q1')

--
Oscar

Thomas Ligon

unread,
Jan 29, 2023, 12:37:40 PM1/29/23
to sympy
Thanks! That was easy and works fine.
Reply all
Reply to author
Forward
0 new messages