comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Sum along diagonals
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Sum along diagonals [message #21426] Mon, 28 August 2000 00:00
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
> display: the image;

Oops, that's a syntax error. Unfortunately the IDL-ku form is
overburdened with commas.

Craig


--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
Re: Sum along diagonals [message #21437 is a reply to message #21426] Mon, 28 August 2000 00:00 Go to previous message
colinr is currently offline  colinr
Messages: 30
Registered: July 1999
Member
On Fri, 25 Aug 2000 17:57:01 -0700,
Todd Clements <mole6e23@hotmail.com> wrote:
> craigmnet@cow.physics.wisc.edu wrote:
>> How about this solution. It's not a one-liner, and it uses two loops,
>> [snip]
>
> That works just fine! Once again, I am duly impressed. I'm sure it took me
> longer to figure out your code than it did for you to write it! Absolutely
> amazing. Having read this newsgroup for a while, I'm beginning to wonder
> if there is an IDL question you can't answer.

Can he write a Stein-Vidar emulator in IDL? Or maybe David could write a
Craig emulator?

--
Colin Rosenthal
Astrophysics Institute
University of Oslo
Re: Sum along diagonals [message #21438 is a reply to message #21426] Mon, 28 August 2000 00:00 Go to previous message
Martin Schultz is currently offline  Martin Schultz
Messages: 515
Registered: August 1997
Senior Member
Craig Markwardt wrote:

> At times like this I turn to the ancient poetry form, IDL-ku. Like
> the haiku it must adhere to the terse 5-7-5 syllable structure, but
> must also be a compileable IDL program. The equals sign, "=", can
> either be silent or short-hand for the one-syllable, "is."

Craig, pleeeaaase post these on your web page! They are great!

Martin


>
> yes, _ref_extra = $
> useful, NOT a, figment (from)
> JDs, imagination
>
> arrays, alter (shape)
> at, unexpected, moments;
> remember, reform (them)
>
> display: the image;
> for robust=results, please do $
> use, imdisp OR plotimage
>
> Nuggets of wisdom from the ages... Not answers to every question, but
> then again we must leave some mystery for the new people.
>
> Craig
>

--
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
[[ Dr. Martin Schultz Max-Planck-Institut fuer Meteorologie
[[
[[ Bundesstr. 55, 20146 Hamburg
[[
[[ phone: +49 40 41173-308
[[
[[ fax: +49 40 41173-298
[[
[[ martin.schultz@dkrz.de
[[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[
Re: Sum along diagonals [message #21441 is a reply to message #21426] Sun, 27 August 2000 00:00 Go to previous message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
Craig Markwardt (craigmnet@cow.physics.wisc.edu) writes:

> At times like this I turn to the ancient poetry form, IDL-ku.

Well done! :-)

I can foresee archeologists in the future digging
up transcripts of the IDL newsgroup archives and
wondering, "Who *were* those guys!"

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
Re: Sum along diagonals [message #21442 is a reply to message #21441] Sun, 27 August 2000 00:00 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
davidf@dfanning.com (David Fanning) writes:

> Todd Clements (mole6e23@hotmail.com) writes:
>
>> Having read this newsgroup for a while, I'm beginning to wonder
>> if there is an IDL question you can't answer. (
...
>
> Here is one from The Book of IDL Questions
> I hear he is writing.
>
> How many contours will I get if
> I let IDL select them?
>
> Who in his right mind thought we needed
> something like the Table Widget?
>
> Why is it so difficult to get PostScript output,
> and why are object graphics lines so thin?
>
> How hard can it be to write code that is
> incomprehensible? Do I need to know more, or less?

At times like this I turn to the ancient poetry form, IDL-ku. Like
the haiku it must adhere to the terse 5-7-5 syllable structure, but
must also be a compileable IDL program. The equals sign, "=", can
either be silent or short-hand for the one-syllable, "is."

yes, _ref_extra = $
useful, NOT a, figment (from)
JDs, imagination

arrays, alter (shape)
at, unexpected, moments;
remember, reform (them)

display: the image;
for robust=results, please do $
use, imdisp OR plotimage

Nuggets of wisdom from the ages... Not answers to every question, but
then again we must leave some mystery for the new people.

Craig

--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
Re: Sum along diagonals [message #21449 is a reply to message #21441] Fri, 25 August 2000 19:28 Go to previous message
jph is currently offline  jph
Messages: 2
Registered: September 1999
Junior Member
I hope you folks realize you are using the wrong
language for this sort of thing. :-) To see what a *real*
array language does with this, look at the "J" solution
(J's a sort of APL in ASCII: http://www.jsoftware.com/)

NB. define a 4x4 matrix:
M=. i. 4 4
NB. display it:
M
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15

NB. sum the diagonals:

+//. M
0 5 15 30 30 25 15

NB. Yes, that's it: +//. is the code!
NB. It doesn't have to be square:

M=. i. 4 6
M
0 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17
18 19 20 21 22 23
+//. M
0 7 21 42 46 50 48 39 23

I know, it's got nothing to do with IDL, but I couldn't
resist. Actually, I use J to compute things & IDL to
display the results....

J. Patrick Harrington


In article <mole6e23-2508001450050001@checont6.ucsd.edu>, mole6e23@hotmail.com (Todd Clements)
|>
|> Every once in a while (not often enough to make me worry about optimizing
|> too much), I want to take a not necessarily square matrix and get the sum
|> along the diagonals, such as the following, with the theoretical function
|> sum_diag:
|>
|> IDL> blah = indgen( 4, 4 )
|> IDL> print, blah
|> 0 1 2 3
|> 4 5 6 7
|> 8 9 10 11
|> 12 13 14 15
|> IDL> print, sum_diag( blah )
|> 0 5 15 30 30 25 15
|>
|> which is the series [0, 4+1, 8+5+2, 12+9+6+3, ... ]
|>
|> Of course, to be difficult, I'd like it to work for non-square matrices as well:
|>
|> IDL> blah = indgen( 5, 3 )
|> IDL> print,blah
|> 0 1 2 3 4
|> 5 6 7 8 9
|> 10 11 12 13 14
|>
|> and the result would be the series [0, 5+1, 10+6+2, 11+7+3, ... ]
|>
Re: Sum along diagonals [message #21455 is a reply to message #21449] Fri, 25 August 2000 00:00 Go to previous message
davidf is currently offline  davidf
Messages: 2866
Registered: September 1996
Senior Member
Todd Clements (mole6e23@hotmail.com) writes:

> Having read this newsgroup for a while, I'm beginning to wonder
> if there is an IDL question you can't answer. (

Have you ever read Pablo Neruda's The Book of Questions?
Fabulous. Here is an example.

Whom can I ask what I came
to make happen in this world?

Why do I move without wanting to,
why am I not able to sit still?

Why do I go rolling without wheels,
flying without wings or feathers,

and why did I decide to migrate
if my bones live in Chile?

Here is one from The Book of IDL Questions
I hear he is writing.

How many contours will I get if
I let IDL select them?

Who in his right mind thought we needed
something like the Table Widget?

Why is it so difficult to get PostScript output,
and why are object graphics lines so thin?

How hard can it be to write code that is
incomprehensible? Do I need to know more, or less?

Cheers,

David

P.S. Let's just say I'll bet *those* questions take
Craig longer than five minutes to answer. :-)

--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
Re: Sum along diagonals [message #21457 is a reply to message #21455] Fri, 25 August 2000 00:00 Go to previous message
mole6e23 is currently offline  mole6e23
Messages: 31
Registered: December 1998
Member
craigmnet@cow.physics.wisc.edu wrote:
> How about this solution. It's not a one-liner, and it uses two loops,
> [snip]

That works just fine! Once again, I am duly impressed. I'm sure it took me
longer to figure out your code than it did for you to write it! Absolutely
amazing. Having read this newsgroup for a while, I'm beginning to wonder
if there is an IDL question you can't answer. (Rhetorical - not meant to
open a can of worms such as the top requested feature thread!)

> There you go! This is a speedy devil on my machine.

It sure is! I put your code into my code, and the running time went from
3.66 seconds <ahem> down to 0.15 seconds for each 512x512 image. It took a
2700x2700 'image' to make your code take 3.6 seconds!

Todd
Re: Sum along diagonals [message #21458 is a reply to message #21455] Fri, 25 August 2000 00:00 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
mole6e23@hotmail.com (Todd Clements) writes:
...
> Every once in a while (not often enough to make me worry about optimizing
> too much), I want to take a not necessarily square matrix and get the sum
> along the diagonals, such as the following, with the theoretical function
> sum_diag:
>
> IDL> blah = indgen( 4, 4 )
> IDL> print, blah
> 0 1 2 3
> 4 5 6 7
> 8 9 10 11
> 12 13 14 15
> IDL> print, sum_diag( blah )
> 0 5 15 30 30 25 15
>
> which is the series [0, 4+1, 8+5+2, 12+9+6+3, ... ]
>
> Of course, to be difficult, I'd like it to work for non-square matrices as well:
...

How about this solution. It's not a one-liner, and it uses two loops,
but remember loops are not always bad if you can do a lot of work
inside one iteration. This one makes NX+NY-1 iterations.

;; Set up the problem with some fake data, a NX x NY array
nx = 4 & ny = 3 & mm = indgen(nx, ny)

;; Output array
tt = fltarr(nx+ny-1)

;; Do the work
ll = lindgen(nx>ny)
for i = 0, ny-1 do tt(i) = total((mm(0+ll,i-ll))(0:i<(nx-1)))
for i = 1, nx-1 do tt(i+ny-1) = total((mm(i+ll,ny-1-ll))(0:(nx-1-i)<(ny-1)))

For the gobledy-gook impaired, first note that the two loops march
down the left side of the array and then across the bottom,
respectively. Second, I'm using array indexing along two dimensions
simultaneously. The expression, mm(0+ll,i-ll), is the actual diagonal
of interest. The bit at the end of the expression, (0:i<(nx-1)), is
used to trim off the end, which may contain the wrong data if the
array is not square.

There you go! This is a speedy devil on my machine.

Good luck,
Craig

--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: Texture mapping onto DEM
Next Topic: Re: IDL 5.2 array definition question.

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Oct 10 08:35:39 PDT 2025

Total time taken to generate the page: 1.59516 seconds