Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-10

Optimized matrix multiplication functions for pascal?

13 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

I am translating a bunch of Fortran functions to FPC and I have now come across a problem with a couple of built-in Fortran90 functions:

MATMUL multiplies two matrices (arrays) and returns the result.

TRANSPOSE reorders the array elemsnts in some predefined way.

The call in the Fortran routine is like this:

b = matmul(transpose(a), a)

where a and b are equal sized dynamic arrays of double.

So I need to get hold of two Delphi functions:

function matmul(X, Y: array of double): array of double;
and
function transpose(X: array of double): array of double;

Where can I find such functions?
I am not a matematician myself so I don't know what these functions really do...

Bo Berglund
Bo Berglund

I am translating a bunch of Fortran functions to FPC and I have now come across a problem with a couple of built-in Fortran90 functions:

MATMUL multiplies two matrices (arrays) and returns the result.

TRANSPOSE reorders the array elemsnts in some predefined way.

The call in the Fortran routine is like this:

b = matmul(transpose(a), a)

where a and b are equal sized dynamic arrays of double.

So I need to get hold of two Delphi functions:

function matmul(X, Y: array of double): array of double;
and
function transpose(X: array of double): array of double;

Where can I find such functions?
I am not a matematician myself so I don't know what these functions really do...

Bo Berglund
Honza
to Bo Berglund

2010/10/9 Bo Berglund :

Show quoted text 9 lines
So I need to get hold of two Delphi functions: function matmul(X, Y: array of double): array of double; and function transpose(X: array of double): array of double; Where can I find such functions? I am not a matematician myself so I don't know what these functions really do...

The machinery is depicted e.g. here:
http://en.wikipedia.org/wiki/Matrix_multiplication http://en.wikipedia.org/wiki/Matrix_transpose

IMO it's not really tough to implement them in FPC.

bflm freepascal-bits.blogspot.com
Michael Van Canneyt
to Bo Berglund: I am translating a bunch of Fortran functions to FPC and I have now come across a problem…

You may want to look at the matrix.pp unit included in the rtl. It contains many matrix operations.

Michael.

Bo Berglund wrote 31 lines
I am translating a bunch of Fortran functions to FPC and I have now come across a problem with a couple of built-in Fortran90 functions: MATMUL multiplies two matrices (arrays) and returns the result. TRANSPOSE reorders the array elemsnts in some predefined way. The call in the Fortran routine is like this: b = matmul(transpose(a), a) where a and b are equal sized dynamic arrays of double. So I need to get hold of two Delphi functions: function matmul(X, Y: array of double): array of double; and function transpose(X: array of double): array of double; Where can I find such functions? I am not a matematician myself so I don't know what these functions really do... -- Bo Berglund _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reimar Grabowski
to Bo Berglund: I am translating a bunch of Fortran functions to FPC and I have now come across a problem…
Where can I find such functions? I am not a matematician myself so I don't know what these functions really do...

Take a look here: http://www.j3d.org/matrix_faq/matrfaq_latest.html

R.

A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing in e-mail?
Paulo Costa
to Bo Berglund: I am translating a bunch of Fortran functions to FPC and I have now come across a problem…
Bo Berglund wrote 23 lines
I am translating a bunch of Fortran functions to FPC and I have now come across a problem with a couple of built-in Fortran90 functions: MATMUL multiplies two matrices (arrays) and returns the result. TRANSPOSE reorders the array elemsnts in some predefined way. The call in the Fortran routine is like this: b = matmul(transpose(a), a) where a and b are equal sized dynamic arrays of double. So I need to get hold of two Delphi functions: function matmul(X, Y: array of double): array of double; and function transpose(X: array of double): array of double; Where can I find such functions? I am not a matematician myself so I don't know what these functions really do...

Try:

http://paginas.fe.up.pt/~paco/pmwiki/index.php?n=DynMatrix.DynMatrix

It is a bit more than just those functions but it could solve, for a while, your problem.

Paulo Costa

Reimar Grabowski
to Adriaan van Os
Matrix functions can be implemented efficiently using SSE, see e.g. <http://www.cortstratton.org/articles/OptimizingForSSE.php>.

FPC can generate the SSE instructions for you on compilation. My tests showed that this is faster than writing the functions using SSE myself. If you have written matrix routines using SSE that are faster than mine written in pascal and optimized by the compiler I am really interested in seeing them.

R.

A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing in e-mail?
David W Noon
to Bo Berglund: I am translating a bunch of Fortran functions to FPC and I have now come across a problem…

On Sat, 9 Oct 2010 07:59:26 +0200, Bo Berglund wrote about [fpc-pascal] Optimized matrix multiplication functions for pascal?:

[snip]

Where can I find such functions?

They are available in a library named LAPACK. You can find downloads by doing a Google search on this name. It is coded in FORTRAN though.

I am not a matematician myself so I don't know what these functions really do...

They are easy to code, as they are simple linear algebra. A good textbook on linear algebra would be a great help to you.

However, LAPACK gets mighty complicated when it gets into other areas of numerical linear algebra. If your FORTRAN code uses more complicated routines, you might find it convenient simply to call the LAPACK FORTRAN code directly. Maintaining a Pascal version of LAPACK would be a non-trivial -- even arduous -- task, but you could check the FPC Wiki to see if somebody might already have done that for you.

Regards, Dave [RLU #314465] *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* (David W Noon) *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Adriaan van Os
to David W Noon
David W Noon wrote 8 lines
On Sat, 9 Oct 2010 07:59:26 +0200, Bo Berglund wrote about [fpc-pascal] Optimized matrix multiplication functions for pascal?: [snip] > Where can I find such functions? They are available in a library named LAPACK. You can find downloads by doing a Google search on this name. It is coded in FORTRAN though.

There is also a C version of LAPACK, see

<http://www.netlib.org/clapack/>
<http://developer.apple.com/hardwaredrivers/ve/vector_libraries.html>

with prebuilt Windows libraries
<http://www.netlib.org/clapack/LIB_WINDOWS/prebuilt_libraries_windows.html>.

It should be possible to call into those libs from FPC.

Regards,

Adriaan van Os

Sven Barth
to Bo Berglund: I am translating a bunch of Fortran functions to FPC and I have now come across a problem…
Bo Berglund wrote 24 lines
I am translating a bunch of Fortran functions to FPC and I have now come across a problem with a couple of built-in Fortran90 functions: MATMUL multiplies two matrices (arrays) and returns the result. TRANSPOSE reorders the array elemsnts in some predefined way. The call in the Fortran routine is like this: b = matmul(transpose(a), a) where a and b are equal sized dynamic arrays of double. So I need to get hold of two Delphi functions: function matmul(X, Y: array of double): array of double; and function transpose(X: array of double): array of double; Where can I find such functions? I am not a matematician myself so I don't know what these functions really do...

Try the unit matrix.pp which is part of FPC's RTL. It also includes overloaded operators if you need them. ^^

(In Lazarus: enter the unit name in the uses clause and do a Ctrl+left mouse button on it to view its contents)

Regards,
Sven

Marco van de Voort
to Sven Barth

In our previous episode, Sven Barth said:

Try the unit matrix.pp which is part of FPC's RTL. It also includes overloaded operators if you need them. ^^

Afaik matrix only defines 4x4 or less.

Numlib also has a large number of matrix routines (including determinant, eigen values etc), but is not entirely documented. (actually there is documentation, but it is in dutch)

Sven Barth
to Marco van de Voort
Marco van de Voort wrote 9 lines
In our previous episode, Sven Barth said: > Try the unit matrix.pp which is part of FPC's RTL. It also includes > overloaded operators if you need them. ^^ Afaik matrix only defines 4x4 or less. Numlib also has a large number of matrix routines (including determinant, eigen values etc), but is not entirely documented. (actually there is documentation, but it is in dutch)

Ok... I didn't realize that he has dynamic arrays as parameters... So some other code than that in matrix.pp might be better here.

Regards,
Sven