shift position in matrix - matrix

I have a n x 1 matrix. I'm trying to find a way to "shift" all the elements position (loosing the last element) and then add an element in position 0,0 in this way:
From
[[ 10 ]
[ 5 ]
[ 2 ]
[ 3 ]
[ 1 ]
[ 5 ]]
to (adding a new element 2 in position 0,0)
[[ 2 ]
[ 10 ]
[ 5 ]
[ 2 ]
[ 3 ]
[ 1 ]]
I'm pretty close to the solution but I don't know how add elements to a nested list.
;initial matrix
set mymatrix matrix:from-column-list [[10 5 2 3 1 5]]
;temp
let list matrix:to-column-list mymatrix
let tmplist matrix:to-column-list states
; ERROR here: the result of fput is [2[10 5 2 3 1 5]]
set tmplist fput 2 tmplist
;new matrix
matrix:set-column mymatrix 0 tmplist
EDIT: I realized that indeed for my needs a matrix is an overkill. I solved switching to pure netlogo lists and doing my business in map-reduce.

If you just need elementwise multiplication of objects that are fundamentally one-dimensional, just use map.
Example: (map * [1 2] [3 4]) reports [3 8].
Now you can just do your bookkeeping with lists, which is much easier.
Even if (for some reason you have not stated) you really need matrix operations elsewhere, you almost surely should use lists for the bookkeeping you describe and then convert when necessary.

Related

How can I compute determinant of n order where n is a variable with mathematica or maple or maxima?

For example, an n*n matrix
A_n=[2 1 1 ... 1]
[1 2 1 ... 1]
[... ...]
[1 1 1 ... 2]
has determinant n+1. Can I compute this result by these softwares?
This
f[n_]:=Det[Table[1,{n},{n}]+IdentityMatrix[n]];
f[12]
returns 13 and seems to work for any modest sized matrix.
Since Maple may not easily provide a structure representing an abstract Vector or Matrix of indeterminate size you might try to recast the problem in terms of some known properties of determinants.
For example, using Sylvester's determinant theorem you could take utilize the nx1 Matrix V(n) having all entries being identically 1.
So det(A(n)) = det(Id(n) + V(n).transpose(V(n))) = 1 + det(transpose(V(n)).V(n)) = 1 + add(i,i=1..n) = 1 + n
Another way might be to consider a minor expansion along the nth row. When the nth row and nth column are removed from the initial Matrix denoted by A(n) then you obtain A(n-1). Removing the nth row and the jth column (j in 1..n-1) produces a Matrix with determinant (-1)^j. I do not show that here. But perhaps note that any of those minors are a single row-exchange away from a (n-1)x(n-1) Matrix with A(n-2) in the upper left and 1's elsewhere.
In consequence Q(n) the determinant of A(n) is given by Q(n)=2*Q(n-1)-(n-1) I believe. And Q(1)=2. Using Maple's rsolve command,
rsolve({Q(n)=2*Q(n-1)-(n-1), Q(1)=2}, Q(n));
n + 1
Another approach might be to consider row-reduction along rows 2..n. You should be able to produce a new Matrix (having the same determinant) whose diagonal entries are: 2, (i+1)/i, i=2..n. The determinant is thus a conveniently telescoping product.
simplify( 2*product((i+1)/i, i=2..n) );
n + 1
Of course it is easy enough to deal with such Matrices for size n taken at a specific value,
H:=n->1+Matrix(n,n,1):
with(LinearAlgebra):
seq(Determinant(H(i)),i=1..5);
2, 3, 4, 5, 6
U7:=LUDecomposition(H(6),output=U);
[2 1 1 1 1 1]
[ ]
[ 3 1 1 1 1]
[0 - - - - -]
[ 2 2 2 2 2]
[ ]
[ 4 1 1 1]
[0 0 - - - -]
[ 3 3 3 3]
[ ]
U7 := [ 5 1 1]
[0 0 0 - - -]
[ 4 4 4]
[ ]
[ 6 1]
[0 0 0 0 - -]
[ 5 5]
[ ]
[ 7]
[0 0 0 0 0 -]
[ 6]
# The product of the main diagonalentries telescopes.
mul(U7[i,i],i=1..6);
7

Compressed Sparse Row (CSR): How do you store empty rows?

How do you represent empty rows in CSR?
Suppose we have the following matrices:
* MATRIX 1 *
a 0 0
0 b 0
0 0 c
val = [ a b c ]
col = [ 0 1 2 ]
row = [ 0 1 2 ] <- makes sense!
—————————————————————
* MATRIX 2 *
a b c
0 0 0
0 0 0
val = [ a b c ]
col = [ 0 1 2 ]
row = [ 0 ] <— makes sense…? but how about…
—————————————————————
* MATRIX 3 *
0 0 0
a b c
0 0 0
val = [ a b c ]
col = [ 0 1 2 ]
row = [ 0 ] <— wait… how do we differentiate between MATRIX 2 and MATRIX 3?
MATRIX 1 is intuitive, but how do we represent the difference between MATRIX 2 and MATRIX 3? Do we use a negative integer for spacing?
Thanks
Take a look at The Wikipedia page. The IA vector (or as you call it "row"), is defined as:
The array IA is of length m + 1. It is defined by this recursive definition:
IA[0] = 0
IA[i] = IA[i − 1] + (number of nonzero elements on the (i − 1)-th row in the original matrix)
Thus, the first m elements of IA store the index into A of the first nonzero element in each row of M [ ed: but only for rows that have at least one such element, i.e. IA[i+1]>IA[i] ], and the last element IA[m] stores NNZ, the number of elements in A, which can be also thought of as the index in A of first element of a phantom row just beyond the end of the matrix M. The values of the i-th row of the original matrix is read from the elements A[IA[i]] to A[IA[i + 1] − 1] (inclusive on both ends), i.e. from the start of one row to the last index just before the start of the next.
Thus, in Matrix 1:
row = [0 1 2 3]
in Matrix 2:
row = [0 3 3 3]
in Matrix 3
row = [0 0 3 3]
The answer provided is close to being a great explanation but needs to be clarified a bit:
The first two bullet points give a clear prescription for the entries in IA.
But the next part,
"Thus, the first m elements of IA store the index into A of the first nonzero element in each row of M, and the last element IA[m] stores NNZ, the number of elements in A, which can be also thought of as the index in A of first element of a phantom row just beyond the end of the matrix M. The values of the i-th row of the original matrix is read from the elements A[IA[i]] to A[IA[i + 1] − 1]
(inclusive on both ends), i.e. from the start of one row to the last index just before the start of the next."
It is difficult to parse, because the references "IA", "m", "NNZ", "M", and "A" are not defined.
Is the length of "IA" equal to one plus the number of nonzero elements in the full matrix?. I think that "A" and "M" refer to the full matrix and the sparse matrix, but there isn't enough information to determine which is which. "NNZ" may refer to the number of nonzero elements in the full matrix. And "m" may be the same as "NNZ".
Would you mind completing the explanation by providing the definitions of "IA", "m", "NNZ", "M" and "A"?
Thanks in advance, it would be much appreciated!

NetLogo get each time different random number from certain list

let's say I have list
let mylist [0 1 2 3]
and I would like to generate random number from this array which is in every tick different than previous one.
Example: Tick one - generates 0
Tick two - generates 2
Tick three - generates 1
Tick four - generates 3
Now I have
let mylist [0 1 2 3]
let x one-of mylist
But that returns for example for two consecutive ticks number 0.
Any tips? Thank you.
One way is to store the number that was used in the last tick, compare it to the one chosen in the current tick, and choose a different one if they are the same.
globals [
previous_number
]
to generate
let current_number previous_number
let mylist [ 0 1 2 ]
while [ current_number = previous_number ] [
set current_number one-of mylist
]
set previous_number current_number
print current_number
end

How to make a matrix of random values in NetLogo?

Is there a way to easily make an $n \cross m$ matrix in NetLogo? Additionally would it be possible to fill this matrix with random values? Thanks.
this answer has been updated for NetLogo 6 task syntax
See http://ccl.northwestern.edu/netlogo/docs/matrix.html for docs on NetLogo's matrix extension.
For creating a matrix, there are several primitives that do that: matrix:make-constant, matrix:make-identity, matrix:from-row-list, matrix:from-column-list.
For creating a matrix and filling it with random values, I'd suggest defining this procedure first:
to-report fill-matrix [n m generator]
report matrix:from-row-list n-values n [n-values m [runresult generator]]
end
Then to make, say, a 5 by 5 matrix, of, say, random integers in the range 0 to 9, it's:
fill-matrix 5 5 [-> random 10]
Example result:
observer> show fill-matrix 5 5 [-> random 10]
observer: {{matrix: [ [ 5 9 3 2 6 ][ 5 8 2 8 0 ][ 6 7 3 7 4 ][ 7 0 4 6 3 ][ 7 9 0 0 5 ] ]}}

Algorithm to swap two indices in a symmetric matrix

I am trying for a day now to find an algorithm to swap two indices in a symmetric matrix so that the result is also a symmetric matrix.
Let´s say I have following matrix:
0 1 2 3
1 0 4 5
2 4 0 6
3 5 6 0
Let´s say I want to swap line 1 and line 3 (where line 0 is the first line). Just swapping results in:
0 1 2 3
3 5 6 0
2 4 0 6
1 0 4 5
But this matrix is not symmetric anymore. What I really want is following matrix as a result:
0 3 2 1
3 0 6 5
2 6 0 4
1 5 4 0
But I am not able to find a suitable algorithm. And that really cracks me up, because it looks like in easy task.
Does anybody know?
UPDATE
Phylogenesis gave a really simple answer and I feel silly that I could not think of it myself. But here is a follow-up task:
Let´s say I store this matrix as a two-dimensional array. And to save memory I do not save the redundant values and I also leave out the diagonal which has always 0 values. My array looks like that:
[ [1, 2, 3], [4, 5], [6] ]
My goal is to transform that array to:
[ [3, 2, 1], [6, 5], [4] ]
How can I swap the rows and then the columns in an efficient way using the given array?
It is simple!
As you are currently doing, swap row 1 with row 3. Then swap column 1 with column 3.

Resources