![]() |
Forum Index : Microcontroller and PC projects : CMM2: The maths pack included in V5.05.04b2b
Author | Message | ||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10067 |
Someone raised improved maths capability in the CMM2 and this is something I'd already been working on. Of course, pretty much any math can be programmed in Basic but there are speed advantages to coding looping structures in C and there is the advantage that once debugged they are there for everyone without re-inventing the wheel. V5.05.04b2b has one new command MATH and one new function MATH() and these are used with subcommands to perform a wide variety of tasks. I've been guided in developing these both by my own interests and background and also by cdeagle our resident mathematical guru. Note: 2 dimensional maths matrices are always specified DIM matrix(n_columns, n_rows) and of course the dimensions respect OPTION BASE. It was done this way so that defining a matrix seems most logical. i.e OPTION BASE 1 DIM matrix(3,3)=(1,2,3,4,5,6,7,8,9) math m_print matrix() 1.0000, 2.0000, 3.0000 4.0000, 5.0000, 6.0000 7.0000, 8.0000, 9.0000 MATH() FUNCTION Simple functions MATH(ATAN3 x,y) MATH(COSH a) MATH(LOG10 a) MATH(SINH a) MATH(TANH a) Simple stats MATH(MEAN a())'returns the average of all values in the a() array, a() can have any number of dimensions MATH(MEDIAN a())'returns the median of all values in the a() array, a() can have any number of dimensions MATH(SD a())'returns the standard deviation of all values in the a() array, a() can have any number of dimensions MATH(SUM a()) 'returns the sum of all values in the a() array, a() can have any number of dimensions MATH(CORREL a(),b()) ' returns the Pearson correlation coefficient of the arrays a() and b() which must have the same cardinality MATH(CHI a()) 'returns the Pearson's chi-squared value of the two dimensional array a()) MATH(CHI_p a()) 'returns the associated probablity in % of the Pearson's chi-squared value of the two dimensional array a()) Vector arithmetic MATH(MAGNITUDE a()) 'returns the magnitude of the vector a(). The vector can have any number of elements MATH(DOTPRODUCT a(), b())'returns the dot product of two vectors. The vectors can have any number of elements but must have the same cardinality MATH COMMAND Simple array arithmetic MATH SET a, b() 'sets all elements in array b() to the value a. Note this is the fastest way of clearing an array by setting it to zero MATH SCALE a(), b ,c() 'This scales the matrix a() by the scalar b and puts the answer in c(). Works for arrays of any dimensionality of both integer and float and can convert between. Setting b to 1 is optimised and is the fastest way of copying an entire array Matrix arithmetic MATH M_PRINT a() 'quick mechanism to print a 2D matrix one row per line line MATH M_TRANSPOSE a(), b()' transpose matrix a() and put the answer in matrix b(), both arrays must be 2D but need not be square. If not square then the arrays must be dimensioned a(m,n) b(n,m) MATH M_MULT a(), b(), c() multiply the arrays a() and b() and put the answer in c. All arrays must be 2D but need not be square. If not square then the arrays must be dimensioned a(m,n) b(p,m) ,c(p,n) Vector arithmetic MATH V_PRINT a() 'quick mechanism to print a small array on a single line MATH V_UNIT a(), b() 'converts a vector a() to unit scale and puts the answer in b()(sqr(x*x + y*y +.......)=1: no limit on number of elements in vector MATH V_MULT a(), b(), c() ' multiplies matrix a() and vector b() returning vector c(). The vectors and the arrays can be any size but must have the same cardinality. MATH V_CROSS a(), b(), c() ‘calculate the cross product of two three element vectors a() and b() and put the answer in c() Quaternion arithmetic Quaternions are stored as a 5 element array w,x,y,z,magnitude MATH Q_INVERT sourcequaternion(), destinationquaternion() ‘inverts the quaternion MATH Q_VECTOR sourcevector(), destinationquaternion() ‘converts a vector to a normalised quaternion vector with the original magnitude stored MATH Q_CREATE theta, x, y, z, rotatequaternion() ‘generates a normalised rotation quaternion to rotate quaternion vectors around axis x,y,z by an angle of theta MATH Q_MULT sourcequaternion1(), sourcequaternion2(), destinationquaternion() ‘ multiplies two quaternions MATH Q_ROTATE , rotatequaternion(), sourcequaternion(), destinationquaternion() ‘rotates the source quaternion vector by the rotate quaternion Edited 2020-07-03 19:24 by matherp |
||||
cdeagle Senior Member ![]() Joined: 22/06/2014 Location: United StatesPosts: 265 |
Attached is a zip file that contains an updated version of the MMBASIC program that computes rise and set of the moon. It uses several of the new MATH functions included in the latest release of MMBASIC. It also writes the program output to a simple text file named "moon_riseset.txt". moon_riseset.zip |
||||
mkopack73 Senior Member ![]() Joined: 03/07/2020 Location: United StatesPosts: 261 |
Awesome!!! That will certainly be useful and was way more extensive than I was hoping for! ![]() |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10067 |
Typo: MATH V_UNIT should read: MATH V_NORMALISE a(), b() 'converts a vector a() to unit scale and puts the answer in b() '(sqr(x*x + y*y +.......)=1)there is no limit on number of elements in vector |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |