Skip to content

Commit 9690051

Browse files
committed
comments resolved, part 2
1 parent 071fc24 commit 9690051

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

statocles-site/blog/2024/12/25/jwst/index.markdown

+3-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ However in this case the resulting spectrum is hot garbage because by summing al
7373
## Broadcasting
7474

7575
We need to only extract the central few rows. Code like
76-
`$jwst->slice(':,13:17')->mv(1,0)->sumover` would work, but we can find a more optimal solution and demonstrate 'broadcasting' at the same time! First look at this code:
76+
`$jwst->slice(':,13:17')->t(1,0)->sumover` would work, but we can find a more optimal solution and demonstrate 'broadcasting' at the same time! First look at this code:
7777

7878
# Make a gaussian extraction profile
7979

@@ -96,7 +96,8 @@ Now we can do some _broadcasting_ and some _reduction_:
9696

9797
Recall `$jwst` has dimensions 435,31. We see another dimension trick, `$gaussian->clump(0)` adds a unit dimension at position 0 resulting in a dimension 1,31 ndarray. When we multiply these ndarrays together the second 31 element dimensions match, and the first dimensions (435 and 1) are also matched. What happens is PDL implicitly expands the unit dimension by repeating it 435 times, and in the multiplication `$gaussian->clump(0)` behaves as a 435,31 ndarray. It is like multiplying two 435,31 ndarrays together. This ultimately results in the gaussian being multiplied through each column, which is then summed by the following line.
9898

99-
This trick is known as 'broadcasting' and is one of the most powerful PDL features. You can see we have written some highly complex code to apply mathematical operations to the image as a function of x,y without writing a single loop! Broadcasting is powerful because it can be applied along any axis with suitable dimension tricks using functions such as `mv()` and `clump()`. It is also extremely fast as it operates at speeds close to what would happen if the loops were written in C or FORTRAN. When using it I tend to need to experiment a bit to achieve what I want and look at the resulting dimensions. The main rule to remember is dimensions need to _match_, this happens when they are either the same size, or when one of them is of size unity. In the latter case the 'broadcasting' happens and you can think of it as growing the axis by repetition. This all happens in the PDL internals during the operation it is being applied to, and the ndarray does not actually get any bigger nor use more memory! The same goes for dimension manipulation with functions like `clump()` and `mv()`, the ndarray is not copied it is simply 'viewed differently'.
99+
This trick is known as 'broadcasting' and is one of the most powerful PDL features. You can see we have written some highly complex code to apply mathematical operations to the image as a function of x,y without writing a single loop! Broadcasting is powerful because it can be applied along any axis with suitable dimension tricks using functions such as `t()` and `clump()`. It is also extremely fast as it operates at speeds close to what would happen if the loops were written in C or FORTRAN. When using it, I tend to need to experiment a bit to achieve what I want and look at the resulting dimensions. The main rule to remember is dimensions need to _match_, this happens when they are either the same size, or when one of them is of size unity. In the latter case the 'broadcasting' happens and you can think of it as growing the axis by repetition. This all happens in the PDL internals during the operation it is being applied to, and the ndarray does not actually get any bigger nor use more memory! The same goes for dimension manipulation with functions like `clump()` and `t()`, the ndarray is not copied it is simply 'viewed differently'.
100+
For switching other dimensions, [mv()](https://metacpan.org/pod/PDL::Slices#mv) and [xchg()](https://metacpan.org/pod/PDL::Slices#xchg).
100101

101102
More info on broadcasting and its rules is given in the pod [PDL::Broadcasting](https://metacpan.org/dist/PDL/view/Basic/Pod/Broadcasting.pod).
102103

0 commit comments

Comments
 (0)