Huge Matrices Library [Updated]
#9
BASIC as a programming language was devised in 1964 at Dartmouth College in the USA as a teaching language. This very first version of BASIC had support for matrix operations in the form of the MAT keyword. Subsequent versions dropped that keyword from the language, hence my library. Dropping that keyword and support may have proved short-sighted to an extent as their use in computer graphics has only grown. Your graphics card performs loads of calculations, per second, involving matrices. But you ask, are there any real world problems that they are good for? Examine this from the ancient (in computing terms) book "Illustrating BASIC" (1977) -

Problem -

There are 3 salespersons who sell a range of 4 products. Here are last weeks sales figures -

Code: (Select All)
                        Product       
Salesperson Maglets Scropers Gimples Nuckers
Mr. Hogg       5        2       0      10
Ms. Burnbra    3        5       2       5
M.  Chauvin   20        0       0       0

Each of those products has a sale price per item and earns the salesperson a set commission on each item sold -

Code: (Select All)
              Price List     
Product    Price  Commission
Maglets     1.50     0.20
Scropers    2.80     0.40
Gimples     5.00     1.00
Nuckers     2.00     0.50

The question is who made the most money for the company and how much commission was paid to each salesperson?

This can be solved in two different ways. Either a bespoke routine can be written to do it or you can use my matrix library -

Common to both approaches

Code: (Select All)
OPTION BASE 1

DIM Sales!(3, 4), Prices!(4, 2), Result!(3, 2)
REM skipping the code for reading in the data

Bespoke

Code: (Select All)
FOR Index1% = 1 to 2 'Columns of Prices!()
    FOR Index2% = 1 to 3 'Rows of Sales!()
        FOR Index3% = 1 to 4 'Columns of Sales!() and Rows of Prices!()
            Result!(Index2%, Index1%) = Result!(Index2%, Index1%) + Sales!(Index2%, Index3%) * Prices!(Index3%, Index1%)
        NEXT Index3%
    NEXT Index2%
NEXT Index1%

Or using my matrices library we just need (using the SINGLE precision library - accurate enough for our needs) -

SMatrixMultiply(Sales!(), Prices!(), Result!())

to achieve the same result. To prove this copy and paste the SINGLE precision version into a new BM file calling it "Mat_Single.BM". Then copy this code into a new BAS file

SALES.BAS
Code: (Select All)
'$INCLUDE: 'MATRIX.BI'

Dim Sales!(3, 4), Prices!(4, 2), Result!(3, 2)

For Index1% = 1 To 3
    For Index2% = 1 To 4
        Read Sales!(Index1%, Index2%)
    Next Index2%
Next Index1%
For Index1% = 1 To 4
    For Index2% = 1 To 2
        Read Prices!(Index1%, Index2%)
    Next Index2%
Next Index1%

SMatrixMultiply Sales!(), Prices!(), Result!()

Print "Results"
Print
Print "Seller        Sold  Earned"
Print "Mr. Hogg  "; "  "; Result!(1, 1); "  "; Result!(1, 2)
Print "Ms. Burnbra"; "  "; Result!(2, 1); "  "; Result!(2, 2)
Print "M.  Chauvin"; "  "; Str$(Result!(3, 1)); ".0"; "    "; Str$(Result!(3, 2)); ".0";
End

Data 5,2,0,10
Data 3,5,2,5
Data 20,0,0,0
Data 1.50,0.20
Data 2.80,0.40
Data 5.00,1.00
Data 2.00,0.50

'$INCLUDE: 'Mat_Single.BM'

and run it. 

TR
Reply


Messages In This Thread
Huge Matrices Library [Updated] - by TarotRedhand - 05-04-2022, 07:57 AM
RE: Huge Matrices Library - by TarotRedhand - 05-04-2022, 08:02 AM
RE: Huge Matrices Library - by TarotRedhand - 05-04-2022, 08:06 AM
RE: Huge Matrices Library - by TarotRedhand - 05-04-2022, 08:11 AM
RE: Huge Matrices Library - by TarotRedhand - 05-04-2022, 08:15 AM
RE: Huge Matrices Library - by TarotRedhand - 05-04-2022, 08:19 AM
RE: Huge Matrices Library - by TarotRedhand - 05-04-2022, 08:23 AM
RE: Huge Matrices Library - by madscijr - 05-10-2022, 02:37 PM
RE: Huge Matrices Library - by TarotRedhand - 05-17-2022, 11:42 AM



Users browsing this thread: 1 Guest(s)