Scala Matrix Inversion - math

Uh, yeah, I'd really need a quick input from someone without creator's eyes. Something's wrong in here, according to my scalacheck tests... but I don't really know enough about it to know where it's wrong.
case class Matrix(_1: (Float, Float, Float, Float), _2: (Float, Float, Float, Float),
_3: (Float, Float, Float, Float), _4: (Float, Float, Float, Float)) extends Immutable {
def invert = {
val _11 = _2._2 * _3._3 * _4._4 - _2._2 * _3._4 * _4._3 - _3._2 * _2._3 * _4._4
+_3._2 * _2._4 * _4._3 + _4._2 * _2._3 * _3._4 - _4._2 * _2._4 * _3._3
val _21 = -_2._1 * _3._3 * _4._4 + _2._1 * _3._4 * _4._3 + _3._1 * _2._3 * _4._4
-_3._1 * _2._4 * _4._3 - _4._1 * _2._3 * _3._4 + _4._1 * _2._4 * _3._3
val _31 = _2._1 * _3._2 * _4._4 - _2._1 * _3._4 * _4._2 - _3._1 * _2._2 * _4._4
+_3._1 * _2._4 * _4._2 + _4._1 * _2._2 * _3._4 - _4._1 * _2._4 * _3._2
val _41 = -_2._1 * _3._2 * _4._3 + _2._1 * _3._3 * _4._2 + _3._1 * _2._2 * _4._3
-_3._1 * _2._3 * _4._2 - _4._1 * _2._2 * _3._3 + _4._1 * _2._3 * _3._2
val _12 = -_1._2 * _3._3 * _4._4 + _1._2 * _3._4 * _4._3 + _3._2 * _1._3 * _4._4
-_3._2 * _1._4 * _4._3 - _4._2 * _1._3 * _3._4 + _4._2 * _1._4 * _3._3
val _22 = _1._1 * _3._3 * _4._4 - _1._1 * _3._4 * _4._3 - _3._1 * _1._3 * _4._4
+_3._1 * _1._4 * _4._3 + _4._1 * _1._3 * _3._4 - _4._1 * _1._4 * _3._3
val _32 = -_1._1 * _3._2 * _4._4 + _1._1 * _3._4 * _4._2 + _3._1 * _1._2 * _4._4
-_3._1 * _1._4 * _4._2 - _4._1 * _1._2 * _3._4 + _4._1 * _1._4 * _3._2
val _42 = _1._1 * _3._2 * _4._3 - _1._1 * _3._3 * _4._2 - _3._1 * _1._2 * _4._3
+_3._1 * _1._3 * _4._2 + _4._1 * _1._2 * _3._3 - _4._1 * _1._3 * _3._2
val _13 = _1._2 * _2._3 * _4._4 - _1._2 * _2._4 * _4._3 - _2._2 * _1._3 * _4._4
+_2._2 * _1._4 * _4._3 + _4._2 * _1._3 * _2._4 - _4._2 * _1._4 * _2._3
val _23 = -_1._1 * _2._3 * _4._4 + _1._1 * _2._4 * _4._3 + _2._1 * _1._3 * _4._4
-_2._1 * _1._4 * _4._3 - _4._1 * _1._3 * _2._4 + _4._1 * _1._4 * _2._3
val _33 = _1._1 * _2._2 * _4._4 - _1._1 * _2._4 * _4._2 - _2._1 * _1._2 * _4._4
+_2._1 * _1._4 * _4._2 + _4._1 * _1._2 * _2._4 - _4._1 * _1._4 * _2._2
val _43 = -_1._1 * _2._2 * _4._3 + _1._1 * _2._3 * _4._2 + _2._1 * _1._2 * _4._3
-_2._1 * _1._3 * _4._2 - _4._1 * _1._2 * _2._3 + _4._1 * _1._3 * _2._2
val _14 = -_1._2 * _2._3 * _3._4 + _1._2 * _2._4 * _3._3 + _2._2 * _1._3 * _3._4
-_2._2 * _1._4 * _3._3 - _3._2 * _1._3 * _2._4 + _3._2 * _1._4 * _2._3
val _24 = _1._1 * _2._3 * _3._4 - _1._1 * _2._4 * _3._3 - _2._1 * _1._3 * _3._4
+_2._1 * _1._4 * _3._3 + _3._1 * _1._3 * _2._4 - _3._1 * _1._4 * _2._3
val _34 = -_1._1 * _2._2 * _3._4 + _1._1 * _2._4 * _3._2 + _2._1 * _1._2 * _3._4
-_2._1 * _1._4 * _3._2 - _3._1 * _1._2 * _2._4 + _3._1 * _1._4 * _2._2
val _44 = _1._1 * _2._2 * _3._3 - _1._1 * _2._3 * _3._2 - _2._1 * _1._2 * _3._3
+_2._1 * _1._3 * _3._2 + _3._1 * _1._2 * _2._3 - _3._1 * _1._3 * _2._2
val det = _1._1 * _11 + _1._2 * _21 + _1._3 * _31 + _1._4 * _41
if (det == 0) this
else Matrix(
(_11, _12, _13, _14),
(_21, _22, _23, _24),
(_31, _32, _33, _34),
(_41, _42, _43, _44)
) * (1 / det)
}
def *(f: Float) = Matrix(
(_1._1 * f, _1._2 * f, _1._3 * f, _1._4 * f),
(_2._1 * f, _2._2 * f, _2._3 * f, _2._4 * f),
(_3._1 * f, _3._2 * f, _3._3 * f, _3._4 * f),
(_4._1 * f, _4._2 * f, _4._3 * f, _4._4 * f)
)
}
Also, can I load this Matrix into OpenGL or do I have to transpose it first. I really always get confused about this maths.

Inverting a matrix is usually a bad idea, because the calculations can be ill-conditioned.
If you want to solve a system of equations it's a better idea to do it using something like LU decomposition and forward-backward substitution, especially if you can reuse the decomposition to solve for several right hand side vectors.
This link shows a Java example for Gaussian elimination with pivoting.
Here's another thought: maybe you can just use Java libraries like Apache Commons Math, the successor to JAMA, in your application?
If you have a particular case in mind, I'd recommend entering it into Wolfram Alpha so you can see what the answer should be before you start coding.

I'm pretty sure Simplex3D implements this calculation (and very likely it's done correctly there).

If you want to play with numerics - by all means go ahead and do it yourself - you have some good suggestions from Jesper and duffymo (inverting matrices is not useful in practice - look into LU decomposition).
If however if you just want to Get Stuff DoneTM look into Scalala and Scalalab.
Either way you will need Linear Algebra background knowledge - which is incredibly useful math for lots of fields.

Look at Invertible matrix: Analytic solution on Wikipedia. The whole bunch of calculations at the top compute the adjugate of the matrix, from which the determinant is calculated, and the inverse is then 1 / det times the adjugate matrix.
The whole calculation is written out explicitly for a 4 x 4 matrix in your code, so if there's a bug in it will take some effort to check the whole thing. The Wikipedia articles explain how it's supposed to work.

Related

How does the code below give the same outcome as the code that follows it. I am trying to understand recursion

function multiply(arr, n) {
let product = 1;
for (let i = 0; i < n; i++) {
product *= arr[i];
}
return product;
}
A section in the course of freecode-camp states that the code above gives the same outcome as the code below; Im trying to wrap my head around this, somebody please help me with this;
Lets assume that we use an array of 1,2,3,4,5 and n of 5.
multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]
The top (iterative) code returns the product of the elements of the array.
What the second one does can be seen by repeated substitution:
multiply(arr, 5) == multiply(arr, 5 - 1) * arr[5 - 1]
== multiply(arr, 4) * arr[4]
== (multiply(arr, 4 - 1) * arr[4 - 1]) * arr[4]
== multiply(arr, 3) * arr[3] * arr[4]
== (multiply(arr, 3 - 1) * arr[3 - 1]) * arr[3] * arr[4]
== multiply(arr, 2) * arr[2] * arr[3] * arr[4]
== (multiply(arr, 2 - 1) * arr[2 - 1]) * arr[2] * arr[3] * arr[4]
== multiply(arr, 1) * arr[1] * arr[2] * arr[3] * arr[4]
== (multiply(arr, 1 - 1) * arr[1 - 1]) * arr[2] * arr[3] * arr[4]
== multiply(arr, 0) * arr[0] * arr[1] * arr[2] * arr[3] * arr[4]
at which point the process should stop with multiply(arr,0) returning 1, but your definition left out that part.

Null appearing at the end of for loop output in r

for (i in 1:9){
print(cat(replicate(i,"*"),"", replicate((9-i),'*')))
}
Output:
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * *NULL
* * * * * * * * * NULL
how to remove the null.
Remove print statement and use \n for new line.
for (i in 1:9){
cat(replicate(i,"*"),"", replicate((9-i),'*'), '\n')
}
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *

Printing a triangle in r

I'm trying to write some code to give me an output that looks like:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Given my limited r experience the closest I got was:
for(i in 1:5) {
print(strrep("*", i))
}
for(i in 4:1) {
print(strrep("*", i))
}
which gives me:
# *
# **
# ***
# ****
# *****
and
# ****
# ***
# **
# *
Any guidance would be really helpful.
Thanks in advance!
Pad extra space
for(i in c(1:5, 4:1))
cat( paste0(strrep(" ", 5 - i), strrep("* ", i), "\n") )
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

Intersection between two vectors

Is there efficient way to find intersection between two vectors?
Ray is infinite so one way is to turn one vector to ray, find intersection between that ray and other vector. Then if there is intersection, find distance between intersection and first vector.
If it is 0 then there is intersection between two vectors, if i don't miss something.
But is there better way in three.js?
That is more a math question than a three.js one, as you mentioned you could use existing methods on the Ray object and work your way out from there, but I doubt this would be optimal as you are concerned about performance.
The right approach is probably to determine the shortest distance between two segments and if this distance is zero then they intersect. You can find a similar discussion on that thread: The Algorithm to Find the Point of Intersection of Two 3D Line Segment
And here I found a C algorithm that does exactly that, it should translate to Javascript without much efforts:
typedef struct {
double x,y,z;
} XYZ;
/*
Calculate the line segment PaPb that is the shortest route between
two lines P1P2 and P3P4. Calculate also the values of mua and mub where
Pa = P1 + mua (P2 - P1)
Pb = P3 + mub (P4 - P3)
Return FALSE if no solution exists.
*/
int LineLineIntersect(
XYZ p1,XYZ p2,XYZ p3,XYZ p4,XYZ *pa,XYZ *pb,
double *mua, double *mub)
{
XYZ p13,p43,p21;
double d1343,d4321,d1321,d4343,d2121;
double numer,denom;
p13.x = p1.x - p3.x;
p13.y = p1.y - p3.y;
p13.z = p1.z - p3.z;
p43.x = p4.x - p3.x;
p43.y = p4.y - p3.y;
p43.z = p4.z - p3.z;
if (ABS(p43.x) < EPS && ABS(p43.y) < EPS && ABS(p43.z) < EPS)
return(FALSE);
p21.x = p2.x - p1.x;
p21.y = p2.y - p1.y;
p21.z = p2.z - p1.z;
if (ABS(p21.x) < EPS && ABS(p21.y) < EPS && ABS(p21.z) < EPS)
return(FALSE);
d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z;
d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z;
d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z;
d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z;
d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z;
denom = d2121 * d4343 - d4321 * d4321;
if (ABS(denom) < EPS)
return(FALSE);
numer = d1343 * d4321 - d1321 * d4343;
*mua = numer / denom;
*mub = (d1343 + d4321 * (*mua)) / d4343;
pa->x = p1.x + *mua * p21.x;
pa->y = p1.y + *mua * p21.y;
pa->z = p1.z + *mua * p21.z;
pb->x = p3.x + *mub * p43.x;
pb->y = p3.y + *mub * p43.y;
pb->z = p3.z + *mub * p43.z;
return(TRUE);
}

Allow only 0 - 100 as percent values in form type

I have a percentage field in my form and I want it have values between 0 - 100 as number. How can I force it and return validation otherwise?
Currently I can even enter 100.10 which is wrong.
Note: 0.00 and 100.00 is allowed
Thanks
ENTITY:
/**
* #var decimal
* #ORM\Column(type="decimal", precision=5, scale=2)
*/
protected $vatRate;
FORM TYPE:
->add(
'vatRate',
'percent',
array('type' => 'integer', 'attr' => array('min' => '0', 'max' =>` '100'))
)
Add a constraint to the entity
use Symfony\Component\Validator\Constraints as Assert;
/* ... */
/**
* #var decimal
* #ORM\Column(type="decimal", precision=5, scale=2)
* #Assert\Range(
* min = 0,
* max = 100,
* minMessage = "Min % is 0",
* maxMessage = "Max % is 100"
* )
*/
protected $vatRate;
Use Range constraint in your entity:
http://symfony.com/doc/current/reference/constraints/Range.html
use Symfony\Component\Validator\Constraints as Assert;
/**
* #var decimal
* #ORM\Column(type="decimal", precision=5, scale=2)
* #Assert\Range(
* min = 0,
* max = 100
* )
*/
protected $vatRate;

Resources