Re: Triangular Matrix



Je-Givara wrote:
I want to get a Mapping Function for following sparse matrix of size
NxN , where N is odd
-----------------------------------------------------------------------------------
0 0 0 0 0 ... 0 a1,n
0 0 0 0 0 ... ... 0 0
0 0 0 0 0 ... a3,n-2 a3,n-1 a3,n
0 0 0 0 0 .. ... 0 0
0 0 0 ... a5,n-4 a5,n-3 a5,n-2 a5,n-1 a5,n
.... .... ... ... ... ... .. ...
.......................................................................
.......................................................................
0 0 0 0 0 ..... 0 0 0
an1 an2 an3 an4 an5 ... an,n-2 an,n-1 an,n
-------------------------------------------------------------------------------------

Suggest a mapping function to map the elements of the matrix onto a
single dimensional array
in which zero "0" elements do not have representation.

Please, any body could find mapping function formula for this kind of
Matrix.

Take a coordinate within the matrix as (x, y) where 0 <= x,y < N. We want to find an index value for a flat array that skips over elements that are known to be zero.

I noted that from the y coordinate you can calculate the number of real values that have gone before as a sum. I started with the formula
Sum[k, {k, 1, n}] == n * (n + 1) / 2
Noting that only odd rows are significant, y is divided by 2. Some fiddling with constants by trial and error and I have a formula.

#include <stdio.h>
#include <assert.h>

#define N 7

#define INDEX(x, y) (((y)/2 + 2) * ((y)/2 + 3) / 2 - N - 2 + (x))
#define ARRSIZE ((N/2 + 2) * (N/2 + 3) / 2 - 2)

int main(void)
{
int arr[ARRSIZE];
int x, y;
assert(N % 2 == 1 /* odd N */);
printf("arr[%d]\n", ARRSIZE);
for(y = 0; y < N; y += 2)
{
for(x = N - y - 1; x < N; x++)
{
printf("(%d, %d) -> %d\n", x, y, INDEX(x, y));
}
}
return 0;
}

C:\docs\prog\c>gcc sparse.c && a
arr[13]
(6, 0) -> 0
(4, 2) -> 1
(5, 2) -> 2
(6, 2) -> 3
(2, 4) -> 3
(3, 4) -> 4
(4, 4) -> 5
(5, 4) -> 6
(6, 4) -> 7
(0, 6) -> 6
(1, 6) -> 7
(2, 6) -> 8
(3, 6) -> 9
(4, 6) -> 10
(5, 6) -> 11
(6, 6) -> 12

The index values go from 0 of course, since this is C.

--
Simon.
.



Relevant Pages

  • Re: Interesting math
    ... Multiple by an odd number. ... Multiplication by zero is both even ... even and odd to not violate our even and odd rule. ... making yourself a wild savage of imagination. ...
    (alt.usage.english)
  • Re: Interesting math
    ... And I wouldn't be too sure about "it" knowing that "zero is ... and odd numbers. ... rather than untrue. ... The same sort of issue arises across all sorts of mathematics. ...
    (alt.usage.english)
  • Re: Probably havent seen this one, but...
    ... and the closest I can come to pinning it on anything known ... take the critical point closest to zero ... upper bound to this first extreme value. ... Don't you mean just when M is odd? ...
    (sci.math)
  • Re: Interesting math
    ... Multiple by an even number. ... Multiple by an odd number. ... Multiplication by zero is both even ... Division by zero yields an infinite number set. ...
    (alt.usage.english)
  • Re: Interesting math
    ... And I wouldn't be too sure about "it" knowing that "zero is ... and odd numbers. ... The same sort of issue arises across all sorts of mathematics. ... my computer science and maths students is persuading them that the difference ...
    (alt.usage.english)