473,785 Members | 2,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Triangular Matrix

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.

Oct 30 '06 #1
5 2168
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.
Oct 30 '06 #2


Je-Givara wrote On 10/30/06 15:41,:
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.
You've numbered the rows and columns starting from one
rather than C's usual zero, but that's all right: I'll use
that numbering (but wanted to draw attention to it to avoid
possible confusion). In those terms, it looks like each
odd-numbered row R has N-R zeroes followed by R possible
non-zeroes, and each even-numbered row is zero throughout.
(If that's not what your diagram means, ignore the rest.)

To access column C of row R, then, you first test for
R odd and C N-R: if either test fails, you return a zero
(on a fetch) or announce a programming error (on a store).
You might also check for 1 <= R,C <= N and announce an error
if either is out of bounds; it depends on how trustworthy you
consider the source of R's and C's. In what follows, I'll
assume R and C are in range, with R odd and C N-R.

How many non-zeroes are in the rows before row R? There
are 1 + 3 + ... + (R-2) = (R-1)*(R-1)/4. How many non-zeroes
are in row R before column C? The row's first non-zero is
in column N-R+1, so there are C-(N-R+1) = R+C-N-1 prior to
column C. Add these to find the number of non-zeroes that
appear before row R column C, and you get

(R-1)*(R-1)/4 + R+C-N-1 = (R+1)*(R+1)/4 + C - N - 1

Double-check by computing a few sample values. The
top right corner is at R=1,C=N, and the formula says it goes
at index 0. Good so far. In row R=3, the three non-zeroes
are at C=N-2,N-1,N, and the formula gives 1,2,3. Looks good.

How big must the linear array be? Easy: figure out the
index of the bottom right corner at R=N,C=N -- and then
recall that C arrays are zero-based, so the number of elements
is one larger than the greatest index.

Going the other way (from a linear array index I to the
corresponding R,C pair) is left as an exercise.

--
Er*********@sun .com

Oct 30 '06 #3

Eric Sosman wrote:
Je-Givara wrote On 10/30/06 15:41,:
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.

You've numbered the rows and columns starting from one
rather than C's usual zero, but that's all right: I'll use
that numbering (but wanted to draw attention to it to avoid
possible confusion). In those terms, it looks like each
odd-numbered row R has N-R zeroes followed by R possible
non-zeroes, and each even-numbered row is zero throughout.
(If that's not what your diagram means, ignore the rest.)

To access column C of row R, then, you first test for
R odd and C N-R: if either test fails, you return a zero
(on a fetch) or announce a programming error (on a store).
You might also check for 1 <= R,C <= N and announce an error
if either is out of bounds; it depends on how trustworthy you
consider the source of R's and C's. In what follows, I'll
assume R and C are in range, with R odd and C N-R.

How many non-zeroes are in the rows before row R? There
are 1 + 3 + ... + (R-2) = (R-1)*(R-1)/4. How many non-zeroes
are in row R before column C? The row's first non-zero is
in column N-R+1, so there are C-(N-R+1) = R+C-N-1 prior to
column C. Add these to find the number of non-zeroes that
appear before row R column C, and you get

(R-1)*(R-1)/4 + R+C-N-1 = (R+1)*(R+1)/4 + C - N - 1

Double-check by computing a few sample values. The
top right corner is at R=1,C=N, and the formula says it goes
at index 0. Good so far. In row R=3, the three non-zeroes
are at C=N-2,N-1,N, and the formula gives 1,2,3. Looks good.

How big must the linear array be? Easy: figure out the
index of the bottom right corner at R=N,C=N -- and then
recall that C arrays are zero-based, so the number of elements
is one larger than the greatest index.

Going the other way (from a linear array index I to the
corresponding R,C pair) is left as an exercise.

--
Er*********@sun .com


unfortunatly this is not RIGHT

Nov 2 '06 #4


Je-Givara wrote On 11/02/06 09:00,:
Eric Sosman wrote:
>>Je-Givara wrote On 10/30/06 15:41,:
>>>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.

You've numbered the rows and columns starting from one
rather than C's usual zero, but that's all right: I'll use
that numbering (but wanted to draw attention to it to avoid
possible confusion). In those terms, it looks like each
odd-numbered row R has N-R zeroes followed by R possible
non-zeroes, and each even-numbered row is zero throughout.
(If that's not what your diagram means, ignore the rest.)

To access column C of row R, then, you first test for
R odd and C N-R: if either test fails, you return a zero
(on a fetch) or announce a programming error (on a store).
You might also check for 1 <= R,C <= N and announce an error
if either is out of bounds; it depends on how trustworthy you
consider the source of R's and C's. In what follows, I'll
assume R and C are in range, with R odd and C N-R.

How many non-zeroes are in the rows before row R? There
are 1 + 3 + ... + (R-2) = (R-1)*(R-1)/4. How many non-zeroes
are in row R before column C? The row's first non-zero is
in column N-R+1, so there are C-(N-R+1) = R+C-N-1 prior to
column C. Add these to find the number of non-zeroes that
appear before row R column C, and you get

(R-1)*(R-1)/4 + R+C-N-1 = (R+1)*(R+1)/4 + C - N - 1

Double-check by computing a few sample values. The
top right corner is at R=1,C=N, and the formula says it goes
at index 0. Good so far. In row R=3, the three non-zeroes
are at C=N-2,N-1,N, and the formula gives 1,2,3. Looks good.

How big must the linear array be? Easy: figure out the
index of the bottom right corner at R=N,C=N -- and then
recall that C arrays are zero-based, so the number of elements
is one larger than the greatest index.

Going the other way (from a linear array index I to the
correspondi ng R,C pair) is left as an exercise.

--
Er*********@s un.com


unfortunatly this is not RIGHT
I never claimed to be infallible. Would you mind
revealing my error, so I can learn and improve myself?

--
Er*********@sun .com

Nov 2 '06 #5
Eric Sosman wrote:
>
Je-Givara wrote On 11/02/06 09:00,:
>Eric Sosman wrote:
>>Je-Givara wrote On 10/30/06 15:41,:

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.
You've numbered the rows and columns starting from one
rather than C's usual zero, but that's all right: I'll use
that numbering (but wanted to draw attention to it to avoid
possible confusion). In those terms, it looks like each
odd-numbered row R has N-R zeroes followed by R possible
non-zeroes, and each even-numbered row is zero throughout.
(If that's not what your diagram means, ignore the rest.)

To access column C of row R, then, you first test for
R odd and C N-R: if either test fails, you return a zero
(on a fetch) or announce a programming error (on a store).
You might also check for 1 <= R,C <= N and announce an error
if either is out of bounds; it depends on how trustworthy you
consider the source of R's and C's. In what follows, I'll
assume R and C are in range, with R odd and C N-R.

How many non-zeroes are in the rows before row R? There
are 1 + 3 + ... + (R-2) = (R-1)*(R-1)/4. How many non-zeroes
are in row R before column C? The row's first non-zero is
in column N-R+1, so there are C-(N-R+1) = R+C-N-1 prior to
column C. Add these to find the number of non-zeroes that
appear before row R column C, and you get

(R-1)*(R-1)/4 + R+C-N-1 = (R+1)*(R+1)/4 + C - N - 1

Double-check by computing a few sample values. The
top right corner is at R=1,C=N, and the formula says it goes
at index 0. Good so far. In row R=3, the three non-zeroes
are at C=N-2,N-1,N, and the formula gives 1,2,3. Looks good.

How big must the linear array be? Easy: figure out the
index of the bottom right corner at R=N,C=N -- and then
recall that C arrays are zero-based, so the number of elements
is one larger than the greatest index.

Going the other way (from a linear array index I to the
correspondi ng R,C pair) is left as an exercise.

--
Er*********@sun .com

unfortunatly this is not RIGHT

I never claimed to be infallible. Would you mind
revealing my error, so I can learn and improve myself?
>>yy
Nov 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1844
by: Nevets Steprock | last post by:
I'm writing a web program where one of the sections is supposed to output a correlation matrix. The typical correlation matrix looks like this: ..23 ..34 .54 ..76 .44 .28 ..02 .77 .80 .99 I've written code to calculate the correlation data and it is populated in a vector like this:
1
1322
by: KinGPIN | last post by:
hi ; I have a homework about CMatrix; and i had a problem ;in my homowork i hace functions and operators written for CMatrix class(this was my first homework); it is wanted to inherite the functions in CMatrix for CTriangular matrix ;and then inherite the CTriangular matrix as upper and lower triangular matrixes.In addition ;in the homework zero elements of matrixes will not be stored to use the memory efficiently;for example a diagonal...
4
1356
by: Je-Givara | last post by:
this is new mapping function needed for following --------------------------------------------------------------- a11 a12 a13 .... a1n 0 a22 a23 .... a2n 0 0 a33 .... a3n . . 0 0 0 .... ann ---------------------------------------------------- for example if we have n=5 : then the map like: ----------------------------------------------------
8
1297
by: empire5 | last post by:
I want to eliminate the little triangular arrow poinger that appears tot he right of the menu item. Which control setting lets me do that.
6
4276
by: suresh4ind | last post by:
Hi i need to copy the upper triangular elements of matrix A to an array B,how to perform this ,can u send me the accessing formula
13
3087
by: mo/-/sin | last post by:
hi........... i m mohsin plz provide me the program which represents triangular matrix in c language.........
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.