tags:

views:

282

answers:

5

i am trying to make a 100 x 100 tridiagonal matrix with 2's going down the diagonal and -1's surrounding the 2's. i can make a tridiagonal matrix with only 1's in the three diagonals and preform matrix addition to get what i want, but i want to know if there is a way to customize the three diagonals to what ever you want. maplehelp doesn't list anything useful.

+2  A: 

The Matrix function in the LinearAlgebra package can be called with a parameter (init) that is a function that can assign a value to each entry of the matrix depending on its position.
This would work:

f := (i, j) -> if i = j then 2 elif abs(i - j) = 1 then -1 else 0; end if;
Matrix(100, f);
jmbr
A: 

LinearAlgebra[BandMatrix] works too (and will be WAY faster), especially if you use storage=band[1]. You should probably use shape=symmetric as well.

Jacques Carette
A: 

I just get "invalid arrow procedure" when I type this into Maple... :^{ (Since I'm doing periodic BC's I can't use BandMatrix w/sparse storage)

Patti

f := (i, j) -> if i = j then 2 elif abs(i - j) = 1 then -1 else 0; end if; Matrix(100, f);

Patti
A: 

jmbr's proposed solutions can be adapted to work:

f := proc(i, j)
  if i = j then 2
  elif abs(i - j) = 1 then -1
  else 0
  end if
end proc;
Matrix(100, f);

Also, I understand your comment as saying you later need to destroy the band matrix nature, which prevents you from using BandMatrix - is that right? The easiest solution to that is to wrap the BandMatrix call in a regular Matrix call, which will give you a Matrix you can change however you'd like:

 Matrix(LinearAlgebra:-BandMatrix([1,2,1], 1, 100)); 
Erik P.
A: 

The answers involving an initializer function f will do O(n^2) work for square nxn Matrix. Ideally, this task should be O(n), since there are just less than 3*n entries to be filled.

Suppose also that you want a resulting Matrix without any special (eg. band) storage or indexing function (so that you can later write to any part of it arbitrarily). And suppose also that you don't want to get around such an issue by wrapping the band structure Matrix with another generic Matrix() call which would double the temp memory used and produce collectible garbage.

Here are two ways to do it (without applying f to each entry in an O(n^2) manner, or using a separate do-loop). The first one involves creation of the three bands as temps (which is garbage to be collected, but at least not n^2 size of it).

M:=Matrix(100,[[-1$99],[2$100],[-1$99]],scan=band[1,1]);

This second way uses a routine which walks M and populates it with just the three scalar values (hence not needing the 3 band lists explicitly).

M:=Matrix(100):

ArrayTools:-Fill(100,2,M,0,100+1);

ArrayTools:-Fill(99,-1,M,1,100+1);

ArrayTools:-Fill(99,-1,M,100,100+1);

Note that ArrayTools:-Fill is a compiled external routine, and so in principal might well be faster than an interpreted Maple language (proper) method. It would be especially fast for a Matrix M with a hardware datatype such as 'float[8]'.

By the way, the reason that the arrow procedure above failed with error "invalid arrow procedure" is likely that it was entered in 2D Math mode. The 2D Math parser of Maple 13 does not understand the if...then...end syntax as the body of an arrow operator. Alternatives (apart from writing f as a proc like someone else answered) is to enter f (unedited) in 1D Maple notation mode, or to edit f to use the operator form of if. Perhaps the operator form of if here requires a nested if to handle the elif. For example,

f := (i,j) -> `if`(i=j,2,`if`(abs(i-j)=1,-1,0));
Matrix(100,f);
acer