Convert a vector of positive/negative elements to all positive elements in Julia? - matrix

Given a one dimensional vector in Julia with positive and negative entries, like A=[1;-3;5;-7], is there any function or command that can alter this vector so that its elements all become positive, so that it becomes A=[1;3;5;7]?

Vectorize over abs:
julia> abs.(A)
4-element Vector{Int64}:
1
3
5
7
Any function in Julia can work over an array by just appending a dot . to its name.

Related

Multiply matrix by vector rowwise (sweep)

Does STAN provide a method for multiplying each row of a matrix by a vector, elementwise? i.e. if I had a matrix:
[1,2,3,
4,5,6]
and a vector:
[2,4,6]
the desired result would be a second matrix:
[2,8,18,
8,15,36]
I'm sure I can do this as a for loop, but it seems like something I should be able to do without it.
Stan has an elementwise multiplication operator: .*. It applies only to objects of the same type (e.g., two vectors, or two matrices). But we can use the rep_matrix() broadcast function to turn the vector into a matrix:
my_matrix .* rep_matrix(my_vector', rows(my_matrix))
If the vector is already a row vector in Stan, then the transposition is unnecessary:
my_matrix .* rep_matrix(my_row_vector, rows(my_matrix))

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

Pairwise matching of tiles

Recently in a coding competition I came across this question.
We have a 1000 tiles where each tile is a 3x3 matrix. Each cell in the
matrix has an integer value from 0 to 9 which signifies the elevation
of the cell. The problem was to find the maximum pairs of tiles such
that they fit in perfectly. The tiles may be rotated to fit in. By fit
in it means that for tile A and tile B
A[i]+B[i]=const for i=0 to 8
The approach I thought for this problem was that I could maintain a hash value corresponding to each tile. Then I would find the possible combinations of tiles that would be
a possible fit and look it up in the hashtable.
Ex. For the tile below
5 3 2 4 6 7 5 7 8
4 8 9 matches with 5 1 0 for const = 9 & with 6 2 1 for const=10
1 4 5 8 5 4 9 6 5
for this tile the 'const' would range from 9(adding 0 to the maximum element) to 10(adding 9 to the minimum element).
So I would get two possible combinations for tiles which i would look up in the table.
But this method is greedy and does not give the desired answer and also I was unable to think of a proper hash function which would consider of all possible rotations.
So what would be a good approach for solving this problem?
I am sure there is a brute force way to solve this problem but I was actually wondering whether a viable solution to the problem exists on the lines of "pairwise equal to k" problem.
For n=1000 I would stick with the O(n^2) brute force solution. However an O(n log n) algorithm is described below.
The lexicographicalish ordering is defined by the following less-than operator:
Given two matrices M1, M2, define M1' as M1 if M1[1] is positive and -M1 if M1[1] is negative, and likewise or M2'. We say that M1<M2 if M1'[1]<M2'[1], or if M1'[1] == M2'[1] and M1'[2] < M2'[2], or if M1'[1] == M2'[1] and M1'[2] == M2'[2] and M1'[3] < M2'[3] etc.
Subtract the middle element of each matrix from the rest of the elements of the matrix i.e. A'[5] = A[5] and A'[i] = A[i] - A[5]. Then A' fits with B' if A'[i] +B'[i] = 0 for i!=5, and the elevation is A'[5] + B'[5].
Create an array of matrices and a dictionary. Rotate each matrix so that the top left corner has minimal absolute value before adding it to the array. If there are multiple corners with the same absolute value then duplicate the matrix and store both rotations in the array.
If some rotation of a matrix fits with itself and i,j are indices of rotations of this matrix, add the key-value pairs (i,j) and (j, i) to the dictionary.
Create an array S of indices 1,2... and sort S using the lexicographicalish ordering.
Instead of needing O(n^2) operations to check all possible pairs of matrices, it is only necessary to check all pairs of matrices with indices are S_i and S_(i+1). If a pair of matrices fits, use the dictionary to check that the two matrices are not rotations of the same original matrix before calculating the elevation of the pair.
Not sure if this is the most efficient way for doing this, but it sure works.
What I would do is:
Go over all tiles and check the maximum and minimum value of each tile and save it in a different array.
Check all possible pairs.
If min(A) + max(B) == min(B) + max(A) then check if some rotation of B fits perfectly on A. If it does, add 1 to your count.
Else, it does not fit so you can skip the checking for this pair.
Note: The reason for saving both maximum and minimum for each tile is that it might save us unnecessary calculations and checking rotations as in O(1) we can check if it doesn't fit.

Multiplying matrices of same dimensions returning error [duplicate]

This question already has answers here:
How to multiply two rows or columns?
(2 answers)
Closed 6 years ago.
Why cannot multiply two matrices of same dimensions (1x3) ? :
>> a = [1 1 1]
a =
1 1 1
>> b = [1 1 1]
b =
1 1 1
>> a * b
error: operator *: nonconformant arguments (op1 is 1x3, op2
is 1x3)
* is the matrix multiplication operator so if A is an n-by-m matrix and B is an x-by-y matrix for
A*B
to be valid you need the inner dimension to match in size thus m must equal x. Or said another way, the number of columns of A (in your case 3) must equal the number of columns of B (in your case 1).
Some solutions:
You wanted the inner product:
A*B.' % result is a scalar (1-by-1)
or the outer product:
A.'*B % result is a 3-by-3 matrix
Or else you wanted the element-wise multiplication (i.e. multiply each element with it's corresponding element, assuming the two matrices have the identical size) which is the .* operator:
A.*B % result is a 1-by-3
Matrix dimensions should match: the columns of the second matrix must be the same number rows of the first matrix. For your case, you can multiply a*b^T or a^T*b, depending on what you're trying to achieve. There is also Hadamard product (elementwise multiplication) but it's not considered as matrix multiplication.

Multipling row and column vector using .* operation

a =
1
2
3
b =
1 2 3
a.*b
ans =
1 2 3
2 4 6
3 6 9
I used the .* operator to multiply a row vector and a column vector in Octave to see the results. I dont understand how the answer is obtained.
This is because Octave (in a notable difference from Matlab) automatically broadcasts.
The * operator in Octave is the matrix multiplication operator. So in your case a*b would output (in Matlab as well)
a*b
ans =
1 2 3
2 4 6
3 6 9
which should be expected. The product of a 3-by-1 matrix with a 1-by-3 matrix would have dimensions 3-by-3 (inner dimensions must match, the result takes the outer dimensions).
However the .* operator is the element-wise multiplication operation. That means that instead of matrix multiplication, this would multiply each corresponding elements of the two inputs independent from the rest of the matrix. So [1,2,3].*[1,2,3] (or a'.*b) results in [1,4,9]. Again this is in Matlab and Octave.
When using element-wise operations, it is important that the dimensions of the inputs exactly match. So [1,2,3].*[1,2] will through an error because the dimensions do not match. In Matlab, your a.*b will through an error as well. HOWEVER in Octave it won't, instead it will automatically broadcast. You can imagine this is as if it takes one of your inputs and replicates it on a singleton dimension (so in a column vector, the second dimension is a singleton dimension because it's size is 1) and then applies the operator element-wise. In your case you have two matrices with singleton dimensions (i.e. a columan vector and a row vector) so it actually broadcasts twice and you effectively (but note that it does not actually expand the matrices in memory and is often far faster than using repmat) get
[1,2,3;1,2,3;1,2,3].*[1,1,1;2,2,2;3,3,3]
which produces the result you see.
In matlab, to achieve the same result you would have to explicitly call the bsxfun function (binary singleton expansion function) like so:
bsxfun(#times, a, b)

Resources