www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Somewhat OT: defining algebras in D

reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
So I was bored in a meeting and decided to implement a generic 
template for defining complex numbers, dual numbers, quaternions 
and many other possible algebras by simply defining a set of 
rules and the components on which they act:


alias quaternion = Algebra!(
         float,
         "1,i,j,k",
         op("1",     any)  = any,
         op("i,j,k", self) = "-1",
         op("i",     "j")  = "k".antiCommutative,
         op("j",     "k")  = "i".antiCommutative,
         op("k",     "i")  = "j".antiCommutative,
     );

source:

https://gist.github.com/Biotronic/833680b37d4afe774c8562fd21554c6b

--
   Simen
Feb 08 2018
next sibling parent reply Amorphorious <Amorphorious gmail.com> writes:
On Thursday, 8 February 2018 at 15:23:05 UTC, Simen Kjærås wrote:
 So I was bored in a meeting and decided to implement a generic 
 template for defining complex numbers, dual numbers, 
 quaternions and many other possible algebras by simply defining 
 a set of rules and the components on which they act:


 alias quaternion = Algebra!(
         float,
         "1,i,j,k",
         op("1",     any)  = any,
         op("i,j,k", self) = "-1",
         op("i",     "j")  = "k".antiCommutative,
         op("j",     "k")  = "i".antiCommutative,
         op("k",     "i")  = "j".antiCommutative,
     );

 source:

 https://gist.github.com/Biotronic/833680b37d4afe774c8562fd21554c6b

 --
   Simen
It would be nice if you learned how to document your code. It's not always easy for someone on the outside to be able to pick it up and it ultimately means your hard work will be less used as it could be. I know that sometimes comments can be redundant but it can also provide a better understanding. For example, it seems that you are using a group presentation to define the a algebra... but a few examples are not enough to provide a complete context in what it can be used for besides the example. This requires understanding the details in detail, which can be too time consuming for some. For example, can it be used to define an algebra on sets? If not, could it be modified to do so easily? To answer that one probably has to know how the code works in detail... which means spending time, which then goes to if it is worth it over a new implementation, etc.
Feb 08 2018
parent reply "Nick Sabalausky (Abscissa)" <SeeWebsiteToContactMe semitwist.com> writes:
On 02/08/2018 04:37 PM, Amorphorious wrote:
 On Thursday, 8 February 2018 at 15:23:05 UTC, Simen Kjærås wrote:
 So I was bored in a meeting and decided to implement a generic 
 template for defining complex numbers, dual numbers, quaternions and 
 many other possible algebras by simply defining a set of rules and the 
 components on which they act:

 source:

 https://gist.github.com/Biotronic/833680b37d4afe774c8562fd21554c6b
Cool. Took me a while to start to understand it and still not 100% grokked (partly because I've never quite been able to fully grasp quaternion math (at least, beyond Unity3D's ultra-easy abstraction for it) and never heard of dual numbers before), but staring at the complex number example helped see how this works. It's a very cool idea!
 It would be nice if you learned how to document your code. It's not 
 always easy for someone on the outside to be able to pick it up and it 
 ultimately means your hard work will be less used as it could be. I know 
 that sometimes comments can be redundant but it can also provide a 
 better understanding.
 
Well, that's the difference between a formal library package release vs sharing a working proof of concept jotted down to pass time ;)
Feb 08 2018
parent reply Amorphorious <Amorphorious gmail.com> writes:
On Friday, 9 February 2018 at 02:40:06 UTC, Nick Sabalausky 
(Abscissa) wrote:
 On 02/08/2018 04:37 PM, Amorphorious wrote:
 On Thursday, 8 February 2018 at 15:23:05 UTC, Simen Kjærås 
 wrote:
 So I was bored in a meeting and decided to implement a 
 generic template for defining complex numbers, dual numbers, 
 quaternions and many other possible algebras by simply 
 defining a set of rules and the components on which they act:

 source:

 https://gist.github.com/Biotronic/833680b37d4afe774c8562fd21554c6b
Cool. Took me a while to start to understand it and still not 100% grokked (partly because I've never quite been able to fully grasp quaternion math (at least, beyond Unity3D's ultra-easy abstraction for it) and never heard of dual numbers before), but staring at the complex number example helped see how this works. It's a very cool idea!
quats are just complex numbers with extra variables like i. Each one is the square root of -1, but they are obviously incompatible. i^2 = j^2 = k^2 = -1 https://en.wikipedia.org/wiki/Quaternion See the multiplication table. It's just an "extension" of complex numbers. They are homomorphic to 4x4 matrices but sometimes easier to work with. Nothing really special about them... sorta like pepsi vs coke.
 It would be nice if you learned how to document your code. 
 It's not always easy for someone on the outside to be able to 
 pick it up and it ultimately means your hard work will be less 
 used as it could be. I know that sometimes comments can be 
 redundant but it can also provide a better understanding.
 
Well, that's the difference between a formal library package release vs sharing a working proof of concept jotted down to pass time ;)
Yes, but he can go back an add some friendly text at some point... He knows most about it so it is much easier and shouldn't take more than a few mins.
Feb 09 2018
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Friday, 9 February 2018 at 15:45:11 UTC, Amorphorious wrote:
 On Friday, 9 February 2018 at 02:40:06 UTC, Nick Sabalausky 
 (Abscissa) wrote:
 Well, that's the difference between a formal library package 
 release vs sharing a working proof of concept jotted down to 
 pass time ;)
Yes, but he can go back an add some friendly text at some point... He knows most about it so it is much easier and shouldn't take more than a few mins.
Indeed I can, and I have: https://gist.github.com/Biotronic/833680b37d4afe774c8562fd21554c6b Doing so after a long, tiring day at work for something I just did to pass the time, though - not worth it when I wanted a shower and some sleep. Luckily, today was just as boring, so I cleaned up the syntax a bit, added more sensible error messages, and even made it do the right thing by default when you leave out a rule: alias complex = Algebra!( float, "1,i", "i" * "i".op = -1); alias dual = Algebra!( float, "1,e", "e" * "e".op = 0); alias splitComplex = Algebra!( float, "1,j", "j" * "j".op = 1 ); alias quaternion = Algebra!( float, "1,i,j,k", "i,j,k" * "i,j,k".op = -1, "i,j,k" * "j,k,i".op = "k,i,j".antiCommutative ); -- Simen
Feb 09 2018
parent Amorphorious <Amorphorious gmail.com> writes:
On Friday, 9 February 2018 at 17:10:11 UTC, Simen Kjærås wrote:
 On Friday, 9 February 2018 at 15:45:11 UTC, Amorphorious wrote:
 On Friday, 9 February 2018 at 02:40:06 UTC, Nick Sabalausky 
 (Abscissa) wrote:
 Well, that's the difference between a formal library package 
 release vs sharing a working proof of concept jotted down to 
 pass time ;)
Yes, but he can go back an add some friendly text at some point... He knows most about it so it is much easier and shouldn't take more than a few mins.
Indeed I can, and I have: https://gist.github.com/Biotronic/833680b37d4afe774c8562fd21554c6b Doing so after a long, tiring day at work for something I just did to pass the time, though - not worth it when I wanted a shower and some sleep. Luckily, today was just as boring, so I cleaned up the syntax a bit, added more sensible error messages, and even made it do the right thing by default when you leave out a rule: alias complex = Algebra!( float, "1,i", "i" * "i".op = -1); alias dual = Algebra!( float, "1,e", "e" * "e".op = 0); alias splitComplex = Algebra!( float, "1,j", "j" * "j".op = 1 ); alias quaternion = Algebra!( float, "1,i,j,k", "i,j,k" * "i,j,k".op = -1, "i,j,k" * "j,k,i".op = "k,i,j".antiCommutative ); -- Simen
Lol, thanks, but that wasn't much. What I was thinking is a sort of paragraph of text at the top that sorta describes the overall conceptualization/overview. One still has to understand how all the pieces fit and the only way to learn that is by learning the pieces. And English equivalent of D code isn't really all that useful because anyone that knows D can understand the D code. E.g., // Create a canonical list of rules from compound rules. template Canonicalize(string units, Rules...) is not really all that useful rather, which I'm making up some type of overview: "Creates algebraic structures[Algebra!(base type, relators/varables, relations...)] using composition rules(relations) to define the structure. To define an algebra type, say, the quaternions of base type float, alias quaternion = Algebra!( float, "1,i,j,k", "i,j,k" * "i,j,k".op = -1, "i,j,k" * "j,k,i".op = "k,i,j".antiCommutative ); which can be used a type: quaternion(a,b,c,d) q; // Defines the quaternion q = a + bi + cj + dk. -- the main bulk of defining an algebra as the relations. Each relation consists of a string expressions sk involving the realtors expressing the relation equation as f(s1,..,fn).op = s0 where f is an algebraic function and s0 is a the special relator(whatever). relators can be used as a csl. e.g., for quats, "i,j,k" * "i,j,k".op = -1 says that i*i = -1, j*j = -1, k*k = -1. Each of the rules could have been specified individually: "i" * "i".op = -1 "j" * "j".op = -1 "k" * "k".op = -1 One can append a string expression using "property syntax": "i,j,k" * "j,k,i".op = "k,i,j".antiCommutative .antiCommutative states that the relator is anti commutative. Other properties are: ........ ........ ........ (no others?) Once and algebra is established it can then act as any complex type and used as a functioning algebra. " etc. The idea here is that to write a few paragraphs of English text so that one can read from the start to finish and understanding the general idea of what is going on without having to read through the entire code(which is much longer) and try to put the pieces together. The code I presented is what gleaned from what you are trying to accomplish and my knowledge of abstract algebra(which most don't have). What's important is that the average person shouldn't have to learn your code to know how to use it. It is not important of the details(of which I haven't looked at) but just how to use them. While you give examples, which is good, the only problem is one can't necessarily glean from those examples how to create their own algebras. e.g., what about the GL(n,Z)? Is it possible? alias GLnZ = Algebra!( Matrix!(n,Z), "e_i_j" = 1, ); Obviously I doubt this would work, but it would be cool if it did, but the only way I could know is if I learned your code and tried to figure out how to massage it to work, if it could.. and that may take up more time than it would be to do my own implementation which I would understand better. Hence adding clarifications to the code then help one make better decisions: e.g., "Base Type must be a D primitive". (again, the only way one can know if your code supports a general type is to learn the code, which may require one to learn all the code). I'm not saying you have to do this, of course... just saying it would be more more helpful. I'm not sure how general your code is but if one could basically make arbitrary algebraic types, it would be very cool indeed and probably be pretty useful when people learned where to use it.
Feb 09 2018
prev sibling parent Mark <smarksc gmail.com> writes:
On Thursday, 8 February 2018 at 15:23:05 UTC, Simen Kjærås wrote:
 So I was bored in a meeting and decided to implement a generic 
 template for defining complex numbers, dual numbers, 
 quaternions and many other possible algebras by simply defining 
 a set of rules and the components on which they act:


 alias quaternion = Algebra!(
         float,
         "1,i,j,k",
         op("1",     any)  = any,
         op("i,j,k", self) = "-1",
         op("i",     "j")  = "k".antiCommutative,
         op("j",     "k")  = "i".antiCommutative,
         op("k",     "i")  = "j".antiCommutative,
     );

 source:

 https://gist.github.com/Biotronic/833680b37d4afe774c8562fd21554c6b

 --
   Simen
Nice. An interesting potential extension is to support composition (for instance, defining the complex numbers using the template and then defining the quaternions as an two-dimensional algebra over the complex).
Feb 09 2018