Pascal triangle to a matrix
11-22-2019, 08:45 AM
Post: #1
 Tonig00 Junior Member Posts: 41 Joined: May 2016
Pascal triangle to a matrix
Hello
I need some help. I do not program often and when I do I have doubts.

I try to put the values of the Pascal triangle,
1 --- m1(1,1)
1 1 --- m1(2,1) m1(2,2)
1 2 1 --- m1(3,1), etc
1 3 3 1
……………..
to a matrix that I call "m1" up to "f" lines, in the following way:

EXPORT Tri_Pa(f)
BEGIN
LOCAL n,m;
LOCAL m1;
FOR n FROM 0 TO f DO
FOR m FROM 0 TO n DO
m1(n,m):=COMB(n,m);
END;
END;
END;

But the program stops in the line "m1(n,m):=…", and says: "Invalid input".

How should I define the matrix to avoid the error?

Toni
11-22-2019, 01:07 PM (This post was last modified: 11-22-2019 01:28 PM by toml_12953.)
Post: #2
 toml_12953 Senior Member Posts: 1,512 Joined: Dec 2013
RE: Pascal triangle to a matrix
(11-22-2019 08:45 AM)Tonig00 Wrote:  Hello
I need some help. I do not program often and when I do I have doubts.

I try to put the values of the Pascal triangle,
1 --- m1(1,1)
1 1 --- m1(2,1) m1(2,2)
1 2 1 --- m1(3,1), etc
1 3 3 1
……………..
to a matrix that I call "m1" up to "f" lines, in the following way:

Toni

What does COMB(0,0) even mean? I don't see it as being useful.

Try this:

Code:
EXPORT Tri_Pa(f) BEGIN    LOCAL n,m;    LOCAL m1:=makemat(0,f,f);    FOR n FROM 1 TO f DO       FOR m FROM 1 TO n DO          m1(n,m):=COMB(n,m);       END;    END; END;

Tom L
I don't care for whom you voted. If you put ice in your beer, you're crazy.
11-22-2019, 03:57 PM
Post: #3
 Didier Lachieze Senior Member Posts: 1,236 Joined: Dec 2013
RE: Pascal triangle to a matrix
(11-22-2019 08:45 AM)Tonig00 Wrote:  But the program stops in the line "m1(n,m):=…", and says: "Invalid input".

How should I define the matrix to avoid the error?

You get an error because:
- You need to define m1 as a matrix
- Matrix indices start at 1 and not 0

So here is a working version of your program :

Code:
EXPORT Tri_Pa(f) BEGIN   LOCAL n,m;   LOCAL m1:=[[0]];   FOR n FROM 0 TO f-1 DO     FOR m FROM 0 TO n DO       m1(n+1,m+1):=COMB(n,m);     END;   END; END;

Note that you can also generate the same matrix using just the MAKEMAT function:

Code:
EXPORT Tri_Pa(f) BEGIN   MAKEMAT(IFTE(I<J,0,COMB(I-1,J-1)),f,f); END;
11-22-2019, 07:14 PM
Post: #4
 Tonig00 Junior Member Posts: 41 Joined: May 2016
RE: Pascal triangle to a matrix
Thanks very much

Both works, and very interesting how the matrix works and the IFTE function.

Thanks
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)