Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:38 09 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Math M_Mult Bug

Author Message
Elijah 2
Newbie

Joined: 08/03/2025
Location: United States
Posts: 6
Posted: 05:09pm 08 Mar 2025
Copy link to clipboard 
Print this post

Hi all, I'm a new member but have been using MMBasic for a bit.
I've found a bug (I think) with the Math M_Mult command.  It seems to be using the Bound() of the wrong dimension when computing a product matrix with M_Mult; thus this bug doesn't manifest when multiplying square matrices.

Here is code the demos the bug:
Dim Float A(1, 6), TransA(6, 1), ProdA(6, 6)

Math Set 1, A()
A(1, 0) = 0 : A(1, 1) = 2 : A(1, 2) = 3 : A(1, 3) = 4
A(1, 4) = 5 : A(1, 5) = 7 : A(1, 6) = 8

Math M_Transpose A(), TransA()
Math M_Mult A(), TransA(), ProdA()

Math M_Print A() : Print
Math M_Print TransA() : Print
Math M_Print ProdA()


Actual Output:
  1.0000,    0.0000
  1.0000,    2.0000
  1.0000,    3.0000
  1.0000,    4.0000
  1.0000,    5.0000
  1.0000,    7.0000
  1.0000,    8.0000

  1.0000,    1.0000,    1.0000,    1.0000,    1.0000,    1.0000,    1.0000
  0.0000,    2.0000,    3.0000,    4.0000,    5.0000,    7.0000,    8.0000

  1.0000,    1.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
  1.0000,    5.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
  1.0000,    7.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
  1.0000,    9.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
  1.0000,   11.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
  1.0000,   15.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
  1.0000,   17.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000


Expected Output for ProdA():
1 1 1 1 1 1 1
1 5 7 9 11 15 17
1 7 10 13 16 22 25
1 9 13 17 21 29 33
1 11 16 21 26 36 41
1 15 22 29 36 50 57
1 17 25 33 41 57 65


You can see that the first two columns of output for ProdA() are correct.  If I were to make A() have three columns, the first three would be correct.  It's a little confusing because in code you dim A(cols, rows) but in math it's rows x cols.  Maybe that's why the bug exists to begin with  

If some of the other forum members can confirm this is a bug, I'd appreciate it.

Peter, sorry for emailing you directly about this.  I honestly didn't think there was anyway I'd get a working membership on this forum, but it let me register (after a lot of leg work ).

Thanks all!
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4851
Posted: 05:23pm 08 Mar 2025
Copy link to clipboard 
Print this post

I used matrix multiplication extensive in the thermal camera. On 5.09.00.
Later tonight I can check on current release.

Volhout
PicomiteVGA PETSCII ROBOTS
 
Elijah 2
Newbie

Joined: 08/03/2025
Location: United States
Posts: 6
Posted: 05:27pm 08 Mar 2025
Copy link to clipboard 
Print this post

Thanks!  I'm using v5.08.  I should have mentioned that.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10062
Posted: 05:31pm 08 Mar 2025
Copy link to clipboard 
Print this post

You don't say what firmware and what version you are using. I get the correct answer with V6.00.02b16
 
Elijah 2
Newbie

Joined: 08/03/2025
Location: United States
Posts: 6
Posted: 05:52pm 08 Mar 2025
Copy link to clipboard 
Print this post

MM.Info(Version) is 5.08 and the firmware filename is WebMiteV5.08.00.uf2.

I'm waiting to upgrade for 6.00.02 [release/stable?] for the LInStr regex fix.  If this has already been addressed, please disregard.
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4851
Posted: 06:30pm 08 Mar 2025
Copy link to clipboard 
Print this post

Hi Elijah,

The thermal camera still work correct with the latest 6.00.02b16_2 on RP2040 in a VGA platform.
I remember when implementing it that I needed to read the description in the user manual carefully, since there was something in the way rows and columns where used. Maybe that is what you are referring to.

These are snippets of the code, array sizes, and how they are used with math M_mult

 'scaling buffers and math matrices
 dim f_t(191),f_v(191)                     'scratchpad temp/voltage
 size%=5                                   'pixel size
 dim Toa(15,11)                            'this is Vir() in 2D form for math
 dim c(15,44)                              'array for intermediate results math
 dim d(11,44),b_e(30,15),b_o(30,15)        'conversion arrays for scaling
 dim x_s%(1394),y_s%(1394)                 'arrays for screen coordinates
 fill_matrix                               'fill conversion matrices
 dim scrn_2D(30,44)                        '2D buffer for math
 dim scrn_1De(1394),scrn_1Do(1394)         'even/odd 1D arrays for 61x45 screen




'lots of code here in between.....




 'show 192 temperatures on screen in a grid of 61x45 interleaved
sub scale_4d
 
 local y31,px,cs0,cs1,x,y as integer
 
 
 'copy linear array into 16x12 array, and perform math to scale up (=21ms)
 MEMORY COPY INTEGER PEEK( VARADDR Vir()),PEEK( VARADDR Toa()),192
 
 math m_mult d(),Toa(),c()           'c() is in correct vertical resolution
 
 'with low delta-T limit the color scaling for visual appearance
 max_colors = min(int(Tmax-Tmin)+2,num_colors-1)
 max_colors = 15
 math window c(),0,max_colors,c()  'x color
 'math window c(),0,max_colors+0.4,c()  'x colors + 0.4c for better distribution
 
 
 'calculate even pixels
 math m_mult c(),b_e(),scrn_2D()
 
 'flatten into 1D array
 MEMORY COPY float PEEK( VARADDR scrn_2D()),PEEK( VARADDR scrn_1De()),1395
 
 'convert to RGB
 colour map scrn_1De(),scrn_1De(),col%()
 
 
 'calculate odd pixels
 math m_mult c(),b_o(),scrn_2D()
 
 'flatten into 1D array
 MEMORY COPY INTEGER PEEK( VARADDR scrn_2D()),PEEK( VARADDR scrn_1Do()),1395
 
 'convert to RGB
 colour map scrn_1Do(),scrn_1Do(),col%()
 
 'plot all the boxes on screen, since VGA does not use a framebuffer
 'the plotting is done as fast and compact as possible. That is why there
 'are dual 1D screen buffers.
 
 '31 even boxes, original x() coordinates
 box x_s%(),y_s%(),size%,size%,1,scrn_1De(),scrn_1De()
 math add x_s%(),size%,x_s%() 'prepare for odd boxes
 
 '30 odd boxes
 box x_s%(),y_s%(),size%,size%,1,scrn_1Do(),scrn_1Do()
 math add x_s%(),-size%,x_s%() 'restore for next even boxes
 
end sub




In this upscaling routine the even pixels and odd pixels are calculated independently, simply because there was not enough RAM memory to hold the whole array of 64bit variables. The b_e() and b_o() arrays contain the multiplication factors for upscaling.

Volhout
Edited 2025-03-09 04:35 by Volhout
PicomiteVGA PETSCII ROBOTS
 
Elijah 2
Newbie

Joined: 08/03/2025
Location: United States
Posts: 6
Posted: 08:08pm 08 Mar 2025
Copy link to clipboard 
Print this post

I've upgraded my pico 1 W to the latest stable firmware WebMiteRP2040V6.00.01.uf2 and rerun the code posted above.  Success!

Output with WebMiteRP2040V6.00.01.uf2:
  1.0000,    0.0000
  1.0000,    2.0000
  1.0000,    3.0000
  1.0000,    4.0000
  1.0000,    5.0000
  1.0000,    7.0000
  1.0000,    8.0000

  1.0000,    1.0000,    1.0000,    1.0000,    1.0000,    1.0000,    1.0000
  0.0000,    2.0000,    3.0000,    4.0000,    5.0000,    7.0000,    8.0000

  1.0000,    1.0000,    1.0000,    1.0000,    1.0000,    1.0000,    1.0000
  1.0000,    5.0000,    7.0000,    9.0000,   11.0000,   15.0000,   17.0000
  1.0000,    7.0000,   10.0000,   13.0000,   16.0000,   22.0000,   25.0000
  1.0000,    9.0000,   13.0000,   17.0000,   21.0000,   29.0000,   33.0000
  1.0000,   11.0000,   16.0000,   21.0000,   26.0000,   36.0000,   41.0000
  1.0000,   15.0000,   22.0000,   29.0000,   36.0000,   50.0000,   57.0000
  1.0000,   17.0000,   25.0000,   33.0000,   41.0000,   57.0000,   65.0000


I didn't see this as an issue that was addressed so presumed it still was.  All is well!

Volhout, your use of Math Window to fit the color pallet is very elegant!  
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025