how to extract a submatrix from a bigger matrix in fortran? - matrix

Now, I have a N*N matrix, mat. I also have
maskmat=(a(1),a(2),...,a(i),...a(N)).
a(i) equals 0 or 1.
If a(i)==1, the i-th colomn and i-th row of the matrix mat should be removed. If a(i)==0, we don't make any changes. Thus, we can get a submatrix accoring to the rule maskmat.
How to achieve it in Fortran?

One approach is to use vector subscripts where the subscripts come from the condition.
To get the indices of the rows and columns to be retained:
integer, allocatable :: idx(:)
idx = PACK([(i, i=1,N)], maskmat.eq.0)
and then for the matrix with bits removed
integer, allocatable :: submat(:,:)
submat = mat(idx, idx)

Related

How can I make all rows of matrix sum 1 from tensor in pytorch

I have a matrix, which is from Cora.
The size of it is [2708,1433]
For any rows, the elements are 1 or 0. I want to make "sum of elements of any rows be 1, by dividing sum of rows."
How can I make it? At first I thought I can do it by 'for' and 'append' command.
Is there any easier way?
xs = xs / xs.sum(dim=-1).unsqueeze(-1)
If xs is your Tensor, xs.sum(dim=-1) is the summation over the column-index (i.e. a Tensor of shape (2708,). By unsqueezing it, you turn it into a matrix of shape (2708, 1) which you can then broadcast against xs. The result of the division
is a matrix, all rows of which sum to 1:
xs.sum(dim=1)
assert torch.allclose(torch.ones(xs.shape[0], dtype=float), xs.sum(dim=1))
ps: if xs is ones and zeros, you might need to cast it to float first:
xs = xs.to(float)

Constructing a vector from a sequence in MacAulay2

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

vectorization of a single loop in matlab (multiplication and then addition)

I have a nX2 matrix A and a 3D matrix K. I would like to take element-wise multiplication specifying 2 indices in 3rd dimension of K designated by each row vector in A and take summation of them.
For instance of a simplified example when n=2,
A=[1 2;3 4];%2X2 matrix
K=unifrnd(0.1,0.1,2,2,4);%just random 3D matrix
L=zeros(2,2);%save result to here
for t=1:2
L=L+prod(K(:,:,A(t,:)),3);
end
Can I get rid of the for loop in this case?
How's this?
B = A.'; %'
L = squeeze(sum(prod(...
reshape(permute(K(:,:,B(:)),[3 1 2]),2,[],size(K,1),size(K,2)),...
1),...
2));
Although your test case is too simple, so I can't be entirely sure that it's correct.
The idea is that we first take all the indices in A, in column-major order, then reshape the elements of K such that the first two dimensions are of size [2, n], and the second two dimensions are the original 2 of K. We then take the product, then the sum along the necessary dimensions, ending up with a matrix that has to be squeezed to get a 2d matrix.
Using a bit more informative test case:
K = rand(2,3,4);
A = randi(4,4,2);
L = zeros(2,3);%save result to here
for t=1:size(A,1)
L = L+prod(K(:,:,A(t,:)),3);
end
B = A.'; %'
L2 = squeeze(sum(prod(reshape(permute(K(:,:,B(:)),[3 1 2]),2,[],size(K,1),size(K,2)),1),2));
Then
>> isequal(L,L2)
ans =
1
With some reshaping magic -
%// Get sizes
[m1,n1,r1] = size(K);
[m2,n2] = size(A);
%// Index into 3rd dim of K; perform reductions and reshape back
Lout = reshape(sum(prod(reshape(K(:,:,A'),[],n2,m2),2),3),m1,n1);
Explanation :
Index into the third dimension of K with a transposed version of A (transposed because we are using rows of A for indexing).
Perform the prod() and sum() operations.
Finally reshape back to a shape same as K but without the third dimension as that was removed in the earlier reduction steps.

Generating a random matrix with non-static constraints

I would like to generate a random matrix with constraints on both rows and columns in MATLAB. But the problem is I have two parameters for this constraints which are not fix for each element. For explanation, consider the mxn matrix P = [P1 ; P2; ...; Pm], and 2 other vectors lambda and Mu with m and n elements, respectively.
Consider lambda as [lambda(1), lambda(2), ..., lambda(m)] and Mu as [Mu(1), Mu2, ..., Mu(n)]
lamda and Mu should have this constraints:
sum of lambda(s) < sum of Mu(s).
,Now for the random matrix P:
each element of the matrix(P[j,i]) should be equal or greater than zero.
sum of the elements of each row is equal to one (i.e. for the row of j: sigma_i(P[j,i] = 1)
for each column j, sum of the production of each element with the correspond lambda(j) is less than the correspond element in the Mu vector (i.e.Mu(i)). i.e. for the column of i: sigma_j(P[j,i]*lambda(j)) < Mu(i)
I have tried coding all these constraints but because the existence of lambda and Mu vectors, just one of the constraints of 3 or 4 can be feasible. May you please help me for coding this matrix.
Thanks in advance
There could be values of Mu and Lambda that does not allow any value of P[i,j].
For each row-vector v:
Constraint 3 means the values are constrained to the hyper-plane v.1 = 1 (A)
Constraint 4 means the values are constrained to the half-space v.Lambda < m (H), where m is the element of Mu corresponding to the current row.
Constraint 1 does not guarantee that these two constraint generates a non-empty solution space.
To verify that the solution-space is non-empty, the easiest method is by checking each corner of hyper-plane A (<1,0,0,...>, <0,1,0,...>, ...). If at least one of the corners qualify for constraint 4, the solution-space is non-empty.
Having said that; Assuming the solution-space is non-empty, you could generate values matching those constraints by:
Generate random vector with elements 0 ≤ vi ≤ 1.
Scale by dividing by the sum of the elements.
If this vector does not qualify for constraint 4, repeat from step 1.
Once you have n such vectors, combine them as rows into a matrix.
The speed of this algorithm depends on how large volume of hyper-plane A is contained inside the half-space H. If only 1% is contained, it would expected to require 100 iterations for that row.

mathematica help ~ assign each element in a matrix to a value from a column vector

I have this 4x4 square matrix A, which has a random value in each element. I now have a column matrix (16x1) B which also has random values. The number of values in B is 16 which corresponds to the total number of elements in A.
I am trying to assign the values in B to elements in matrix A in the following way:
A[[1,1]] = B[[1]],
A[[1,2]] = B[[2]],
A[[1,3]] = B[[3]],
A[[1,4]] = B[[4]],
A[[2,1]] = B[[5]],
A[[2,2]] = B[[6]],
A[[2,3]] = B[[7]],
A[[2,4]] = B[[8]],
etc...
Does anyone know a convenient way of doing this so that I can achieve this for any NxN square matrix, and any length M column matrix(Mx1 matrix)? Assuming of course that the total # of elements are the same in both matrices.
If you have Mathematica 9, the function ArrayReshape can turn your list B into an arbitrary m x n Matrix.

Resources