ANSI C Compatibility

Discussion of issues related specifically to writing code for native mode devices. This includes ZBasic code as well as assembly language code and C code, both inline and standalone.
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

The link below contains decent linear algebra functions in C language. I can not take credit for them. Charlie Matlack and "RobH45345" from the Arduino playground deserve the credit. As for the recursive covariance functions, I will handle that part - its working in Matlab, now it needs to be ported. I may do it Visual Basic or C. Anyway, the linear algebra functions below are a good start. I'm sure several of your customers will find them useful.

http://arduino.cc/playground/uploads/Co ... ixMath.zip
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

If you have a simple matrix add example showcasing passing arrays between Zbasic and C, that'll help tremendously. Thanks.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:The link below contains decent linear algebra functions in C language. I can not take credit for them. Charlie Matlack and "RobH45345" from the Arduino playground deserve the credit.
The code can't be used directly because it is implemented as part of a C++ class. The code generated from ZBasic source is C code and you can't instantiate C++ objects or invoke C++ methods directly from C (but see below for more information). One could add an intermediary module written in C++ that provides C-style entry points but that seems pointless in this case because there is absolutely no value added by encapsulating the matrix math functions in a C++ class other than the ability to use the Arduino Print object.

I've created a new file, MatrixMath.c, that contains all of the functionality of MatrixMath.cpp except for MatrixPrint() and displaying a message when an error is encountered in matrix inversion. I modified the MatrixMath.h file to be compatible with both MatrixMath.c and MatrixMath.cpp.

The attached archive contains a ZBasic project matrix_test.pjt, a ZBasic source file matrix_test.bas and the MatrixMath.c and MatrixMath.h files described above. The test is a simple one that creates a matrix, transposes it and then displays the original and transposed matrix. The code is written in a general way to allow any rectangular ZBasic array to be used, irrespective of the lower bound. The code could be simplified if zero-based or one-based arrays were assumed.

An upcoming version of ZBasic will support directly accessing functions in C++ modules and classes. We're also working on the ability to import external procedures automatically from information in .h files.
Attachments
MatrixMath.zip
(4.17 KiB) Downloaded 669 times
- Don Kinzer
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

Excellent! I will give it a shot. Thank you very much.
liam.zbasic
Posts: 163
Joined: 24 March 2008, 23:33 PM
Location: Southern California (Blue)

Post by liam.zbasic »

All the matrix functions with 6x6 test matrices yield correct results. Question... I modified your matrix display function to show more significant figures, but the IDE will not display more than 6 decimal places. Is that the limit for single type variables?
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:I modified your matrix display function to show more significant figures, but the IDE will not display more than 6 decimal places.
What you observed is not a limitation of the IDE, of course, but a documented limitation of Fmt() based on the limits of the single precision (32-bit) floating point representation.

There has been discussion recently (among the maintainers of avr-gcc) of adding optional double precision (64-bit) floating point support in the avr-gcc compiler. If that is realized, we will probably add support for the Double data type in ZBasic as well.
- Don Kinzer
kranenborg
Posts: 57
Joined: 27 July 2009, 14:20 PM
Location: Groningen, The Netherlands
Contact:

Post by kranenborg »

Hello,

Just want to remark that the matrix transpose example works very well on my ZX-328nu too, and that I am very interested in further developments regarding the kalman filtering procedures. Furthermore, with these basic matrix algebra functions a wealth of applications becomes available, and with Don's announced ZBASIC support improvements it will become even more easy to include mathematical routines. I highly appreciate this

/Jurjen
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

liam.zbasic wrote:All the matrix functions with 6x6 test matrices yield correct results.
If you aren't using all of the matrix routines in your application, you might consider adding the line below to your .pjt file. The overall effect of this set of options, which are passed on to the back end compile and link phase, is to eliminate routines that are not referenced. With these options in place, the matrix_test code went from 10290 bytes to 7880 bytes when compiled for a ZX-24n.

Code: Select all

--gcc-opts=-ffunction-sections -fdata-sections -Wl,--gc-sections,--relax
The v3.4.x version of the compiler (currently being tested) includes these options by default for the back end compiler/linker in the build script.
- Don Kinzer
Post Reply