ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I assume zero based indexing, but that depends on the language.
1) Initialize the sums to zero. There should be Rows+Cols-1 sums
2) Iterate over the source matrix in any convenient sequence, hitting each r,c (row and column intersection) exactly once.
2a) Compute which sum it belongs to:
index = c-r
if (index<0) index=Cols-index-1
2b) Add to the correct sum
S[index] += M[r,c]
Notice that I chose a source oriented algorithm: Deal with the source values in any convenient sequence, and for each source value figure out which destination position(s) it goes to.
A destination oriented algorithm usually is closer to the original problem statement: For each destination position, go through all the source positions needed to compute that destination.
Part of learning to program is learning to make good choices between alternate approaches like that. In this case, the source oriented algorithm has only one complicated detail (adjusting the index for the case that the simplistic index is negative). But a destination oriented algorithm has more than one complication.
In serious programming, you often need to focus on performance consequences rather than just simplicity in such choices. In this case, that drives the decision the same way: The big source and small destination means that (if the matrix were big enough for performance to matter) cache coherent access to the source is more important than cache coherent access to the destination.
... extend it to the right with three columns of zeroes. Why three? It is the number of rows, minus one. Then replicate the original matrix on the right of this "padded with zeroes" version. This awk builds Workfile 1 ...
The finished product, nine sums, are written to an output file. They look like this:
Code:
22 17 21 8 10 7 2 8 18
Having done this step-by-step and writing interim results to work files allows us to visualize the process and debug as necessary. When confident that the code is working properly the three steps may be combined into one pipe, eliminating the need for work files. The pipe looks like this:
I haven't tested your awk command to see what I might be missing, but I can't see the reordering could work. You use
(i+m)%n-m
So the direction (as i increases) is positive on both sides of the wrap point.
As I understood the original request, the specified sequence through the simplistic diagonal numbers is negative on the other side of the wrap point.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.