How do I do definite integrals in jupyter 6.01 - jupyter

x=symbols('x')
(print) integrate(.6*x, (x,pi/3,3*pi/2))
^
Syntaxerror:invalid syntax
Always gives me an error and not sure what I'm doing wrong. Error code is showing up for line 2. Using sympy.

I did this and it works
from sympy import *
x = symbols('x')
print(integrate(.6*x, (x, pi/3, 3*pi/2)))
If you do not want the decimal expression, you can put
print(integrate(Rational(6, 10)*x, (x, pi/3, 3*pi/2)))

Related

removing implicit bound placed on a linear programming problem

I am trying to use the linear programming function from pythons scipy library however I am unable to remove a non negativity constrain placed on the variable. To demonstrate this consider the following code.
from scipy.optimize import linprog
c = [-1]
A = [[1]]
b = [-3]
print(linprog(c, A_ub=A, b_ub=b, bounds=None))
it gives the following output:
fun: 3.0
message: 'Optimization failed. Unable to find a feasible starting point.'
nit: 0
status: 2
success: False
x: nan
This should be a formulation of the following problem: minimize c*x such that Ax≤b or equivalently minimize -1*x st. 1x≤-3. Hopefully I have done so correctly. Based on the current output I suspect that there is an additional constraint that x≥0. I don't know how to remove this constraint.
I have set the bounds to None with the understanding that this means no additional bounds are placed on the problem other than Ax≤b however there is clearly some other bound being placed on the problem. How can I remove this bound? Thanks
Your not the first one to be confused by this--the docstring should explain this better.
When you use bounds=None, you are telling linprog to use the default behavior, which is to assume the nonnegative constraint. It is only by setting bounds to something else that the default behavior is changed. In this case bounds=(None, None) will remove the bound on each variable.
In [40]: from scipy.optimize import linprog
In [41]: c = [-1]
In [42]: A = [[1]]
In [43]: b = [-3]
In [44]: print(linprog(c, A_ub=A, b_ub=b, bounds=(None, None)))
con: array([], dtype=float64)
fun: 3.0
message: 'The solution was determined in presolve as there are no non-trivial constraints.'
nit: 0
slack: array([0.])
status: 0
success: True
x: array([-3.])

How to convert symbol variable to iterable variable?

I am trying to minimize a function using scipy.optimize. Here is my programme and the last line is the error message.
import sympy as s
from scipy.optimize import minimize
x,y,z=s.symbols('x y z')
f= lambda z: x**2-y**2
bnds = ((70,None),(4,6))
res = minimize(lambda z: fun(*x),(70,4), bounds=bnds)
<lambda>() argument after * must be an iterable, not Symbol
How to convert symbol to an iterable or define an iterable directly ?
In Python, calling a function with f(*x) means f(x[0], x[1], ...). That is it expects x to a tuple (or other iterable), and the function should have a definition like
def f(*args):
<use args tuple>
I'm not quite sure what you are trying to do with the sympy code, or why you are using it instead of defining a function in Python/numpy directly.
A function like:
def f(z):
x,y = z # expand it to 2 variables
return x**2 - y**2
should work in a minimize call with:
minimize(f, (10,3))
which will vary x and y starting with (10,3) seeking to minimize the f value.
In [20]: minimize(f, (70,4), bounds=((70,None),(4,6)))
Out[20]:
fun: 4864.0
hess_inv: <2x2 LbfgsInvHessProduct with dtype=float64>
jac: array([ 139.99988369, -11.99996404])
message: b'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL'
nfev: 9
nit: 1
status: 0
success: True
x: array([ 70., 6.])

reduceByKey results in Infinity value

I am trying to add values for same key.
val final= d1.join(d2).flatMap(line => Seq(line.swap._1)).reduceByKey((x, y) =>(x+y))
d1 and d2 are data streams. After flatMap I get Key value pair.
However, it is resulting in Infinity value in this line reduceByKey((x, y) =>(x+y))
for example, if the pairs are (k1,1.0) (k1,1.0) the line reduceByKey((x, y) =>(x+y)) results in (k1,Infinity)
Any suggestion?
The above code snippet is working. As #maasg righty hinted the problem was elsewhere. The error was caused by division by zero in previous code which I didnt post here. Thanks!

Error running matlab code after compiling

It looks like this has been asked many times, but none of the past posts seem to solve my question. All those had to do with matrix/vector while my code does not have any of these, just simple variables. It takes three variables as arguments. It works perfectly fine within the Matlab environment. I only got the error when I compiled it with mcc -m Normal.m and tried to run with the executable like this "./Normal 1 5 0.5". The complete error message is:
Error using /
Matrix dimensions must agree.
Error in Normal (line 4)
MATLAB:dimagree
It is complaining about line 4: N=2/dt, what is wrong with this?
Here is the code:
function val=Normal(l1,l2,dt)
const=(l2/l1-1);
N=2/dt;
S=1.0/sqrt(l2/l1);
Z(1)=S;
for i=2:N
t= -1+(i-1)*dt;
Z(i)=1.0/sqrt(const*t*t+1);
S=S+2*Z(i);
end
Z(21)=1.0/(l2/l1);
S=S+1.0/sqrt(l2/l1);
val=dt*S/2;
end
But dt is not a scalar when passed into the standalone through the command ./Normal 1 5 0.5. It is a character array with 3 elements ('0', '.','5')!
When passing numerical arguments to a standalone, they are passed as strings. Thus, inside the function, you need to convert '0.5' into a double, and similarly for l1 and l2:
dt = str2num(dt);
l1 = str2num(l1);
l2 = str2num(l2);
Note that you can use isdeployed to determine at runtime if the function is a standalone:
if isdeployed, dt = str2num(dt); end
And you might need to display the result:
if isdeployed, disp(val); end
Result:
>> system('Normal 1 5 0.5');
1.4307
>> Normal(1,5,0.5) % .m function for comparison
ans =
1.4307

Using elementwise operation and element indexing together in Matlab

I want to write an anonymous function taking a vector theta as its input and compute the sum of the fourth-squared of the first half elements of theta:
L=#(theta) sum(theta.^4(1:length(theta)/2))
But Matlab reports an error
??? Error: File: c2n9.m Line: 3 Column: 27
Unbalanced or unexpected parenthesis or bracket.
I identified the error same as the following simpler example
>> a=ones(1,4)
a =
1 1 1 1
>> a.^4(1:2)
??? a.^4(1:2)
|
Error: Unbalanced or unexpected parenthesis or bracket.
>> (a.^4)(1:2)
??? (a.^4)(1:2)
|
Error: Unbalanced or unexpected parenthesis or bracket.
I wonder how I can make the simple example as well as the anonymous function work?
Thanks!
You could instead do
a(1:2).^4
you should do the indexing before the per-element raising to the power
instead of:
L=#(theta) sum(theta.^4(1:length(theta)/2))
try
L=#(theta) sum(theta(1:round(length(theta)/2)).^4)
note that I also added a round to take care of the case where the length of theta is odd
Err, aren't you missing a multiplication sign at the place that the first error message points to ? Or something else ?