I use the code
from sympy.physics.vector import *
RF = ReferenceFrame('e')
from sympy.physics.vector import gradient
scalar_field = 1/(sqrt(RF[0]**2+RF[1]**2+RF[2]**2))
gradient(scalar_field, RF)
The output is
Now I want to make substitutions, e_x = 1, etc., or maybe e_x= t.
Does it possible? How?
Substitutions are performed with subs method, for example:
gr = gradient(scalar_field, RF)
gr.subs({RF[0]: 1, RF[1]: 2, RF[2]: 3})
outputs
- sqrt(14)/196*e.x - sqrt(14)/98*e.y - 3*sqrt(14)/196*e.z
You can also substitute formulas, say
var('t')
gr.subs({RF[0]: 1, RF[1]: t, RF[2]: 3*t+2})
Related
When trying to get deflection equation using sympy beam module in continuum mechanics, i get an error if I use float in location argument
from sympy.physics.continuum_mechanics.beam import Beam
from sympy import symbols, Piecewise
E, I = symbols ( 'E, I')
b = Beam (30, E, I)
b.apply_support (0, 'roller')
b.apply_support (10 , 'roller')
b.apply_support (30, 'roller')
b.apply_load (-10, 5, -1) ## if 5. is changed to 5, deflection equation works?
b.apply_load (-10, 15, -1) ## if 5. is changed to 5, deflection equation works?
R_0, R_10, R_30 = symbols ('R_0, R_10, R_30')
b.solve_for_reaction_loads (R_0, R_10, R_30)
b.load
b.shear_force ()
b.plot_shear_force ()
b.deflection ()
Does anyone know if above commented lines are valid, or I have to convert argument values into integers?
Yes, you may have to use integers otherwise (as in this case) an inconsistent set of equation might result which has no solution. In your case the equations to be solved are
[C4, 10*C3 + C4 + 156.25, 30*C3 + C4 + 468.749999999996]
Because of rounding errors, the value of C3 calculated from the second equation is not the same as that from the 3rd so the EmptySet is returned as a solution.
Im trying to solve some inequations containing absolute values and I want to use sympy to make my life a bit easier.
There are some conditions for the given variable to be followed, for example:
Let x be element of [-1, 0). Find the zero point of `f(x) = |-2.5x^3-3x^2-0.5x|`
where |...| indicates the absolute value.
I've tried different things like:
import sympy as sp
x = sp.Symbol('x', real=True)
i = sp.Interval.Ropen(-1, 0)
f = sp.Abs(-2.5*x**3 - 3*x**2 - 0.5*x)
print(sp.imageset(x, f, i))
Apparently the imageset function has some problems with absolute values. Also I don't know if imageset is the right function at all.
Is there a way like:
import sympy as sp
i = sp.Interval.Ropen(-1, 0)
x = sp.Symbol('x', real=True, element_of=i)
f = sp.Abs(-2.5*x**3 - 3*x**2 - 0.5*x)
print(sp.solve(f))
to print a set of solutions??
If you are trying to get positive or negative solutions, give that assumption to your variable and use solve:
>>> x = Symbol('x', negative=True)
>>> solve(x**2 - 1)
[-1]
If you really want to specify a domain/interval that is not just positive or negative, then pass that interval to solveset:
>>> solveset((x-3)**2-1,x)
{2, 4}
>>> solveset((x-3)**2-1,x,Interval(1,3))
{2}
The integrate.quad function in scipy displays the analytical and absolute error of the integration. How do
I make it output just the analytical portion?
The code below returns (21.333333333333332, 2.3684757858670003e-13). I just want 21.3333
from scipy import integrate
x2 = lambda x: x**2
integrate.quad(x2, 0, 4)
This way you can print only the 21.3333
from scipy import integrate
x2 = lambda x: x**2
results = integrate.quad(x2, 0, 4)
print(results[0])
The output will be:
21.333333333333336
integrate.quad(x2,0,4) returns a tuple. Indexing is one way of dealing with this:
my_result = integrate.quad(x2, 0, 4)[1]
Another option is unpacking the tuple, using underscore for the values you are not interested in:
_, my_result = integrate.quad(x2, 0, 4)
Note that the underscore is still accessible like any other variable would have been (you could also have called it a, or temp, or not_interesting), but using an underscore is a Pythonic way that will make other people reading your code understand that the variable will not be used.
I have 2 data sets x1 and x2. I want to be able to get a total sum of all the products of x1 and x2 only in the rows where the From column has Auckland in it.
see here
The final answer should be (5*1) + (2*1) + (3*1) + (4*1) or 14. The PuLP code that I wrote to do this is given below
# Import PuLP modeller functions
from pulp import *
varFinal = sum([x1[a] * x2[a] for a in Arcs if a == Nodes[0]])
print Nodes[0]
print Arcs[0]
Final = varFinal
The output that gets printed to the console is
Auckland
('Auckland', 'Albany')
I realise that my final value is zero because Arcs[some number] does not equal Nodes[some number]. Is there anyway to change the code so my final value is 14?
Any help is appreciated.
Welcome to stack overflow! Cause you've only posted part of your code, I have to guess at what data-types you're using. From the output, I'm guessing your Nodes are strings, and your Arcs are tuples of strings.
Your attempt is very close, you want the from column to have Auckland in it. You can index into a tuple the same way you would into an array, so you want to do: a[0] == Nodes[0].
Below is a self-contained example with the first bit of your data in which outputs the following (note that I've changed to python 3.x print statements (with parentheses)):
Output:
Auckland
('Auckland', 'Albany')
14
Code:
# Import PuLP modeller functions
from pulp import *
# Data
Nodes = ['Auckland',
'Wellington',
'Hamilton',
'Kansas City',
'Christchuch',
'Albany',
'Whangarei',
'Rotorua',
'New Plymouth']
Arcs = [('Auckland','Albany'),
('Auckland','Hamilton'),
('Auckland','Kansas City'),
('Auckland','Christchuch'),
('Wellington','Hamilton'),
('Hamilton','Albany'),
('Kansas City','Whangarei'),
('Christchuch','Rotorua')]
x1_vals = [1, 2, 3, 4, 5, 9, 11, 13]
x2_vals = [5, 1, 1, 1, 1, 1, 1, 1]
x1 = dict((Arcs[i], x1_vals[i]) for i in range(len(Arcs)))
x2 = dict((Arcs[i], x2_vals[i]) for i in range(len(Arcs)))
varFinal = sum([x1[a] * x2[a] for a in Arcs if a[0] == Nodes[0]])
print(Nodes[0])
print(Arcs[0])
print(varFinal)
For future reference, answers are most likely to be forthcoming if you include code which others can try to run (without external data dependencies), that way people can try to run it, fix it, and re-post it.
I don't know my question is possible or not. I am using ortools to solve an optimization problem and I know in the part of conditions the argument should be defined in double type, like this:
constraints[i] = solver.Constraint(0.0 , 10,0)
But my problem is that, I don't want to use this type of argument in creating conditions. For example I want to have a list.
So I wrote this in my code:
constraints[i] = solver.Constraint([1,2,3,...])
And I got this error:
return _pywraplp.Solver_Constraint(self, *args)
NotImplementedError: Wrong number or type of arguments for overloaded
function 'Solver_Constraint'.
Possible C/C++ prototypes are:
operations_research::MPSolver::MakeRowConstraint(double,double)
operations_research::MPSolver::MakeRowConstraint()
operations_research::MPSolver::MakeRowConstraint(double,double,std::string
const &)
operations_research::MPSolver::MakeRowConstraint(std::string const &)
Is there any way to change the type of condition's argument?
My Assumptions
your constraint expression is "a sum of some lists", meaning something along the lines of what the NumPy library does: e.g., if you have two lists of values, [1, 2, 3] and [4, 5, 6], their sum would be element-wise, s.t. [1, 2, 3] + [4, 5, 6] = [1+4, 2+5, 3+6] = [5, 7, 9].
your "list constraint" is also element-wise; e.g., [x1, x2, x3] <= [1, 2, 3] means x1 <= 1, x2 <= 2 and x3 <= 3.
you're using the GLOP Linear Solver. (Everything I say below applies to the ILP/CP/CP-SAT solvers, but some of the particular method names/other details are different.)
My Answer
The thing is, ortools only lets you set scalar values (like numbers) as variables; you can't make a "list variable", so to speak.
Therefore, you'll have to make a list of scalar variables that effectively represents the same thing.
For example, let's say you wanted your "list variable" to be a list of values, each one subjected to a particular constraint which you have stored in a list. Let's say you have a list of upper bounds:
upper_bounds = [1, 2, 3, ..., n]
And you have several lists of solver variables like so:
vars1 = [
# variable bounds here are chosen arbitrarily; set them to your purposes
solver.NumVar(0, solver.infinity, 'x{0}'.format(i))
for i in range(n)
]
vars2 = [...] # you define any other variable lists in the same way
Then, you would make a list of constraint objects, one constraint for each upper bound in your list:
constraints = [
solver.Constraint(0, ubound)
for ubound in upper_bounds
]
And you insert the variables into your constraints however is dictated for your problem:
# Example expression: X1 - X2 + 0.5*X3 < UBOUND
for i in range(n):
constraints[i].SetCoefficient(vars1[i], 1)
constraints[i].SetCoefficient(vars2[i], -1)
constraints[i].SetCoefficient(vars3[i], 0.5)
Hope this helps! I recommend taking (another, if you already have) look at the examples for your particular solver. The one for GLOP can be found here.