Linear algebra library (libFBla)

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Linear algebra library (libFBla)

Post by TJF »

This project is about a general purpose matrix library.

It offers two new variable types: vector and matrix (LA_V and LA_M = linear algebra matrix). In the FB source code they can be used like any variables with operators like '+', '*' or '/' (but mind the linear algebra rules). Here's an example code on how to use it:

Code: Select all

#INCLUDE "libFBla.bas"

#DEFINE english

' used to print out headers and values
#MACRO HEADER(_G_, _E_, _T_)
 #IFDEF english
   ?:?_E_
 #ELSE
   ?:?_G_
 #ENDIF
 ?_T_
#ENDMACRO


HEADER("Loesung eines linearen Gleichungssystemes", _
       "Solution of a linear equation system", "")
?"   3x - 3y + z = 0"
?"      + 4y - z = 5"
?"   2x - 2y + z = 1"

VAR A = LA_M("3, -3,  1" NL _
             "0,  4, -1" NL _
             "2, -2,  1")
HEADER("Koeffizientenmatrix A:", _
       "Coefficient matrix A:", A)

VAR B = LA_V("0, 5, 1")
HEADER("Loesungsvektor B:", _
       "Solution vector B:", B)

VAR r = B / A
HEADER("Ergebnisvektor r = B / A:", _
       "Result vector r = B / A:", r)

HEADER("Ergebnisse:", _
       "Results:", "")
?"x = ";r.Val_(0)
?"y = ";r.Val_(1)
?"z = ";r.Val_(2)



HEADER("Matrizenrechnung", _
       "Matrix algebra", "")
VAR m1 = LA_M( _
  ".11, .12, .13" NL _
  ".21, .22, .23")
?"m1:" : ?m1

VAR m2 = LA_M( _
  "11, 12" NL _
  "21, 22" NL _
  "31, 32")
?"m2:" : ?m2

VAR m3 = m1 * m2
?"m3 = m1 * m2:" : ?m3

#IFNDEF __FB_UNIX__
SLEEP
#ENDIF
Features
  • The library is based on a user defined real type (ie SINGLE or DOUBLE). By changing this type the library can be adjusted to meet custom needs regarding execution speed and precision (ie: REAL10, QUAD, COMPLEX, ...).
  • The new types vector and matrix are variable in size (they can shrink or grow anywhere in the code).
  • The new types can be used at modul level as well as inside UDTs (without limits).
  • Easy initialisation of vectors and matrices.
  • Alot of (all?) operators predefined.
  • Variable soft- error handling (report and stop -- only warn -- ignore).
  • Easy output with PRINT, custom output format.
Library download:
Feel free to post your comments and bug reports here.
Last edited by TJF on May 04, 2012 13:13, edited 1 time in total.
Topito216
Posts: 18
Joined: Dec 11, 2009 13:13
Location: Spain

THANKS

Post by Topito216 »

your library is very usefull for me and I like it. But my knowledge about programming with modules (function,bas,libraries) is very basic.

I have test your library like this simply code

Code: Select all

#INCLUDE "libFBla.bas"
Var A = LA_M("3, -3,  1" NL "0,  4, -1" NL "2, -2,  1")
Var B = LA_V("0, 5, 1")
Var R = B / A
Var N = A*R
? N
The theorical result is obvious is B (0,5,1) but de real result is:
(2.384186e-007,5, 1)

This error is normal, Can I improve it?. Should I use a definition of double? how?

thanks in advance.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: THANKS

Post by TJF »

Topito216 wrote:This error is normal, Can I improve it?. Should I use a definition of double? how?

thanks in advance.
To use double precision prepend the line
  • #DEFINE LA_ScalarDouble
to your code. This line must be before the #INCLUDE "libFBla.bas"-line.
Topito216
Posts: 18
Joined: Dec 11, 2009 13:13
Location: Spain

To improve

Post by Topito216 »

I did that you said but the error keeps.

Code: Select all

#INCLUDE "libFBla.bas"
#DEFINE LA_ScalarDouble
Var A = LA_M("3, -3,  1" NL "0,  4, -1" NL "2, -2,  1")
Var B = LA_V("0, 5, 1")
Var R = B / A
Var N = A*R
? N
Sleep

Best regards
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: To improve

Post by TJF »

I said prepend -- like this

Code: Select all

#DEFINE LA_ScalarDouble
#INCLUDE "libFBla.bas"
Var A = LA_M("3, -3,  1" NL "0,  4, -1" NL "2, -2,  1")
Var B = LA_V("0, 5, 1")
Var R = B / A
Var N = A*R
? N
Sleep
Topito216
Posts: 18
Joined: Dec 11, 2009 13:13
Location: Spain

Sorry

Post by Topito216 »

I am very nerveous. You are allright.

Now it works ok.

If you have more examples of the use I'll apreciate because I have seen functions like distance and angle between vectors, and more. For me it's the speedest way to learn it.

Best regards.
Last edited by Topito216 on Sep 28, 2011 14:03, edited 1 time in total.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Post by TJF »

Sorry, no extra documentation or examples jet (I use it in a commercial app).

Read the comments in the source and just use the new vector and matrix variables as normal variables and mind the Linear Algebra rules. Errors will be shown.

You may post some of your testcode here as examples for other users.
integer
Posts: 410
Joined: Feb 01, 2007 16:54
Location: usa

Post by integer »

Looks good.
I'll try it.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Post by TJF »

Version 0.2 is uploaded (link in first post):
  • some errors switched to warnings (not aborting)
  • advanced error messages with line# and additional infos
  • new division operator matrix / matrix
  • new comparison operators (>, <, >=, <=, <>, =)
  • new mathematic operators (\=, ^=)
  • improved STRING CAST (output)
  • FUNCTION Lenght(V) removed (use PROPERTY instread)
  • TYPE LA_S LA_Eps can be defined outside libFBla now (default is DOUBLE now)
  • macro MKV() and LA_ScalarDouble removed (pointers used only now)
  • bugfix: declaration of matrix with one row works now (Var C = LA_M("0,2,1"))
  • can be used with fbmath.bi now (include libFBla.bas first)
Post Reply