Set all exponential terms to equal to zero in mathmatica - wolfram-mathematica

I have an expression where I want to set all exponential term to equal to zero at one time
example e^x+xx+tt+e^y+5*e^5, Is there is a direct way to set all e->zero and produce xx+tt?
Thanks in advance

e^x + xx + tt + e^y + 5*e^5 /. e^__ :> 0
(* tt + xx *)

Related

Mathematica Order of an Equation

Is there a way to obtain the order of an ODE in mathematica.
For example, if i have y''+5y i want mathematica return 2 (beacuse it's a 2nd order equation). So, is it possible what i'm asking?
here is a way to extract the value automatically:
ode = y'' + y' + y == 0 ;
Max[Cases[ ode , Derivative[n_][y] :> n , Infinity]]
2
note this just finds the largest derivative in the expression, it doesn't verify if the expression is actually an ode..

How can I get rid of irritating prefactor of one in Mathematica?

I am trying to refine algebraic expressions into a convenient form using Mathematica to make sure I don't drop a sign or make some other trivial slip. After a lot of swearing, I have come to accept that this is not a deterministic process and I will have to do it step by step and also that the algebraic manipulation palette is my friend. However, there is still one thing that's driving me nuts. Sometimes mathematica spits out expressions with these seemingly extraneous leading ones. For example, right now I'm looking at:
0.5*(1.*Log[-1.*a^2 + 1.*bigr^2] - 1.*Log[1.*a^2 - 2.*a*bigr + 1.*bigr^2])
when I would much rather be looking at:
0.5*(Log[-a^2 + bigr^2] - Log[a^2 - 2.*a*bigr + bigr^2])
It's more than just a cosmetic problem because it confuses Factor[] when I try to apply it to some of the obvious quadratic factorizations in the above expression. Any clean fixes?
your = 0.5*(1.*Log[-1.*a^2 + 1.*bigr^2] - 1.*Log[1.*a^2 - 2.*a*bigr + 1.*bigr^2])
your /. {1. -> 1, -1. -> -1}
(* -> 0.5 (Log[-a^2 + bigr^2] - Log[a^2 - 2. a bigr + bigr^2]) *)
The dot after the 1 tells mathematica to treat the number as a non-exact quantity. So for example
1. * 1/2
(* -> .5 *)
but
1 * 1/2
(* -> 1/2 *)
Use exact quantities in your calculations (1, 2, 1/2) instead of decimal numbers (1., 2. ,0.5) if you need exact results
expr = 0.5*(1.*Log[-1.*a^2 + 1.*bigr^2] - 1.*Log[1.*a^2 - 2.*a*bigr + 1.*bigr^2]);
Convert all approximate numbers to precise:
expr // Rationalize
1/2 (Log[-a^2 + bigr^2] - Log[a^2 - 2 a bigr + bigr^2])
Selectively:
expr /. x_ /; x == 1 -> 1
0.5 (Log[-1. a^2 + bigr^2] - 1. Log[a^2 - 2. a bigr + bigr^2])

Multiple calculations of one model in a table

I am a new user of Mathematica and I can't figure out how to solve this problem.
I have a computation S that gives me for 10 Random Variates 10 results:
Xi = RandomVariate[NormalDistribution[], 10]
Mu = -0.00644131
Sigma= 0.0562005
t = 0.1
s = 100
fnmc[s_,Mu_,Sigma_, t_,Xi_] := s Exp[(Mu - Sigma^2/2) t + Sigma Sqrt[t ] Xi]
S = fnmc[s, Mu, Sigma, t, Xi]
Now I need to compute formula S 10 times - so I'll have 100 numbers in result.
I can't find the way to do it in a TABLE. Further, I will have to sum those 10 results and calculate Mean etc. I wanted to use TABLE because of the further computation - SUM, MEAN - I thought it is the easiest "form" of results to work with....is it?
I had in mind something like:
Table[S(i),{i,10}]
but off course it multiplies S x (i). Any suggestions?
S(i) multiplies S with i. S[i] calls function S with parameter i.
The four kinds of bracketing in Mathematica
I just realized that S isn't a function at all, so you don't want to call it with parameter i. You can get the result of S 10 times simply by Table[S,{10}], but since Xi is only calculated once, this will just give you 10 times the same vector. Maybe you want to do the whole calculation 10 times? That would be:
Table[
(
Xi = RandomVariate[NormalDistribution[], 10];
Mu = -0.00644131;
Sigma = 0.0562005;
t = 0.1; s = 100;
s*Exp[(Mu - Sigma^2/2)*t + Sigma*Sqrt[t]*Xi]
), {10}]
You could use a functional programming approach map ( /# ) your function over the Xis you've created.
Mu = -0.00644131;
Sigma= 0.0562005;
t = 0.1;
s = 100;
(* if you wanted ten scalar random numbers, with each one used on one application of your equation *)
Xi = RandomVariate[NormalDistribution[], 10];
ans = s Exp[(Mu - Sigma^2/2) t + Sigma Sqrt[t ] #] & /# Xi;
(* if you wanted ten 10 dimensional random numbers, with each 10D number used on one application of your equation *)
Xi = RandomVariate[NormalDistribution[], {10,10}];
ans = s Exp[(Mu - Sigma^2/2) t + Sigma Sqrt[t ] #] & /# Xi;

Efficient algorithm to determine range [a, b] of sin wave with interval

I have a sine wave whose parameters I can determine (they are user-input). It's of the form y=a*sin(m*x + t)
I'd like to know whether anyone knows an efficient algorithm to figure out the range of y for a given interval which goes from [0, x] (x is again another input)
For example:
for y = sin(x) (i.e. a=1, t=0, m=1), for the interval [0, 4] I'd like an output like [1, -0.756802]
Please keep in mind, m and t can be anything. Thus, the y-curve does not have to start (or end) at 0 (or 1). It could start anywhere.
Also, please note that x will be discrete.
Any ideas?
PS: I'll use python for implementing the algorithm.
Since function y(x) = a*sin(m*x + t) is continuous, maximum will be either at one of the interval's ends or at the maximum inside interval, in this case dy/dx will be equal to zero.
So:
1. Find values of y(x) at the ends of interval.
2. Find out if dy/dx == a * m cos (mx + t) have zero(s) in interval, find out values of y(x) at the zero(s).
3. Choose point where y(x) have maximum value
If you have greater than one period then the result is just +/- a.
For less than one period you can evaluate y at the start/end points and then find any maxima between the start/end points by solving for y' = 0, i.e. cos(m*x + t) = 0.
All the answers are more or less the same. Thanks guys=)
I think I'd go with something like the following (note that I am renaming the variable I called "x" to "end". I had this "x" at the beginning which denoted the end of my interval on the X-axis):
1) Evaluate y at 0 and "end", use an if-block to assign the two values to the correct PRELIMINARY "min" and "max" of the range
2) Evaluate number of evolutions: "evolNr" = (m*end)/2Pi. If evolNr > 1, return [-a, a]
3) If evolNr < 1: First find the root of the derivative, which is at "firstRoot" = (1/2m)*Pi - phase + q * 1/m * Pi, where q = ceil(m/Pi * ((1/2m) * Pi - phase) ) --- this gives me the first root at some position x > 0. From then on I know that all other extremes are within firstRoot and "end", we have a new root every 1/m * Pi.
In code: for (a=firstRoot; a < end; a += 1/m*Pi) {Eval y at a, if > 0 its a maximum, update "max", otherwise update "min"}
return [min, max]

Google Code Jam 2008: Round 1A Question 3

At Google Code Jam 2008 round 1A, there is problem:
Calculate last three digits before the
decimal point for the number
(3+sqrt(5))^n
n can be big number up to 1000000.
For example: if n = 2 then (3+sqrt(5))^2 = 27.4164079... answer is 027.
For n = 3: (3+sqrt(5))^3 = 3935.73982... answer is 935.
One of the solution is to create matrix M 2x2 : [[0, 1], [-4, 6]] than calculate matrix P = M^n, Where calculation preformed by modulo 1000.
and the result is (6*P[0,0] + 28*P[0,1] - 1) mod 1000.
Who can explain me this solution?
I'll present a method to solve this problem without even understanding the solution.
Assuming that you are familiar with the fibonacci numbers:
ghci> let fib = 0 : 1 : zipWith (+) fib (tail fib)
ghci> take 16 fib
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]
And are also familiar with its closed form expression:
ghci> let calcFib i = round (((1 + sqrt 5) / 2) ^ i / sqrt 5)
ghci> map calcFib [0..15]
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]
And you notice the similarity of ((1 + sqrt 5) / 2)n and (3 + sqrt 5)n.
From here one can guess that there is probably a series similar to fibonacci to calculate this.
But what series? So you calculate the first few items:
ghci> let calcThing i = floor ((3 + sqrt 5) ^ i)
ghci> map calcThing [0..5]
[1,5,27,143,751,3935]
Guessing that the formula is of the form:
thingn = a*thingn-1 + b*thingn-2
We have:
27 = a*5 + b*1
143 = a*27 + b*5
We solve the linear equations set and get:
thingn = 4*thingn-1 + 7*thingn-2 (a = 4, b = 7)
We check:
ghci> let thing = 1 : 5 : zipWith (+) (map (* 4) (tail thing)) (map (* 7) thing)
ghci> take 10 thing
[1,5,27,143,761,4045,21507,114343,607921,3232085]
ghci> map calcThing [0..9]
[1,5,27,143,751,3935,20607,107903,564991,2958335]
Then we find out that sadly this does not compute our function. But then we get cheered by the fact that it gets the right-most digit right. Not understanding why, but encouraged by this fact, we try to something similar. To find the parameters for a modified formula:
thingn = a*thingn-1 + b*thingn-2 + c
We then arrive at:
thingn = 6*thingn-1 - 4*thingn-2 + 1
We check it:
ghci> let thing =
1 : 5 : map (+1) (zipWith (+)
(map (*6) (tail thing))
(map (* negate 4) thing))
ghci> take 16 thing == map calcThing [0..15]
True
Just to give an answer to a very old question:
Thanks to yairchu i've got the idea to reread the prove of Binet's formula on the wikipedia page. It's there not really that clear, but we can work with it.
We see on the wikipedia page there is a closed form with 'computation by rounding': Fn = ⌊φ/√5⌋n.
If we could replace the φ/√5 with 3 + √5 (call the latter x). We could compute the floor of xn fairly easily, especially mod 1000, by finding the nth term in our freshly constructed sequence (this is the analogon of F (later we will call this analogon U)).
What sequence are we looking for? Well, we'll try following the prove for the Binet's formula. We need a quadratic equation with x as a root. Let's say x2 = 6 x-4 this one has roots x and y := 3 - √5. The handy part is now:
Define Un (for every a and b) such:
Un = a xn + b yn
by definition of x and y you can see that
Un = 6 Un-1 - 4 Un-2
Now we can choose a and b freely. We need Un to be integers so I propose choosing a=b=1. Now is U0 = 2, U1 = 6, U2 = 28...
We still need to get our 'computation by rounding'. You can see that yn < 1 for every n (because y ≅ 0.76 < 1) so Un = xn + yn = ⌈xn⌉.
If we can compute Un we can find ⌊xn⌋, just subtract 1.
We could compute Un by it's recursive formula but that would require O(n) computation time. We can do better!
For computing such a recursive formula we can use matrices:
⌈ 0 1⌉ ⌈ U(n-1) ⌉ ⌈ U(n) ⌉
⌊-4 6⌋ ⌊ U(n) ⌋ = ⌊U(n+1)⌋
Call this matrix M. Now does M*(U(1), U(2)) compute (U(2), U(3)).
Now we can compute P = Mn-1 (notice that I use one less than n, you can see that this is right if you test the small cases: n=0, n=1, n=2) P*(6,28) gives us now the nth and (n+1)th term of our sequence so:
(P*(6,28))0 - 1 = ⌊xn⌋
Now we can take everything mod 1000 (this is simplifying the calculations (a lot)) and we get the desired result in computation time O(log(n)) (or even better with the computational wonders of powers of matrices (over a cyclic finite field)). This explains the very weird looking solution, I guess.
I don't know how to explain that, but the auther of the problem have compose this analysis.

Resources