How to acces individual solutions of outputs in maple - maple

I am trying to get to specific solutions from the output in maple. Sometimes there are multiple solutions and they come as tuples. If I assign a variable to the output, I would like to do something like x(1) gives me the first solution, x(2) gives me the second solution and so on.
with(LinearAlgebra):
with(VectorCalculus):
A := Matrix([[1, 2], [8, 1]])
x := Eigenvectors(A)
The eigenvectors x are:

Instead of using round brackets, use square brackets to denote the index position for the element you wish to return in the expression sequence. For example:
x[1];
x[2];
Returns:
Vector([-3, 5])
Matrix([[-1/2, 1/2], [1, 1]])
For more on indexing data structures in Maple, the 8th chapter in the user manual may come in handy.

The Eigenvectors command returns a sequence of two things.
So you could also use multiple assignment, to assign each to its own name directly. Eg,
with(LinearAlgebra):
with(VectorCalculus):
A := Matrix([[1, 2], [8, 1]]):
xvals, xvecs := Eigenvectors(A);
[-3] [-1/2 1/2]
xvals, xvecs := [ ], [ ]
[ 5] [ 1 1 ]
xvals;
[-3]
[ ]
[ 5]
xvecs;
[-1/2 1/2]
[ ]
[ 1 1 ]

Related

Change some entries of a matrix in maple

I have a zero matrix in Maple and I need to replace some of its entries with some non zero elements. How do I do that? I also want to find the inverse of the new matrix after replacement.
If you created the Matrix by using the Matrix command then, by default, all its entries are zero. In that case you can subsequently assign to them as you wish, using indexed assignment.
M:=Matrix(2,2):
M;
[0 0]
[ ]
[0 0]
M[2,2]:=3.7:
M;
[0 0 ]
[ ]
[0 3.7]
The command to compute the inverse is LinearAlgebra:-MatrixInverse. Eg,
M:=Matrix(2,2):
M[2,2]:=3.7:
M[2,1]:=4.1:
M[1,2]:=-0.3:
M[1,1]:=5.9:
M;
[5.9 -0.3]
[ ]
[4.1 3.7 ]
Minv:=LinearAlgebra:-MatrixInverse(M):
Minv;
[0.160450997398092 0.0130095403295750]
[ ]
[-0.177797051170859 0.255854293148309 ]
M . Minv;
[1. 0.]
[ ]
[0. 1.]
See the help page for topic LinearAlgebra for more.

Comparing indices for symbolic expression in matlab

I have a (2x2) symbolic matrix in matlab, and substituted one element.
a = sym('a', [2 2])
a = subs(a, a(1,2), a(2,1))
Then the symbolic matrix looks like
a = [ a1_1, a2_1]
[ a2_1, a2_2]
Now I want to compare the two elements in each row whether their second indices are same or not. For example, if their second indices are same, we substitute them to 10, otherwise no change. After this operation, a should be changed like
a = [ 10, 10]
[ a2_1, a2_2]
How should I write script?

Mean of Nested Lists

I'm trying to calculate the mean of nested lists. I have tried using the map function, but the default gives the mean in the opposite dimension that I am interested in. See the below example:
set a [[1 1][2 2][3 3]] ; create a nested list
set b map mean a ; b equals [1 2 3]
This answer gives [1 2 3] for b. However I am interested in the answer [2 2] by taking the mean in the "other" dimension. I would imagine there is a way to do this with map but haven't figured it out.
to go
print column-means [
[ 1 1 ]
[ 2 2 ]
[ 3 3 ]
]
end
to-report column-means [ matrix ]
if length (remove-duplicates map length matrix) > 1 [
error "All rows must be the same length"
]
report n-values length first matrix [ mean extract ? matrix ]
end
to-report extract [ i row ]
report map [ item i ? ] row
end
A possible solution is the following
set a [[1 2 3] [1 2 3]]
set b map mean a
this will give you [2 2] for b.

MATLAB addressing different matrix elments with an index?

How can I create an index-matrix that specifies which elements of a matrix to address?
So for example I have a matrix A which is 80 by 50. I know that A(1:5,:) addresses only the first 5 elements, but what if I want to multiply A with another matrix which also changes the elements to be addressed? So I want to multiply B(1,:) with A(1:5,:), and B(2,:) with A(10:15,:) and so on. Is there a smart way to specify this index-matrix where the information (1:5; 10:15, etc.) is stored?
Yes you can certainly define indices into a matrix using another matrix. Here is a simple example using a cell array to store the index list:
X =[1,2,3,4,5,6]
Idx = { [1, 2, 3], [4, 5, 6] }
Y = X( Idx{1} ) .* X( {Idx{2} )
Y = [ 4, 10, 18]

PDL pairwise row comparison

I have created a PDL matrix. I need to do a pairwise comparison between each row. Currently I am using the 'where' and 'cov' command to return the pairwise comparison for two slices (generated in a perl loop).
My question: How can I use 'range' and 'slice' to loop over the rows in a pairwise fashion? How can I return my index position? I have looped over the matrix using perl. I have read that looping with perl really cripples the power of PDL.
Desired output:
indexA indexB Value
pos1 pos5 1
pos1 pos6 5
pos1 pos0 7
To be clear I only want to use PDL functionality.
Here is some pseudo code that will (hopeful) illustrate my point better.
p $b
[
[1 0 3 0]
[0 1 0 1]
[1 3 1 3] <- example piddle y
[0 1 0 1] <- example piddle z
]
my concept function{
slice $b (grab row z) - works fine
slice $b (grab row y) - works fine
($a, $b) = where($a,$b, $a < 3 && $b < 3 ) - works fine
p $a [1 1]
p $b [0 0]
cov($a $b) - works just fine.
}
I just need a way to execute pairwise across all rows. I will need to do factorial(n rows) comparisons.
PDL threading is the concept you are looking for here. The general technique for looping along dimensions is to add dummy dimensions in the appropriate places so that the calculation generates implicit threadloops needed. For a multi-dimensional problem, there can be a number of different ways to add dims and hence to create the threadloops.
For your pairwise row calculation, you can choose two nested loops over slice indexes which has perl loops over the two index counts and will generate PDL threading along the rows. You could use just one perl loop over indexes but take advantage of implicit threadlooping to calculate for all rows at once.
A fully PDL-threadloop computation would be to add a dummy dimension for the loop over rows for each of the arguments so that you would calculate the entire N**2 row calculations at once. Here is an example for a shape [4,3] array with the calculation being the == operator:
pdl> $b = floor(random(4,3)*5)
pdl> p $b
[
[0 4 3 3]
[3 3 4 2]
[4 0 1 4]
]
pdl> p $b(,*3)==$b(,,*3)
[
[
[1 1 1 1]
[0 0 0 0]
[0 0 0 0]
]
[
[0 0 0 0]
[1 1 1 1]
[0 0 0 0]
]
[
[0 0 0 0]
[0 0 0 0]
[1 1 1 1]
]
]
The result is a shape [4,3,3] piddle with the 0th dimension corresponding to the rows resulting from the pairwise calculation and the 1st and 2nd dims correspond to the row indexes involved in the == operation.
If you need an index value from or for one of these threadloop calculations, use the xvals, yvals, zvals, or axisvals to generate a piddle with the index values corresponding to that array axis.
pdl> p $b->xvals
[
[0 1 2 3]
[0 1 2 3]
[0 1 2 3]
]
pdl> p $b->yvals
[
[0 0 0 0]
[1 1 1 1]
[2 2 2 2]
]
There are a lot of details relating to the implementation of the PDL threading (not the same as perl threading or posix threads). I recommend the perldl mailing list for reference and discussion with other PDL users and developers. Also, see the first on-line draft of the PDL Book which has more comprehensive coverage of PDL computation and threading.
I think what you're looking for is a method to find all different pairs of rows in the array and then process each pair using cov? If that's correct then I haven't heard of cov and a quick search through the documentation doesn't help. However I can say a few things that may help.
I think you're being overly cautious about dropping out of PDL into Perl code, which will be fine if all you are doing is looping over the indices of all row pairs and pulling those rows out using slice. This is shown in the some sample code below.
Also you can't call where like that as $a < 3 etc. are piddles themselves and the boolean operator won't do what you want on them. Use the & operator instead, and add some parentheses to make sure the expression gets executed in the right order.
Beyond that I can't help unless you correct my understanding of your question or direct me to some documentation of the cov subroutine.
use strict;
use warnings;
use PDL;
my $dat = pdl <<END;
[
[1 0 3 0]
[0 1 0 1]
[1 3 1 3]
[0 1 0 1]
]
END
my $max2 = $dat->dim(1) - 1;
for my $i (0 .. $max2 - 1) {
for my $j ($i + 1 .. $max2) {
my $row1 = $dat->slice(",($i)");
my $row2 = $dat->slice(",($j)");
($row1, $row2) = where($row1, $row2, ($row1 < 3) & ($row2 < 3));
cov($row1, $row2);
}
}