Maple - converting 1D lists into 2D arrays - list

This is a question in Maple.
I understand in terms of java that I want a count and an increment but my logic doesn't convert as simply to maple code.
I have an very long list of numbers LIST (196) that I wish to turn into a 14x14 Array but using the convert(LIST,Array) only gives me a 1 dimensional array.
In Maple code, this will give me my first column.
j:=1;
for i from 1 to 14 do
B[i,j]:=Longlistvalue[i];
end do;
It's clear that my second column comes from t=2 and s from 15 to 24 but I'm struggling to put this into a loop.
Surely there is either a loop I can use for this or a maple command that puts the first 14 into the first row (or column) then the next 14 into the next row/column etc?
My most recent attempt gets me
B:=Array(1..14,1..14):
n:=1;
m:=14;
for j from 1 to 14 do
for i from n to m do
B[i,j]:=Longlistvalue[i];
end do;
n:=n+14;
m:=m+14;
end do;
But not it states that my array is out of range (because the s in B[i,j] must be less than 15).
Is there a way to get around this by means of a more efficient loop?

The Array (or Matrix) constructor can be used to do this directly, using an operator to assign the entries.
You can lay the list entries down into the Array either by column or by row. Adjust the example to fit your case where m=14 and n=14.
m,n := 3,4:
L:=[seq(i,i=1..m*n)]; # you got your list in another way
L := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Array(1..m,1..n,(i,j)->L[(j-1)*m+i]);
[1 4 7 10]
[ ]
[2 5 8 11]
[ ]
[3 6 9 12]
Array(1..m,1..n,(i,j)->L[(i-1)*n+j]);
[1 2 3 4]
[ ]
[5 6 7 8]
[ ]
[9 10 11 12]
You could also use nested loops.
Longlistvalue:=[seq(i,i=1..14^2)]: # your values will differ, of course
B:=Array(1..14,1..14):
n:=14;
m:=14;
for j from 1 to m do
for i from 1 to n do
B[i,j]:=Longlistvalue[(j-1)*m+i];
end do;
end do:
# so we can see the contents, displayed in full
interface(rtablesize=infinity):
B;

Related

Extracting a List from a List of Lists (Maxima)

I am using maxima, and I believe I have a simple question: How do I put the eigenvalues of a matrix in a list, so that I can compute the largest eigenvalue?
From what I'm reading, if M is a matrix, then eigenvalues(M) is a list containing two lists, the first is contains the eigenvalues of M, the second is a list of numbers representing their respective multiplicities. So I guess what I need to do is define a new list by extracting the first list from eigenvalues(M)...Not sure how to do that, however.
Another related question: some of these eigenvalues will be complex, so when I take the maximum of the list, will maxima ignore the complex numbers in the list and just look at the real-valued eigenvalues, or it will give me an error?
Firstly we input the matrix:
M : matrix([1,2],[2,3])$
Then we calculate its eigenvalues:
eigs : eigenvalues(M);
(%o56) [[2 - sqrt(5), sqrt(5) + 2], [1, 1]]
Finally, we extract the maximum eigenvalue:
lmax(first(eigs));
(%o59) sqrt(5) + 2
With regard to the maximum of a list of complex numbers, remember that the concept of maximum only belongs to the realm of real numbers. There is not a maximum of a set of complex numbers.
Here's how I would go about it. I'll assign the result of eigenvalues to two variables, so one is the first list and the other is the second. Note that I'm using parallel assignment for that, e.g. [a, b] : [foo, bar]; assigns foo to a and bar to b. Then I'll paste the elements in the two lists together, to keep each eigenvalue with its multiplicity. Finally I'll sort the eigenvalues by magnitude. I'll sort by decreasing magnitude, so the first element is the largest eigenvalue and its multiplicity.
(%i2) A : matrix ([1/7, 15/14, 15/7], [-13/7, 85/14, 71/7], [9/7, -27/7, -47/7]);
[ 1 15 15 ]
[ - -- -- ]
[ 7 14 7 ]
[ ]
[ 13 85 71 ]
(%o2) [ - -- -- -- ]
[ 7 14 7 ]
[ ]
[ 9 27 47 ]
[ - - -- - -- ]
[ 7 7 7 ]
(%i3) [vals, mults] : eigenvalues (A);
1
(%o3) [[-, - 2, 1], [1, 1, 1]]
2
(%i4) vals_mults : map (lambda ([a, b], [a, b]), vals, mults);
1
(%o4) [[-, 1], [- 2, 1], [1, 1]]
2
(%i5) sort (vals_mults, lambda ([a, b], abs(a[1]) > abs(b[1])));
1
(%o5) [[- 2, 1], [1, 1], [-, 1]]
2
(%i6) first (%);
(%o6) [- 2, 1]

Longest sub-sequence the elements of which make up a set of increasing integers

Find the length of the longest continuous sub-sequence of an array the elements of which make up a set of continuous increasing integers.
The input file consists of the number n(the number of elements in the array) followed by n integers.
example input - 10 1 6 4 5 2 3 8 10 7 7
example output - 6(1 6 4 5 2 3 since they make the set 1 2 3 4 5 6).
I was able to write an algorithm that satisfies 0<n<5000 but in order to get 100 points the algorithm had to work for 0<=n<=50000.
How about something like this? Arrange the array elements in descending order, each coupled with its index-range as a local maximum (for example, A[0] = 10 would be the maximum for array indexes, [0, 10], while A[3] = 4 would be the local maximum for array indexes, [3,3]. Now traverse this list and find the longest, continuously descending sequence where the index-ranges are all contained in the starting range.
10 1 6 4 5 2 3 8 10 7 7
=> 10, [ 0,10]
8, [ 1, 7]
7, [ 9,10]
6, [ 1, 6] <--
5, [ 3, 6] | ranges
4, [ 3, 3] | all
3, [ 5, 6] | contained
2, [ 5, 5] | in [1,6]
1, [ 1, 1] <--

Array elements not getting edited in a Python List

sticks = int(raw_input());
stickList= map(int,raw_input().split()) ;
stickList = sorted(stickList);
for i in xrange(0,len(stickList)):
stickList[i] = stickList[i]-stickList[0];
print stickList;
Given Input is :
6
5 4 4 2 2 8
Why the output is this: [0, 2, 4, 4, 5, 8]
instead of [0,0,2,2,3,6]
That is because you are changing the value in source stickList in for loop.
After first iteration in loop stickList[0] will become 0 for remaining iterations.
As ShadowRanger mentioned reversed list will do,
stickList = map(int, "5 4 4 2 2 8".split())
stickList.sort()
for i in reversed(xrange(len(stickList))):
stickList[i] -= stickList[0]
print stickList

Matrix operation in SAS/IML

In SAS/IML, I'm doing the following:
matrix = {1 2 3 4 5, 2 3 1 2 3, 8 4 8 1 1};
empty = j(5,5);
do i=1 to 5;
empty[i,] = matrix[1,];
end;
So I want to replace the ith row of "empty" with the first row of matrix, but this code doesn't work. How can I replace entire rows of a matrix like this?
If you are trying to replace every row of 'empty' with matrix[1,], I don't see any thing wrong with the code.

Ascending subsequences in permutation

With given permutation 1...n for example 5 3 4 1 2
how to find all ascending subsequences of length 3 in linear time ?
Is it possible to find other ascending subsequences of length X ? X
I don't have idea how to solve it in linear time.
Do you need the actual ascending sequences? Or just the number of ascending subsequences?
It isn't possible to generate them all in less than the time it takes to list them. Which, as has been pointed out, is O(NX / (X-1)!). (There is a possibly unexpected factor of X because it takes time O(X) to list a data structure of size X.) The obvious recursive search for them scales not far from that.
However counting them can be done in time O(X * N2) if you use dynamic programming. Here is Python for that.
counts = []
answer = 0
for i in range(len(perm)):
inner_counts = [0 for k in range(X)]
inner_counts[0] = 1
for j in range(i):
if perm[j] < perm[i]:
for k in range(1, X):
inner_counts[k] += counts[j][k-1]
counts.add(inner_counts)
answer += inner_counts[-1]
For your example 3 5 1 2 4 6 and X = 3 you will wind up with:
counts = [
[1, 0, 0],
[1, 1, 0],
[1, 0, 0],
[1, 1, 0],
[1, 3, 1],
[1, 5, 5]
]
answer = 6
(You only found 5 above, the missing one is 2 4 6.)
It isn't hard to extend this answer to create a data structure that makes it easy to list them directly, to find a random one, etc.
You can't find all ascending subsequences on linear time because there may be much more subsequences than that.
For instance in a sorted original sequence all subsets are increasing subsequences, so a sorted sequence of of length N (1,2,...,N) has N choose k = n!/(n-k)!k! increasing subsequences of length k.