Re: Initialize a Diagonal Matrix

<michael@xxxxxxxxxxxxxxxx> wrote:

Other than using a DO loop is there any compact way of initializing the
elements of a diagonal matrix. For a vector one can write,
v(:)=(/1,3,10/). Is there something similar for a diagonal matrix?

Mostly, no. I think I might have seen an obscure related trick but I
don't recall the details and I might even misrecall having seen such a
thing. If it existed, it certainly wasn't anything that you'd actually
want to do other than to win a bet or some such thing.

I'm slightly puzzled in two ways about your mention of DO loops.

First, I feel obligated to ask what is wrong with DO loops. I ask
becasue there are some people who seem to have what I can only regard as
a prejuice against DO loops, thinking that they are somehow
intrinsically inferior to newer features of the language. I disagree
with that.

That aside, I have trouble imagining exactly what you are talking about
in DO loops. I don't know of a simple direct way to do this using DO
loops either. If I had a very small (say 3x3) array, I'd probably just
set it to 0 and then individually assign each of the diagonal elements.
If I had an array that was much larger than that ·or was dynamically
sized), I'd likely initialize a temporary rank 1 array for the diagnonal
elements and then use a DO loop to copy that to the diagonal of the
rank-2 array. Perhaps you meant something like that, but I wasn't quite
sure whether you had something else in mind, since I would say that the
main characterization of that method is that it uses a temporary rank-1
array for the diagonal; the DO loop seems like a smaller detail.

Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain

Relevant Pages

  • Re: Global array operations: a performance hit?
    ... as if many DO loops were executed instead than just one. ... global array operations then? ... Note that the usual terminology is something more like "whole array ... once in a while they might also get you faster execution, ...
  • Re: Puppy Mastiff wants to Nip at Faces
    ... first couple of weeks of an introductory data structures ... it seems to me by my recollection that loops were... ... in my first college textbook on structured programming. ... they did was loop through an array to show how you could easily access ...
  • Re: Inefficient code?
    ... line is written to the output file. ... or read it into an array. ... # Backup File Field Positions ... Define 'my %found;' above the loops. ...
  • Re: Global array operations: a performance hit?
    ... when using array operations you have additional information ... which is not always the case in DO loops. ... optimizing array expressions. ... The compiler has to ...
  • Re: Problem with reading data from files?
    ... Blocked reallocations? ... Not using an array at all is another. ... which is the use of infinite DO loops. ... All I saw was the "pick a large number" scheme, ...