Matrix Multiplication bug and workaround

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Matrix Multiplication bug and workaround

Post by capellan » Sun Jul 30, 2017 8:36 pm

Using LC 8.1.3, I just found a bug in the Matrix Multiply function.
Could you check if this bug was fixed in more recent LiveCode versions?

Follow this recipe to reproduce this bug:

1) Create a new stack with a button, a text field
and a check box named "showbug"

2) paste these two lines into the text field:

TM 1,0.6,-1.2,1,40,10
CO 0,30

You could find this and other examples in this webpage:
https://stackoverflow.com/documentation ... 4300495488

3) paste this script into the button:
----------------------------------------------------------------

Code: Select all

local tMatrix1
local tMatrix2

on mouseUp
   put empty into tMatrix1
   put empty into tMatrix2
   put empty into tNewMatrix
   put empty into tNewCoordinatePoint
  
   put fld 1 into tMatrixList
  
   repeat for each line tMX in tMatrixList
     
      switch word 1 of tMX
         case TM
            TransformTMatrix(word 2 of tMX)
            break
         case CO
            CoordinatePointMatrix(word 2 of tMX)
            break
      end switch
     
   end repeat
  
   put matrixMultiply(tMatrix1, tMatrix2) into tNewMatrix
      combine tNewMatrix with comma
      answer item 1 to 2 of tNewMatrix

end mouseUp

on TransformTMatrix pArgs
   local tA
   local tB
   local tC
   local tD
   local tE
   local tF
  
   local tMatrix
  
if the hilite of btn "showbug" then
   put item 1 of pArgs into tA
   put item 2 of pArgs into tB
   put item 3 of pArgs into tC
   put item 4 of pArgs into tD
else -- use a workaround
   put item 1 of pArgs into tA
   put item 2 of pArgs into tC -- change position
   put item 3 of pArgs into tB -- change position
   put item 4 of pArgs into tD
end if
  
   put tA into tMatrix[0,0]
   put tB into tMatrix[0,1]
   put tC into tMatrix[1,0]
   put tD into tMatrix[1,1]
  
   if (the number of items in pArgs) is 6 then
      put item 5 of pArgs into tE
      put item 6 of pArgs into tF
   else
      put 0 into tE
      put 0 into tF
   end if
  
   put tE into tMatrix[0,2]
   put tF into tMatrix[1,2]
  
   put 0 into tMatrix[2,0]
   put 0 into tMatrix[2,1]
   put 1 into tMatrix[2,2]
  
   if tMatrix1 is empty
   then
      put tMatrix into tMatrix1
      return empty
   end if
  
end TransformTMatrix

on CoordinatePointMatrix tArgs
   put item 1 of tArgs into tG
   put item 2 of tArgs into tH
  
   put tG into tCoordinatePoint[0,0]
   put tH into tCoordinatePoint[1,0]
   put 1 into tCoordinatePoint[2,0]
  
   if tMatrix1 is empty
   then
      put tCoordinatePoint into tMatrix1
      return empty
   end if
  
   if tMatrix2 is empty
   then
      put tCoordinatePoint into tMatrix2
      return empty
   end if
  
end CoordinatePointMatrix
----------------------------------------------------------------
4) Hilite the check box "showbug" and click the button... the result is 58,40
This result is wrong.

5) Unhilite the check box "showbug" and click the button... the result is 4,40
This result is correct.

You could verify these results using this LC Library:
http://www.ekkotek.com/index.php/produc ... ecoded-1-0
or with this online matrix calculator:
http://matrix.reshish.com/multiplication.php

If this bug is still present in more recent LC versions. Could you report it to the Quality Center?
Thanks in advance!

Al
MatrixMultiplicationBug.zip

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Matrix Multiplication bug and workaround

Post by capellan » Mon Jul 31, 2017 12:45 am

Hermann Hoch corrected my mistake in a message posted in the mail list:

>This is not a bug, your script is wrong. If TM is a,b,c,d,e,f then you have to do
> put item 1 of pArgs into tMatrix[0,0]
> put item 2 of pArgs into tMatrix[1,0]
> put item 3 of pArgs into tMatrix[0,1]
> put item 4 of pArgs into tMatrix[1,1]
>this yields correct results.
>[In fact your 'workaround' corrects the wrong script part]

Now I understand. This transformation matrix

a b c d e f
1,0.6,-1.2,1,40,10
should be stored like this:

[0,0] [0,1] [0,2]
a c e
1 -1.2 40

[1,0] [1,1] [1,2]
b d f
0.6 1 10

[2,0] [2,1] [2,2]
0 0 1

The Dictionary must include a functional example of using Matrix Multiply for processing svg matrix transforms.

I asked to change from bug report to enhancement request.
http://quality.livecode.com/show_bug.cgi?id=20208

Al

Post Reply

Return to “Talking LiveCode”