PR #2475: Expression syntax extension

Merged, abandoned or rejected pull requests are moved here to clear the main Pull Requests forum.
User avatar
DeepSOIC
Posts: 7600
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: PR #2475: Expression syntax extension

Post by DeepSOIC » Mon Sep 30, 2019 2:29 pm

wmayer wrote:
Mon Sep 30, 2019 12:22 pm
This is true for numbers (1/X = X^-1), but there is a rigorous mathematical notion behind it, that allows to define X^-3 or X^(1/2), but for matrices or Placements, what would mean :
On Wikipedia you can find some information for square roots of a matrix but it's only well-defined for positive-definite matrices while for arbitrary matrices it causes ambiguities. But what when using 1/3 as exponent instead? Here I couldn't find a valid definition. So, a power function for arbitrary exponents makes probably not much sense (especially not in a CAD program) and it should be limited to integers only as done by the current implementation.
I've been actually wanting this arbitrary exponent as a way to interpolate between placements.
You don't have to define, what ^(1/3) means, as 1/3 can't be precisely represented by a float anyway. A fractional part of float is a sum of fractions:
bit1*1/2 + bit2*1/4 + bit3*1/8 +...
Power-of-two fractional powers can be easily calculated by multiplying M^(1/2) by itself.
User avatar
Zolko
Posts: 842
Joined: Mon Dec 17, 2018 10:02 am

Re: PR #2475: Expression syntax extension

Post by Zolko » Mon Sep 30, 2019 2:42 pm

wmayer wrote:
Mon Sep 30, 2019 2:07 pm
As shown here the inverted placement of
y = R * x + t is x = Q * y - Q * t
where Q is the transposed and inverse of R. Now make the block matrices (R|t) and (Q|-Q*t) and imagine that for both matrices the fourth row is (0, 0, 0, 1). I claim that (Q|-Q*t) is the inverse of (R|t)
yes, OK, I understand. Mathematically it's correct, I didn't think about the last 1 in the 4x4 corner, I assumed all 0-s. But now, you propose to calculate the inverse of a Placement by calculating the inverse of the 4x4 matrix, instead of transposing the orthonormal 3x3 R matrix and changing the sign of the translation vector T ? What algorithm do you propose to invert the non-orthonormal 4x4 matrix ?

What I claim is that inverting a 3x3 Rotation matrix and 4x4 Placement matrix use different algorithms (for a Rotation it's a simple transpose, for a Placement it's not a simple transpose), and making a generic matrix.power() function that inverts the matrices of Placement and Rotation in the same manner is a questionable solution. Whereas it is a good solution for multiplications, because the generic matrix multiplication will bring in both cases optimal results.

And what I still didn't see is a practical use-case for the matrix.power(N) feature with N≠-1.
try the Assembly4 workbench for FreCAD v0.19
install with Tools > Addon Manager > Assembly4 — tutorials here and here
wmayer
Site Admin
Posts: 15716
Joined: Thu Feb 19, 2009 10:32 am

Re: PR #2475: Expression syntax extension

Post by wmayer » Mon Sep 30, 2019 3:10 pm

What algorithm do you propose to invert the non-orthonormal 4x4 matrix ?
Maybe Gauß-Jordan. Or you can check the Eigen3 library which has a lot of optimized algorithms. However, the code is partially difficult to understand as it's heavily template-based.
But now, you propose to calculate the inverse of a Placement by calculating the inverse of the 4x4 matrix, instead of transposing the orthonormal 3x3 R matrix and changing the sign of the translation vector T ?
No. The Placement internally uses a quaternion and there it's trivial to invert it because you only have to either multiply its Q0-Q2 or its Q3 with -1. And the translational part is only a simple multiplication with the inverted quaternion.
And what I still didn't see is a practical use-case for the matrix.power(N) feature with N≠-1.
Especially in FreeCAD there is probably no obvious use-case where it's really needed. But in a general context this is e.g. very useful in graph theory.
wmayer
Site Admin
Posts: 15716
Joined: Thu Feb 19, 2009 10:32 am

Re: PR #2475: Expression syntax extension

Post by wmayer » Mon Sep 30, 2019 3:13 pm

DeepSOIC wrote:
Mon Sep 30, 2019 2:29 pm
wmayer wrote:
Mon Sep 30, 2019 12:22 pm
This is true for numbers (1/X = X^-1), but there is a rigorous mathematical notion behind it, that allows to define X^-3 or X^(1/2), but for matrices or Placements, what would mean :
On Wikipedia you can find some information for square roots of a matrix but it's only well-defined for positive-definite matrices while for arbitrary matrices it causes ambiguities. But what when using 1/3 as exponent instead? Here I couldn't find a valid definition. So, a power function for arbitrary exponents makes probably not much sense (especially not in a CAD program) and it should be limited to integers only as done by the current implementation.
I've been actually wanting this arbitrary exponent as a way to interpolate between placements.
You don't have to define, what ^(1/3) means, as 1/3 can't be precisely represented by a float anyway. A fractional part of float is a sum of fractions:
bit1*1/2 + bit2*1/4 + bit3*1/8 +...
Power-of-two fractional powers can be easily calculated by multiplying M^(1/2) by itself.
OK, when you need such a function then of course we can try to extend it.
User avatar
DeepSOIC
Posts: 7600
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: PR #2475: Expression syntax extension

Post by DeepSOIC » Mon Sep 30, 2019 3:19 pm

wmayer wrote:
Mon Sep 30, 2019 3:13 pm
OK, when you need such a function then of course we can try to extend it.
Well, I can't say I "need" it, I have already implemented the interpolation by simply interpolating translation and quaternion separately. It is more of a curiosity.

If M^(1/2) isn't a placement matrix, then it isn't going to be useful for me, I think.
User avatar
Zolko
Posts: 842
Joined: Mon Dec 17, 2018 10:02 am

Re: PR #2475: Expression syntax extension

Post by Zolko » Mon Sep 30, 2019 3:34 pm

wmayer wrote:
Sat Sep 28, 2019 3:02 pm
The branch is merged now.
OK, so with FreeCAD_0.19-18353-Linux-Conda_Py3Qt5_glibc2.12-x86_64.AppImage the multiplication of Placements works, I can do:

Code: Select all

Cuve.Placement * Cuve#LCS_1.Placement * constr_Screw_CHC_1.AttachmentOffset
And it does what it's supposed to do. I have to check the inverse part now.
try the Assembly4 workbench for FreCAD v0.19
install with Tools > Addon Manager > Assembly4 — tutorials here and here
User avatar
Zolko
Posts: 842
Joined: Mon Dec 17, 2018 10:02 am

Re: PR #2475: Expression syntax extension

Post by Zolko » Tue Oct 01, 2019 6:26 am

and the inverse also works :

Code: Select all

Cuve.Placement * Cuve#LCS_1.Placement * constr_Screw_CHC_1.AttachmentOffset * Screw_CHC#LCS_0.Placement ^ -1
This is great news, thank-you. I'll update now the Asm4 workbench for the new syntax.
try the Assembly4 workbench for FreCAD v0.19
install with Tools > Addon Manager > Assembly4 — tutorials here and here
triplus
Posts: 9425
Joined: Mon Dec 12, 2011 4:45 pm

Re: PR #2475: Expression syntax extension

Post by triplus » Fri Oct 04, 2019 2:24 pm

Therefore now Assembly 4 module is fully compatible with FreeCAD 0.19?
User avatar
Zolko
Posts: 842
Joined: Mon Dec 17, 2018 10:02 am

Re: PR #2475: Expression syntax extension

Post by Zolko » Fri Oct 04, 2019 9:55 pm

triplus wrote:
Fri Oct 04, 2019 2:24 pm
Therefore now Assembly 4 module is fully compatible with FreeCAD 0.19?
yes, I've just finished the update to the new syntax. I'll make a new release 0.6 tomorrow, the current 0.5 is not compatible (you still need the -asm3 branch). Works like before, I corrected some bugs on the way. There are still truckloads of bugs probably, but we'll have to see them if/when people test.
try the Assembly4 workbench for FreCAD v0.19
install with Tools > Addon Manager > Assembly4 — tutorials here and here
triplus
Posts: 9425
Joined: Mon Dec 12, 2011 4:45 pm

Re: PR #2475: Expression syntax extension

Post by triplus » Fri Oct 04, 2019 11:08 pm

That is really good to hear. Thanks to all involved in making it happen. Don't forget about tutorial.

P.S. Now i guess Assembly 3 module still needs some work. For being able to work with FreeCAD 0.19? I have seen AppImage, and likely all other binaries based on Conda, already have SolveSpace library included.
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests