Equation containing a matrix power - matrix

I have an equation on the form A^n*b =e= c where A is a matrix and b & c are column vectors.
n is a fixed number for my model determined by a constant. It will most likely be in the hundreds and be changed for different solutions.
A is a matrix of variables, b & c are constants.
How can I formulate A^n*b =e= c in gams?
Optionally: the model that lead me to this is that I have a graph with a connectivity matrix con(x,x2) denoting the connectivity between x and x2 when x and x2 are connected. I would like to calculate the connection between 2 arbitrary nodes, the connectivity between 2 nodes x to x2 is the sum of the connectivity for all paths from x to x2. the connectivity for a path is the product of all connections along the path. Is there a smarter way to formulate this constraint so that I don't have to do matrix exponentiation?
A is not symmetric or invertible but is positive Semidefinite.

You need to define your data in terms of sets and parameters first. Follow this link for more information about the data structure in GAMS: http://www.gams.com/latest/docs/userguides/userguide/_u_g__data_entry.html
Start by defining the sets and parameters of your problem, supposing you have 100 vertices, you can declare x like this for example:
Set x /x1*x100/;
alias(x,x2);
Because you will need to use the same set twice in your matrix, you have to define an alias so that x2 is interpreted by GAMS as the same as x in your model.
Then, declare n and b as parameters, you can do it like this:
Parameter
n /200/
c /100/;
Parameter b(x)
/
x1 3
x2 43
...
x100 23
/;
Note that parameters and variables with more than one value (i.e vectors or matrices) have to be defined over a previously defined set in GAMS. This is why b is defined over set x, think of x like indices of your vectors/matrices.
Declaration of A will have the form:
Variable A(x,x2);
Now you can define your equation using these sets, parameters and variables:
eq(x,x2) .. power(A(x,x2),n) * B(x2) =e= c;
Of course, you will still need to pick a suitable solver (NLP) and define an objective function, but this is how you would model the equation you want and variables for it.

Related

How can I bind the same variables together in prolog

So, I'm relatively new to prolog and was wondering if it is possible to bind 2 variables together in a matrix and would they update simultaneously.
For example, I have this
X = [[_,_], [_,_]].
X = [[_23838, _23844], [_23856, _23862]].
But, I was wondering is it possible to do something like this, almost unifying the diagonal elements in the matrix
X = [[_,_], [_,_]].
X = [[_23838, _23844], [_23856, _23838]].
Assuming the above is possible if _23838 was later bound then would they be the same value?
For example
X = [[5,_], [_,5]].
X = [[5, _23844], [_23856, 5]].
Yes you can.
Just name the variables instead of using the don't care name (aka. "the anonymous variable"), i.e. _:
X = [[A,B], [C,A]].
This expresses the constraint that the value at (1,1) must be the same as the value at (2,2).
You can also start off with "all variables different" and later force them to be equal by unification:
X = [[A,B], [C,D]], A=D
Conversely, you can state that you do not want to see equality in a result (all proofs that can only continue by making A and B equal will fail after dif/2):
X = [[A,B], [C,D]], dif(A,B).
dif/2 is of some interest.

Mathematica Does No Find the Global Maximum It Just Print Out My Function

I am am mathmatica notebook to find an analytical solution to the follow constrained optimization problem:
Max y^(1-b)(x^b(1-a(x/(x+1)))) s.t. M = Px+qy
x,y
I have tried the following code:
Maximize[{y^(1-b)(x^b(1-a(x/(x+1)))), M==Px+qy}, {x,y}]
and in returns the same function as an output. In the function a, b, M, P, and q are all parameters. I have also tried assigning the parameters arbitrary values to test to see if mathmatica is not sure how to deal with the parameters. I used to following code:
Maximize[{y^(1-0.5)(x^0.5(1-0.75(x/(x+1)))), 1000=5x+5y},{x,y}]
and it returns the same function. However, if I remove the constraint it will solve the optimization problem.
Maximize[{y^(1-0.5)(x^0.5(1-0.75(x/(x+1))))},{x,y}]
{7.2912*^59,{x->2.89727*^60,y->2.93582*^60}}
I am not sure what to do. After reading about constrained optimization problem the syntax appears to be correct. Sorry, it this question is really basic I am very new to mathmatica, also since I am using a notebook I could not past the output from the first two lines in.
The constraint is incorrectly specified, it should be 1000 == 5 x + 5 y. Maximize works better with exact numbers.
Maximize[{Rationalize[y^(1 - 0.5) (x^0.5 (1 - 0.75 (x/(x + 1))))],
1000 == 5 x + 5 y}, {x, y}] // N
(* {25.7537, {x -> 96.97, y -> 103.03}} *)

This is about relations on sets

Definition : A relation on set S and T is any subset of the Cartesian product S x T.
Example:
The "larger than" relation for real numbers is the set:
L = {(x, y) | x, y E R, x > y}
Here the part I don't understand it says :
"Here S and T both equal R".
What does it mean?
The elements of set S is equal R and also the elements of set T is eqaul to R.
so S and T is also equal?
The elements of set S is equal R and also the elements of set T is eqaul to R. so S and T is also equal?
Exactly. As both x and y are real numbers, they are taken from the very same set, therefore they are sharing the same domain.

Best way to do an iteration scheme

I hope this hasn't been asked before, if so I apologize.
EDIT: For clarity, the following notation will be used: boldface uppercase for matrices, boldface lowercase for vectors, and italics for scalars.
Suppose x0 is a vector, A and B are matrix functions, and f is a vector function.
I'm looking for the best way to do the following iteration scheme in Mathematica:
A0 = A(x0), B0=B(x0), f0 = f(x0)
x1 = Inverse(A0)(B0.x0 + f0)
A1 = A(x1), B1=B(x1), f1 = f(x1)
x2 = Inverse(A1)(B1.x1 + f1)
...
I know that a for-loop can do the trick, but I'm not quite familiar with Mathematica, and I'm concerned that this is the most efficient way to do it. This is a justified concern as I would like to define a function u(N):=xNand use it in further calculations.
I guess my questions are:
What's the most efficient way to program the scheme?
Is RecurrenceTable a way to go?
EDIT
It was a bit more complicated than I tought. I'm providing more details in order to obtain a more thorough response.
Before doing the recurrence, I'm having problems understanding how to program the functions A, B and f.
Matrices A and B are functions of the time step dt = 1/T and the space step dx = 1/M, where T and M are the number of points in the {0 < x < 1, 0 < t} region. This is also true for vector the function f.
The dependance of A, B and f on x is rather tricky:
A and B are upper and lower triangular matrices (like a tridiagonal matrix; I suppose we can call them multidiagonal), with defined constant values on their diagonals.
Given a point 0 < xs < 1, I need to determine it's representative xn in the mesh (the closest), and then substitute the nth row of A and B with the function v( x) (transposed, of course), and the nth row of f with the function w( x).
Summarizing, A = A(dt, dx, xs, x). The same is true for B and f.
Then I need do the loop mentioned above, to define u( x) = step[T].
Hope I've explained myself.
I'm not sure if it's the best method, but I'd just use plain old memoization. You can represent an individual step as
xstep[x_] := Inverse[A[x]](B[x].x + f[x])
and then
u[0] = x0
u[n_] := u[n] = xstep[u[n-1]]
If you know how many values you need in advance, and it's advantageous to precompute them all for some reason (e.g. you want to open a file, use its contents to calculate xN, and then free the memory), you could use NestList. Instead of the previous two lines, you'd do
xlist = NestList[xstep, x0, 10];
u[n_] := xlist[[n]]
This will break if n > 10, of course (obviously, change 10 to suit your actual requirements).
Of course, it may be worth looking at your specific functions to see if you can make some algebraic simplifications.
I would probably write a function that accepts A0, B0, x0, and f0, and then returns A1, B1, x1, and f1 - say
step[A0_?MatrixQ, B0_?MatrixQ, x0_?VectorQ, f0_?VectorQ] := Module[...]
I would then Nest that function. It's hard to be more precise without more precise information.
Also, if your procedure is numerical, then you certainly don't want to compute Inverse[A0], as this is not a numerically stable operation. Rather, you should write
A0.x1 == B0.x0+f0
and then use a numerically stable solver to find x1. Of course, Mathematica's LinearSolve provides such an algorithm.

Pseudo random number generator from two inputs

I need a pseudo random number generator that gives me a number from the range [-1, 1] (range is optional) from two inputs of the type float.
I'll also try to explain why I need it:
I'm using the Diamond-Square algorithm to create a height map for my terrain engine. The terrain is split into patches (Chunked LOD).
The problem with Diamond-Square is that it uses the random function, so let's say two neighbor patches are sharing same point (x, z) then I want the height to be the same for them all so that I won't get some crack effect.
Some may say I could fetch the height information from the neighbor patch, but then the result could be different after which patch was created first.
So that's why I need a pseudo number generator that returns an unique number given two inputs which are the (x, z).
(I'm not asking someone to write such function, I just need a general feedback and or known algorithms that do something similar).
You need something similar to a hash function on the pair (x, z).
I would suggest something like
(a * x + b * z + c) ^ d
where all numbers are integers, a and b are big primes so that the integer multiplications overflow, and c and d are some random integers. ^ is bitwise exclusive or. The result is a random integer which you can scale to the desired range.
This assumes that the map is not used in a game where knowing the terrain is of substantial value, as such a function is not secure for keeping it a secret. In that case you'd better use some cryptographic function.
If you're looking for a bijection from IRxIR -> [-1;1], I can suggest this:
bijection from IR to ]-a:a[
First let's find a bijection from IR-> ]-1;1[ so we just need to find a bijection from IRxIR->IR
tan(x): ]-Pi/2;Pi/2[ -> IR
arctan(x) : IR -> ]-Pi/2;Pi/2[
1/Pi*arctan(x) + 1/2: IR -> ]0;1[
2*arctan(x) : IR->]-Pi:Pi[
and
ln(x) : IR + -> IR
exp(x): IR -> R+
Bijection from ]0,1[ x ]0,1[ -> ]0,1[
let's write:
(x,y) in ]0,1[ x ]0,1[
x= 0,x1x2x3x4...xn...etc where x1x2x3x4...xn represent the decimals of x in base 10
y=0,y1y2y3y4...ym...etc idem
Let's define z=0,x1y1x2y2xx3y3....xnyn...Oym in ]0,1[
Then by construction we can provethere that it is exact bijection from ]0,1[ x ]0,1[ to ]0,1[.
(i'm not sure it's is true for number zith infinite decimals..but it's at least a "very good" injection, tell me if i'm wrong)
let's name this function : CANTOR(x,y)
then 2*CANTOR-1 is a bijection from ]0,1[ x ]0,1[ -> ]-1,1[
Then combining all the above assertions:
here you go, you get the bijection from IRxIR -> ]-1;1[...
You can combine with a bijection from IR-> ]0,1[
IRxIR -> ]-1;1[
(x,y) -> 2*CANTOR(1/Pi*arctan(x) + 1/2,1/Pi*arctan(y) + 1/2)-1
let's define the reciproque, we process the same way:
RCANTOR: z -> (x,y) (reciproque of CANTOR(x,y)
RCANTOR((z+1)/2): ]-1:1[ -> ]01[x ]0,1[
then 1/Pi*tan(RCANTOR((z+1)/2)) + 1/2 : z ->(x,y)
]-1;1[ -> IRxIR
Just pick any old hash function, stick in the binary description of the coordinates and use the output.

Resources