If I use the units module in Sympy, I can't find how to see the values of units.
Say I wanted to see what is the gravitional constant.
import sympy.physics.units as u
G = u.gravitational_constant
What should I do to get some value out? I know it's possible to call Sympy's module "convert to" But it assumes I already know what the constant is.
For example to see the speed of light I can write:
u.convert_to(u.speed_of_light,u.meter/u.second)
>>> 299792458 m/s
But that assumes I know speed of light is a velocity.
Every Quantity has a dimension:
>>> from sympy.physics.units import *
>>> G.dimension
Dimension(length**3/(mass*time**2))
Knowing these, you can see the value for your dimensions of interest:
>>> G.convert_to(m**3/kg/s**2)
6.6743e-11*meter**3/(kilogram*second**2)
Related
I am computing the solution to a dynamic non-linear optimization problem, that I set up usign the pyomo library. I use a ConcreteModel, with an objective function and several constraints, all time-indexed.
My objective function takes the form of a ScalarObjective (I am solving a dynamic general equilibrium problem in which I seek to maximize total welfare). I would like to compute the gradient of the objective, evaluated at the optimum, with respect to one of the model's variables at a given period t. My problem is a discrete-time problem.
I have tried many different options, asking AI chatbots for help (both You Chat and ChatGPT), but every solution I'm given is incorrect -- on this topic the AI chatbots seem to know very little.
I feel that some method in the library pyomo.dae could be of help, but I haven't found a solution yet. Could anyone help me, please?
You can do this using Pyomo's differentiate function. Here is a toy example:
import pyomo.environ as pyo
from pyomo.core.expr.calculus.derivatives import differentiate
m = pyo.ConcreteModel()
m.x = pyo.Var()
m.con = pyo.Constraint(expr=m.x<=10)
m.obj = pyo.Objective(expr=m.x**2)
pyo.SolverFactory('ipopt').solve(m)
print(pyo.value(m.x))
# -1.2528349584581178e-10
# Evaluate the derivative at current value of m.x
ddx = differentiate(m.obj, wrt=m.x)
print(ddx)
# -2.5056699169162357e-10
# Return derivative expression
ddx2 = differentiate(m.obj, wrt=m.x, mode='sympy')
print(ddx2)
# 2.0*x
You can read more about this function here: https://github.com/Pyomo/pyomo/blob/main/pyomo/core/expr/calculus/derivatives.py#L31
So I'm trying to create two arrays using numpy. One array is a lot bigger than the other, so I want to search the large array to see where each element in my small array are located (i.e. what index). However when I run the code below, one of the elements in the small array cannot be found and I'm not sure why. Is it a data type mismatch?
Please advise, thank you!
import matplotlib.pyplot as plt
import numpy as np
GMean = np.array([4.23, 4.93, 5.67, 6.62, 4.67])
conc_x = np.arange(0.0, 90, 0.1)
GMean = np.round(GMean, decimals=1)
for i in np.nditer(GMean):
spec_index = np.where(conc_x==i) #look for index in conc_x data set where our GMean data point lies
print i
print spec_index
console output:
4.2
(array([42]),)
4.9
(array([49]),)
5.7
(array([57]),)
6.6
(array([], dtype=int32),) #why can it not find the index here?
4.7
(array([47]),)
So using numpy.around() instead of numpy.round() works. I get an index every time.
I thought they were the same but looking at the documentation, there is a subtle difference:
"Round an array to the given number of decimals."
vs:
"Evenly round to the given number of decimals."
So i'm thinking "evenly round" means it is rounding all trailing digits beyond the desired decimal, and therefore, both numbers you are comparing become exactly the same.
Hope this makes sense.
I know how to fit a tree using sklearn. I also know how to use it for prediction using either predict or predict_proba. However, for prediction I want to get the (raw) sample fractions rather than the probability.
For example, in a fitted tree, two leaf nodes might both have probability of 0.2 for class A but one with 2/10 while the other with 400/2000. Now, if I use this tree, I want to get something like [400,2000] or [2,10] rather than just 0.2.
n_node_sample and value attributes store such information in the fitted tree object but I dont know how to extract the appropriate values from them in prediction.
Thanks in advance.
You can use the tree's tree.tree_.apply method to find out which leaf the point ends up in, and then use the tree.tree_.value array to check how many samples of each class are in this leaf:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
iris = load_iris()
tree = DecisionTreeClassifier(max_depth=2).fit(iris.data, iris.target)
leaf = tree.tree_.apply(iris.data[50:51].astype(np.float32))
print(leaf)
# output [3]
print(tree.tree_.value[leaf])
# output [[[ 0. 49. 5.]]]
print(tree.predict_proba(iris.data[50:51]))
# output [[ 0. 0.90740741 0.09259259]]
In the next version 0.17 tree.tree_.apply will be "public" as tree.apply and will take care of the datatype conversion (to float32). See the docs.
I am using the interpolate package from scipy. In the documentation of the splprep function, it says that amongst the return values, there is also the variable "fp" that contains the residuals of the spline fit.
http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.splprep.html
I don't know how to retrieve the fp value because I cannot call the function with more than two return variables.
Here is some sample code I use:
from scipy import interpolate
[tck_poly, u] = interpolate.splprep([[1.,2.,3.,4.,5.]])
Does anybody know how to get this residual or another easy way to determine the fit quality?
Specify full_output=True:
(tck, u), fp, ier, msg = interpolate.splprep([[1.,2.,3.,4.,5.]], full_output=True)
Is there a command in sympy to simplify sinh(x)+cosh(x) to exp(x)? If I issue
from sympy import *
x = Symbol('x')
(sinh(x)+cosh(x)).simplify()
I just get sinh(x)+cosh(x) back, but I want to see exp(x) instead.
Even assuming that the simplify function in sympy was very good, what you suggest may not have worked, because what is "simple" is not rigorously defined.
I think what you want is the functionality present in .rewrite:
In [1]: (sinh(x)+cosh(x)).rewrite(exp)
Out[1]:
x
e
You can use .rewrite for many other transformations including gamma <-> combinatorics and inverse trig <-> logarithms.