I am trying to solve an equation for r when given values for x and y. to do this I am using the solve ability of sympy. the code that I have is
import numpy as np
import matplotlib.pyplot as plt
from sympy import solve
from sympy import Symbol
from sympy import acos,sin
x=2
y=-2
r=Symbol("r",real=True)
solve(r(acos(1.0-(y/r)))-sin(acos(1.0-(y/r)))-x)
when I run the code it gives me the error
'Symbol' object is not callable
line 10, in <module>
solve(r(acos(1.0-(y/r)))-sin(acos(1.0-(y/r)))-x)
the reason I import numpy and matplotlib is that I will use them later in my code. Thanks for any help.
The error directs you toward what to look for: a Symbol that is being called. In Python syntax this is a Symbol followed by pair of parentheses with one or more arguments between them. You probably intended to multiply by r in the first argument of the expression:
>>> solve(r(acos(1.0-(y/r)))...
^__make that r*acos(1.0-(y/r))...
An editor that highlights matching parentheses (like the online editor of Python code at repl.it) can be helpful in these circumstances. Parentheses are either grouping or, when following a Python name, acting as the delimiters for the arguments being passed to a function.
Related
I've tried googling (to little avail) to more clearly understand what different meaning period . has during an import statement, vs. once a module has already been imported.
For example, these all work:
import numpy
X = numpy.random.standard_normal
from numpy.random import standard_normal
import numpy.random
but this doesn't work:
import numpy.random.standard_normal
I'm a bit confused as to why this is. Why is there a difference in what the period . does when accessing a module before vs. after an import?
It's because standard_normal is a method
<built-in method standard_normal of numpy.random.mtrand.RandomState object at 0x0000029D722FBD40>
whenever you do from numpy.random import standard_normal you are importing the method
and i don't think you can do this import numpy.random.standard_normal cause standard_normal again is a method, this would be possible if standard_normal would be some module.
Take a look at this, you when I typed dir(standard_normal) I get the output of those things which are attributes and when I typed standard_normal it says <built-in method standard_normal of numpy.random.mtrand.RandomState object at 0x000002509D504740> cause it simply says it is a method
Now when I did this import numpy.random.standard_normal , you are expecting to import the method right? But what it really does is trying to import a module, Well... there is no such thing as standard_normal module or standard_normal.py file.
Take a look at this again. I imported the random module and I used the . operator to access the standard_normal function. You can see the sense of it right. Cause on the random.py module it has there a standard_normal function or method.
Sorry I had to use the CMD.
If I import sympy with:
from sympy import *
Then how do set lambda to be a symbol and not a function?
E.g.
lambda = symbols('lambda')
In my case, likely because sympy has all its functions imported (I am working in a sympy only environment so it is convenient), I receive the error:
lambda = symbols('lambda')
^
SyntaxError: invalid syntax
Is there any way to avoid this if I am importing all the functions from sympy?
Thank you
This is because lambda is a keyword for creating lambda functions. You can't use it as your variable name. You'll have to find a new name. Even if you find a way to assign lambda to something, it won't work because it's not parsed as a potential variable name.
For example:
lambda_ = symbols('lambda')
will not have the same error.
I am using Python to solve an equation. I added the 'Bessel function' in scipy.special, It was working. Now I want to find a variable using Bessel function. For example, I added the order(1) and value(0.44005058574) in Python, but it is not working. (in order to find the variable, I also used solver)
How I can solve the problem?
import numpy as np
import scipy.special as sc
import math
from sympy import Symbol
from sympy.solvers import solve
x=Symbol('x')
y=sc.jn(1,x)-0.44005058574
print(solve(x))
As the output is hinting, the function scipy.special.jn does not know how to handle the object x from simpy. Instead, you should use a numerical approach
>>> from scipy import optimize
>>> f = lambda x: sc.jn(1, x) - 0.44005058574
>>> root = optimize.newton(f, 1.0)
>>> print(root)
0.9999999999848267
The SymPy docs state the following:
SymPy has dozens of functions to perform various kinds of simplification. There is also one general function called simplify() that attempts to apply all of these functions in an intelligent way to arrive at the simplest form of an expression.
I am using SymPy as a tool to help me relearn maths, so it would be really useful if I could view all of the functions that SymPy tries.
Is is possible to display all of the functions that SymPy tries during simplify()? How can I do this?
The source of simplify is here. According to it, SymPy attempts the following operations, most of which are documented in simplify module docs (the page you linked is from SymPy tutorial, which does not go into details.)
cancel(expr)
_mexpand(expr).cancel()
together(expr, deep=True)
factor_terms(expr, sign=False)
hyperexpand(expr)
piecewise_fold(expr)
besselsimp(expr)
trigsimp(expr, deep=True)
expand_log(expr, deep=True)
logcombine(expr)
combsimp(expr)
sum_simplify(expr)
product_simplify(expr)
quantity_simplify(expr)
powsimp(expr, combine='exp', deep=True)
powsimp(expr)
expand_power_exp(expand_mul(expr)))
exptrigsimp(expr)
To try these directly, import
from sympy import *
from sympy.simplify.simplify import sum_simplify, product_simplify
from sympy.core.function import _mexpand
However, simplify does not just try these methods one by one: most of them are used only when the expression matches some pattern, and some of them are used in combinations.
I'm trying to reproduce the example given here: http://jkitchin.github.io/blog/2013/02/12/Nonlinear-curve-fitting-with-parameter-confidence-intervals/
So I imported the module like that:
from scipy.stats.distributions import t
But when I try to a simple
tval = t.ppf(1-alpha/2, dof)
I have the exception:
AttributeError: 'numpy.ndarray' object has no attribute 'ppf'
So t is a numpy.ndarray. But if I read the doc, it is supposed to be an object, with methods.
Do you have an idea about what's happening ?
It seems you may have overwritten the variable t with an array somewhere. What your error message means is that t is a numpy.ndarray which has no ppf method. The t you intended to import shouldn't be an ndarray but rather a distribution generator.
Either find where it became an array and use another name there, or import with better names.
For example, try changing your import line to this:
from scipy.stats import distrbutions as dists
and then change the problem line to:
tval = dists.t.ppf(1-alpha/2, dof)
Alternatively:
from scipy.stats.distributions import t as tdist
tval = tdist.ppf(1-alpha/2, dof)