How do you create an array of (variable)matrices A1,..,AN of semi definite matrices in Julia Jump where N is a parameter? #variable(model,x[1:N]) won't work because this is an array of variables, not of matrix variables.
Thanks in advance.
It's best to use the anonymous variable syntax here.
#variable(m, [1:N,1:N], SDP)
returns one matrix N x N of variables which is symmetric and PSD constrained. If you want a collection of K of them, just make one:
A = [#variable(m, [1:N, 1:N], SDP) for k in 1:K]
Related
I am in the following situation:
S=QQ[x_0..x_n];
for i from 0 to n do for j from i to n do d_{i,j} = x_i*x_j;
Now I would like to construct a vector whose elements are
d_{0,0}=x_0^2,d_{0,1}=x_0*x_1,...,d_{0,n}=x_0*x_n,d_{1,1}=x_1^2,d_{1,2}=x_1*x_2,...,d_{n,n}=x_n^2
How can I do this in MacAulay2? Thank you very much.
This may be what you are looking for.
m=ideal(S_*)
m^2_*
The _* operator gets the generators of an ideal. So, m is the maximal ideal, and you are looking for the generators of m^2.
Alternatively
flatten entries basis(2,S)
which simply gives you the vector basis of the ring S in degree 2.
In Macaulay2, vector refers to a column vector, and if we have vector elements, we can construct the following vector:
SQ= for i from 0 to n list d_{i}
vector(SQ)
But since the vector you want is not a column vector, it's best to make a matrix:
d=mutableMatrix genericMatrix(S,n,n)
for i from 0 to n do for j from 0 to n do d_(i,j)=x_i*x_j
Having a bit of trouble generating an NxN matrix in Mathematica. Given the value of N, I need to construct the NxN matrix that looks like the following:
N = Input["Enter value for N:"];
matrix = ConsantArray[0,{N,N}];
Do[matrix[[i,j]] = **"???"** ,{i,N}, {j,N}]
matrix // Matrix Form
Not sure in what should go as my statement in Do-Loop. Any help would appreciate it.
You could create a 1D array [1 ... n2] and then reshape or partition it to a matrix.
matrix = ArrayReshape[Range[n^2], {n, n}]
(* also works: *)
matrix = Partition[Range[n^2], n]
a couple more ways.
matrix=Table[j+(i-1) n,{i,n},{j,n}]
matrix=Array[#2+(#1-1) n &,{n,n}]
the Table form should give a clue how to fix your Do as well, but that's usually a poor approach performance-wise.
do not use capital N by the way its a reserved symbol.
I'm looking for a possibility to specify matrix quantities that depend on a variables. For scalars that works as follows, using undefined functions:
from sympy import *
x = Function('f')(t)
diff(x,t)
For Matrix Symbols like
x = MatrixSymbol('x',3,3)
i cannot find an equivalent. There is
i,j = Symbols('i j')
x = FunctionMatrix(6,1,Lambda((i,j),f))
but this is not what i need as you need to specify the contents of the matrix. The context is that i have equations
which should be derived in time and contain matrix valued elements.
I cannot deal with the elements of the matrices one by one.
Thanks!
I'm not sure about what you want, but I think you want to make a Matrix with differentiable elements. In that case, see if this works for you.
Create a matrix with function elements:
X = sym.FunctionMatrix(6,1,lambda i,j:sym.Function("x_%d%d" % (i,j))(t))
M = sym.Matrix(X)
M.diff(t)
This results in
Matrix([
[Derivative(x_00(t), t)],
[Derivative(x_10(t), t)],
[Derivative(x_20(t), t)],
[Derivative(x_30(t), t)],
[Derivative(x_40(t), t)],
[Derivative(x_50(t), t)]])
You may then replace stuff as you need.
Also, it may be preferrable if you populate the matrix with the expressions you need before differentiating. Leaving them as undefined functions may make it harder for you to simplify after substitution.
I have a sparse matrix and I need to fill certain entries with a specific value, I am using a for loop right now but I know its not the correct way to do it so I was wondering if its possible to vectorise this for loop?
K = sparse(N);
for i=vectorofrandomintegers
K(i,i) = 1;
end
If I vectorise it normally as so:
K(A,A) = 1;
then it fills all the entries in each row denoted by A whereas I want individual entries (i.e. K(1,1) = 1 or K(6,6)=1).
Also, the entries are not diagonally adjacent so I can't plop the identity matrix into it.
If you are going to use a vectorized method, you would need to get the linear indices to be set. The issue is that if you define your sparse matrix as K = sparse(N) and then linearly index into K, it would extend the size of it in one direction only and not along both row and column. Thus, you need to specify to MATLAB that you are
looking to use this sparse to store a 2D array. Thus, it would be -
K = sparse(N,N);
Get the linear indices to index into K using sub2ind and set them -
ind1 = sub2ind([N N],vectorofrandomintegers,vectorofrandomintegers);
K(ind1) = 1;
It's fairly simple
i'd use
K((A-1)*N+A))=1;
i believe that should fix your problem by treating the matrix as a vector
Instead of declaring and then filling a sparse matrix, you can fill it at the same time you define it:
i = vectorofrandomintegers; j = i;
K = sparse(i,j,1,N,N)
I'm looking for a hash function over sets H(.) and a relation R(.,.) such that if A is included in B then R(H(A), H(B)). Of course, R(.,.) must be easy to verify (constant time), and H(A) should be computed in linear time.
One example of H and R is:
H(A) = OR over 1 << (h(x) % k), for x in A, k a fixed integer and h(x) a hash function over integers.
R(H(A), H(B)) = ((H(A) & H(B)) == H(A))
Are there any other good examples? (good is hard to define but intuitively if R(H(A), H(B)) then whp A is included in B).
After thinking about this, I ended up with the example you gave. I.e. each element in B sets a bit in the hash, and A is only contained in B if each bit which is set in H(A) is also set in H(B).
Maybe a Bloom filter is applicable in your case. It seems to use the same bit trick, but with multiple hash functions.