I would like to test several values of intensity.
I need them to be spaced logarithmically from 1 to 1000. Yet I just use 1, 10, 100, 1000, but I would like to have more data point, let`s say 10.
How could I find 10 logarithmically spaced number between 1 and 1000 in Mathematica ?
If a is start, c is end and b is number of intervals:
{a, b, c} = {1, 10, 1000};
t = (c/a)^(1/b) // N
a*t^Range[b]
1.99526
{1.99526, 3.98107, 7.94328, 15.8489, 31.6228, 63.0957, 125.893, 251.189, 501.187, 1000.}
I used N just to see better, what do we have.
Here is one way:
In[11]:= base = Block[{a}, a /. NSolve[a^9 == 1000, a][[-1, 1]]]
Out[11]= 2.15443
In[13]:= base^Range[0, 9]
Out[13]= {1., 2.15443, 4.64159, 10., 21.5443, 46.4159, 100.,
215.443,464.159, 1000.}
EDIT
Here is a much shorter and more direct way to get the same:
In[18]:= N[10^Range[0, 3, 1/3]]
Out[18]= {1., 2.15443, 4.64159, 10., 21.5443, 46.4159, 100.,
215.443, 464.159, 1000.}
Solve the equation x ** 9 = 1000 -- then your numbers are: x ** 0, x ** 1, ... x ** 9.
note: where x ** y means x to the power of y
Related
I have a code where we first need to generate n + 1 numbers in a range with a given step. However, I don't understand how and why it works:
a = 2;
b = 7;
h = (b-a)/n;
x[0] = a;
Array[x, n+1, 0];
For[i = 0, i < n + 1, i++, x[i] = a + h*i]
My questions are:
Are elements of x automatically generated when accessed? There's no mention of x before the line x[0] = a
Shouldn't index access be like x[[i]]?
What exactly does Array do here? It isn't assigned to anything which confuses me
Try Range[2,10,2] for a range of numbers from 2 to 10 in steps of 2, etc.
Beyond that there some faults in your code, or perhaps in your understanding of Mathematica ...
x[0] = a defines a function called x which, when presented with argument 0 returns a (or a's value since it is previously defined). Mathematica is particular about the bracketing characters used [ and ] enclose function argument lists. Since there is no other definition for the function x (at least not that we can see here) then it will return unevaluated for any argument other than 0.
And you are right, doubled square brackets, ie [[ and ]], are used to enclose index values. x[[2]] would indeed refer to the second element of a list called x. Note that Mathematica indexes from 1 so x[[0]] would produce an error if x existed and was a list.
The expression Array[x, n+1, 0] does return a value, but it is not assigned to any symbol so is lost. And the trailing ; on the line suppresses Mathematica's default behaviour to print the return value of any expression you execute.
Finally, on the issue of the use of For to make lists of values, refer to https://mathematica.stackexchange.com/questions/7924/alternatives-to-procedural-loops-and-iterating-over-lists-in-mathematica. And perhaps ask further Mathematica questions at that site, the real experts on the system are much more likely to be found there.
I suppose I might add ... if you are committed to using Array for some reason ask another question specifically about that. As you might (not) realise, I recommend not using that function to create a list of numbers.
From the docs, Array[f, n, r] generates a list using the index origin r.
On its own Array[x, n + 1, 0] just produces a list of x functions, e.g.
n = 4;
Array[x, n + 1, 0]
{x[0], x[1], x[2], x[3], x[4]}
If x is defined it is applied, e.g.
x[arg_] := arg^2
Array[x, 4 + 1, 0]
{0, 1, 4, 9, 16}
Alternatively, to use x as a function variable the Array can be set like so
Clear[x]
With[{z = Array[x, n + 1, 0]}, z = {m, n, o, p, q}]
{x[0], x[1], x[2], x[3], x[4]}
{m, n, o, p, q}
The OP's code sets function variables of x in the For loop, e.g.
Still with n = 4
a = 2;
b = 7;
h = (b - a)/n;
For[i = 0, i < n + 1, i++, x[i] = a + h*i]
which can be displayed by Array[x, n + 1, 0]
{2, 13/4, 9/2, 23/4, 7}
also x[0] == 2
True
The same could be accomplished thusly
Clear[x]
With[{z = Array[x, n + 1, 0]}, z = Table[a + h*i, {i, 0, 4}]]
{2, 13/4, 9/2, 23/4, 7}
Note also DownValues[x] shows the function definitions
{HoldPattern[x[0]] :> 2,
HoldPattern[x[1]] :> 13/4,
HoldPattern[x[2]] :> 9/2,
HoldPattern[x[3]] :> 23/4,
HoldPattern[x[4]] :> 7}
Let's say I have a constant matrix A and I want to compute pow(A, n). As described in this question I can calculate its eigenvalue decomposition (or more generally, its invariant subspaces and the generalized modal matrix) to speed up the process.
If A is a square matrix of size k, then the algorithm has complexity O(k log n) via exponentiation by squaring, and a preparation cost (to compute the modal matrix) of O(k^3).
The problem I am thinking about is loss of precision. Calculating eigenvalues et al takes us out of the domain of integers into floating point numbers. Even though in the end, we know that pow(A, n) has to have all integer entries, the algorithm outlined above only computes floating point numbers.
Another way is to exploit only exponentiation by squaring but that gives us only a O(k^3 log n) algorithm.
Is there a way to accurately - without converting to floating point numbers - compute pow(A, n) fast?
Eigenvalue decomposition is also possible for a matrix over a finite field, but only if the field is just right. So it not just takes preprocessing to do the eigenvalue decomposition, but also to find (some) finite field(s) over which that is even possible.
Finding multiple solutions is useful to avoid having work with gigantic finite fields, then compute pow(A, n) in some small fields and use the CRT to work out what the solution would have been in ℤ. But this requires somehow having a sufficient number of fields of sufficient size to work with and you wouldn't really know in advance what will be sufficient (there is always some n above which it stops working), so maybe this all won't work in practice.
As a small example, take:
A = [[1, 1],
[1, 0]]
Characteristic x² - x - 1, let's guess that modulo 1009 will work (it does), then there are roots 383 and 627, so:
A = QDP mod 1009
Q = [[ 1, 1],
[382, 626]]
D = [[383, 0],
[ 0, 627]]
P = [[ 77, 153],
[933, 856]]
So for example
pow(A, 15) = Q [[928, 0], P = [[987, 610],
[ 0, 436]] [610, 377]]
Fibonacci numbers as expected, so it all worked out. But with just 1009 as the modulus, going above 15 for the exponent makes the result not match would it would be in ℤ, then we would need more/bigger fields.
Using the Cayley-Hamilton theorem we can be faster. The theorem states that every matrix power for dimension k can be written as a sum of the first k powers of A.
If we know that, we can use exponentiation by squaring but instead of working on matrices we work on polynomials over A with coefficients in ℤ. We can then, after each step reduce the polynomial by the characteristic polynomial.
As a small example:
A = [[1, 1],
[1, 0]]
A^2 = A + 1 = writing poly. coefficients = {1, 1}
pow(A, 15) = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
= {1, 0} * ({1, 0} * ({1, 0} * {1, 0}^2)^2)^2
= {1, 0} * ({1, 0} * ({1, 0} * {1, 0, 0})^2)^2
= {1, 0} * ({1, 0} * ({1, 0} * {1, 1})^2)^2
= {1, 0} * ({1, 0} * ({1, 1, 0})^2)^2
= {1, 0} * ({1, 0} * {2, 1}^2)^2
= {1, 0} * ({1, 0} * {4, 4, 1})^2
= {1, 0} * ({1, 0} * {8, 5})^2
= {1, 0} * ({8, 5, 0})^2
= {1, 0} * {13, 8}^2
= {1, 0} * {169, 208, 64}
= {1, 0} * {377, 233}
= {377, 233, 0}
= {610, 377}
= [[987, 610],
[610, 377]]
So, what is the runtime cost? Trivially O(k^2 * log n) because at each squaring step we need to compute the square of two polynomials and reduce by the char. polynomial. Using a similar trick as #harold in the other answer yields O(k log k log n) by using the discrete fourier polynomial multiplication as we can find primitive roots.
It occurred to me today that I have forgotten almost all of my high school math, so I need some assistance working out some basic math in Ruby.
Essentially, I have a set of values from 1 to 10. Right now, I simply multiply them by 10 to get a figure I use as a percentage. eg 2 = 20%, 4 = 40% etc.
This is a little too linear and so what I really need to do is calculate a exponential decay style value, where by values closer down to 1 are given a more generous percentage and numbers closer up to 9 and 10 the percentage flattens out.
There is a possibility that there may be more than 10 numbers in the future too, so for example, 1 to 20. Im assuming this is simply enough to change?
Here is a quick chart that describes what im trying to do.
Can anyone help me figure this one out? Preferably in Ruby but otherwise any pseudo code would help.
Another curve that may suit you is square/squareroot/parabola
> 11.times{|x| puts "#{x}, #{100-(10-x)**2}"}
0, 0
1, 19
2, 36
3, 51
4, 64
5, 75
6, 84
7, 91
8, 96
9, 99
10, 100
To change the scale to 20, use
> 21.times{|x| puts "#{x}, #{100-(10-x/2.0)**2}"}
First, you need to have the probability distribution function, say F(x). It doesn't need to be normalized. In your case (exponential decay), this function could be
F(x) = e^(-kx)
You can tune k to adjust how fast it decays.
Then calculate the range of F(x) over your domain.
Then work out its inverse function G(x), which is
G(x) = -(1/k)ln(x)
Then you can do sampling by G(rand(range of F))
Here's my ruby code:
k = 1
f = -> x {Math.exp(-k * x)}
l, h = f.call(10), f.call(1)
g = -> x {-Math.log(x) / k}
r = -> {rand(h - l) + l}
samples = 10_000.times.map{g.call(r.call)}
samples.count{|x| x < 1} #=> 6327
samples.count{|x| x < 2} #=> 8620
samples.count{|x| x < 3} #=> 9477
samples.count{|x| x < 4} #=> 9809
samples.count{|x| x < 5} #=> 9929
samples.count{|x| x < 6} #=> 9970
samples.count{|x| x < 7} #=> 9986
samples.count{|x| x < 8} #=> 9994
samples.count{|x| x < 9} #=> 9997
samples.count{|x| x < 10} #=> 10000
Example piecewise wise function:
f[x_]:=Piecewise[{{x^2, 0<x<1-epsilon},{x,1<x<2-epsilon},{2,x>2}}]
Is there a way to connect these parts in interval epsilon, so I get a smooth function?
EDIT: By smooth, I don't mean it needs to be derivable in point of connection, just that in some numerical work it looks like a "natural" connection.
EDIT2:
Two black circles represent the points where lies the problem. I'd like it to look like a derivable function (although it doesn't need to be in rigor mathematical sense, but I don't want these two spikes). Red circle represents the part where it looks good.
What I could do is do this by nonlinear fitting the [x-epsilon, x+epsilon], but I was hoping that there was an easier way with piecewise function.
At first, given a function we should define it precisely on the whole range {x,0,2}, ie. its values on ranges 1-epsilon <= x < 1 and 2 - epsilon <= x < 2.
The easiest way is to define f1[x] piecewise linear on the both ranges, however the resulting function wouldn't be differentiable on the gluing points, and it would involve spikes.
To prevent such a situation we should choose (in this case) at least third order polynomials there:
P[x_] := a x^3 + b x^2 + c x + d
and glue them together with f[x] assuming "gluing conditions" (equality of functions at given points as well as of their first derivatives) ie. solve resulting equations :
W[x_, eps_]:= P[x]//. Flatten#Solve[{#^2 == P[#],
1 == P[1],
2# == 3a#^2 +2b# +c,
1 == 3a +2b +c}, {a, b, c, d}]&#(1-eps)
Z[x_, eps_]:= P[x]//. Flatten#Solve[{# == P[#],
2 == P[2],
1 == 3a#^2 +2b# +c,
0 == 12a +4b +c}, {a, b, c, d}]&#(2-eps)
To visualise the resuls we can take advantege of Manipulate :
f1[x_, eps_]:= Piecewise[{{x^2, 0 < x < 1 -eps}, {W[x, eps], 1 -eps <= x < 1},
{ x , 1 <= x < 2 -eps}, {Z[x, eps], 2 -eps <= x < 2},
{ 2 , x >=2}}];
Manipulate[ Plot[f1[x, eps], {x, 0, 2.3},
PlotRange -> {0, 2.3}, ImageSize->{650,650}]
//Quiet, {eps, 0, 1}]
Depending on epsilon > 0 we get differentiable functions f1, while for epsilon = 0 f1 is not differentiable at two points.
Plot[f1[x, eps]/. eps -> .4, {x, 0, 2.3}, PlotRange -> {0, 2.3},
ImageSize -> {500, 500}, PlotStyle -> {Blue, Thick}]
If we wanted f1 to be a smooth function (infinitely differentiable) we should play around defining f1 in range [1 - epsilon <= x < 1) with a transcendental function, something like for example Exp[1/(x-1)] etc.
You could do a gradually change between the functions that define the begin and end point of the interval. Below I do this by shifting the weight in the weighted sum of these functions depending on the position in the interval:
ClearAll[f]
epsilon = 0.1;
f[x_] :=
Piecewise[
{
{x^2, 0 < x < 1 - epsilon},
{Rescale[x, {1 - epsilon, 1}, {1, 0}] x^2 + Rescale[x, {1 - epsilon, 1}, {0, 1}] x,
1 - epsilon <= x <= 1},
{x, 1 < x < 2 - epsilon},
{Rescale[x, {2 - epsilon, 2}, {1, 0}] x + Rescale[x, {2 - epsilon, 2}, {0, 1}] 2,
2 - epsilon <= x <= 2},
{2, x > 2}
}
]
Plot[f[x], {x, 0, 2.5}]
I am not sure I understand your question, but from what I gather here is an idea
ClearAll[f]
e = 0.1
f[x_] := Piecewise[{{x^2, 0 < x < 1 - e}, {whatEver,
1 - e <= x <= 1 + e}, {x, 1 + e < x < 2}, {2, x > 2}}, error]
f[1] the gives whatEver.
This is an example. I want to know if there is a general way to deal with this kind of problems.
Suppose I have a function (a ε ℜ) :
f[a_, n_Integer, m_Integer] := Sum[a^i k[i],{i,0,n}]^m
And I need a closed form for the coefficient a^p. What is the better way to proceed?
Note 1:In this particular case, one could go manually trying to represent the sum through Multinomial[ ], but it seems difficult to write down the Multinomial terms for a variable number of arguments, and besides, I want Mma to do it.
Note 2: Of course
Collect[f[a, 3, 4], a]
Will do, but only for a given m and n.
Note 3: This question is related to this other one. My application is different, but probably the same methods apply. So, feel free to answer both with a single shot.
Note 4:
You can model the multinomial theorem with a function like:
f[n_, m_] :=
Sum[KroneckerDelta[m - Sum[r[i], {i, n}]]
(Multinomial ## Sequence#Array[r, n])
Product[x[i]^r[i], {i, n}],
Evaluate#(Sequence ## Table[{r[i], 0, m}, {i, 1, n}])];
So, for example
f[2,3]
is the cube of a binomial
x[1]^3+ 3 x[1]^2 x[2]+ 3 x[1] x[2]^2+ x[2]^3
The coefficient by a^k can be viewed as derivative of order k at zero divided by k!. In version 8, there is a function BellY, which allows to construct a derivative at a point for composition of functions, out of derivatives of individual components. Basically, for f[g[x]] and expanding around x==0 we find Derivative[p][Function[x,f[g[x]]][0] as
BellY[ Table[ { Derivative[k][f][g[0]], Derivative[k][g][0]}, {k, 1, p} ] ]/p!
This is also known as generalized Bell polynomial, see wiki.
In the case at hand:
f[a_, n_Integer, m_Integer] := Sum[a^i k[i], {i, 0, n}]^m
With[{n = 3, m = 4, p = 7},
BellY[ Table[{FactorialPower[m, s] k[0]^(m - s),
If[s <= n, s! k[s], 0]}, {s, 1, p}]]/p!] // Distribute
(*
Out[80]= 4 k[1] k[2]^3 + 12 k[1]^2 k[2] k[3] + 12 k[0] k[2]^2 k[3] +
12 k[0] k[1] k[3]^2
*)
With[{n = 3, m = 4, p = 7}, Coefficient[f[a, n, m], a, p]]
(*
Out[81]= 4 k[1] k[2]^3 + 12 k[1]^2 k[2] k[3] + 12 k[0] k[2]^2 k[3] +
12 k[0] k[1] k[3]^2
*)
Doing it this way is more computationally efficient than building the entire expression and extracting coefficients.
EDIT The approach here outlined will work for symbolic orders n and m, but requires explicit value for p. When using it is this circumstances, it is better to replace If with its Piecewise analog, e.g. Boole:
With[{p = 2},
BellY[Table[{FactorialPower[m, s] k[0]^(m - s),
Boole[s <= n] s! k[s]}, {s, 1, p}]]/p!]
(* 1/2 (Boole[1 <= n]^2 FactorialPower[m, 2] k[0]^(-2 + m)
k[1]^2 + 2 m Boole[2 <= n] k[0]^(-1 + m) k[2]) *)