Newsgroups: comp.lang.apl
Path: watmath!watserv2.uwaterloo.ca!torn!howland.reston.ans.net!usc!elroy.jpl.nasa.gov!jato!csi!csi.jpl.nasa.gov!sam
From: sam@csi.jpl.nasa.gov (Sam Sirlin)
Subject: Re: Question: Tri-diagonal matrices in APL
Message-ID: <1993Apr27.170835.11165@csi.jpl.nasa.gov>
Sender: usenet@csi.jpl.nasa.gov (Network Noise Transfer Service)
Nntp-Posting-Host: kalessin
Organization: Jet Propulsion Laboratory, Pasadena, CA
References:  <27APR93.10680596.0076@UNBVM1.CSD.UNB.CA>
Date: Tue, 27 Apr 1993 17:08:35 GMT
Lines: 33

In article <27APR93.10680596.0076@UNBVM1.CSD.UNB.CA>, Allison Schriver <SCHR@UNB.CA> writes:
|> 
|> What is the most efficient way to generate a tri-diagonal matrix,
|> as below, in APL? I am using STSC APL*PLUS, v10.0.
|> 
|>  2 -1  0  0  0  0
|> -1  2 -1  0  0  0
|>  0 -1  2 -1  0  0
|>  0  0 -1  2 -1  0
|>  0  0  0 -1  2 -1
|>  0  0  0  0 -1  2

Well I think this should work:

.del m .is a tri n
   m .is (n,n) .take (1 - .iota n) .rv (n,n+1) .take (n,3) .rho a
.del  

For example
   _1 2 _1 tri 6

should produce your example above (origin 1). Don't know about
efficiency... space or time? If efficiency worries you, why build the
tridiagonal matrix at all, as all you need to store are the 3 elements
along the diagonal, and the 0's don't tell you anything.  Are you sure
this is the bottleneck in your code? Usually I find that the process
of generating data to work on is done once, so it can be inefficient,
whereas the following analysis is where I spend my time.

-- 
Sam Sirlin
Jet Propulsion Laboratory         sam@kalessin.jpl.nasa.gov

