Recent

Author Topic: Looking for guidance on an OO implementation issue  (Read 19136 times)

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Looking for guidance on an OO implementation issue
« on: November 19, 2014, 11:52:09 am »
Hi all,

I am currently using FPC 2.6.4. and Lazarus 1.2.4. under Windows 7/64 on an Intel i5 430M (Westmere) core.

My project is about developing an arbitrary precision math library in Pascal (and optimized assembler). It looks like I will soon finish a first stable implementation of fundamental arithmetics and I am now looking for some guidance on how to best implement a class system for various number types. As I am pretty new to OO programming I thought I'd ask the experts what should be considered.

Here is the set-up

* there are several basic ordered number types that should be supported - natural, integer, rationals, reals, complex and quaternions

* only the first four are primitives, while for the complex and quaternions each component can be of one of the lower types. This means that for example a complex number must not neccessarily be a compound of two reals but it could also be a compound of two integers. This implies that in the first case all operations on complex numbers can be computed while in the later case e.g. sin(z) does not make sense and should yield a suitable response.

What I would like to come up with is a class system that supports the above described set-up and later on would allow for operator overloading to support easy use.

Sorry about the mathematical language above but I don't know how to express my ideas in a simple way otherwise. If the above is still intelligible please ask and I'll try my best to rephrase or explain different.

So, any takers with ideas how to define & structure a suitable class system?

Cheers,
MathMan

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Looking for guidance on an OO implementation issue
« Reply #1 on: November 19, 2014, 12:41:29 pm »
Quote
sin(z) does not make sense
Maybe I don't understand correctly, but the sine of a complex number does make sense: http://en.wikipedia.org/wiki/Sine#Sine_with_a_complex_argument

sfeinst

  • Full Member
  • ***
  • Posts: 230
Re: Looking for guidance on an OO implementation issue
« Reply #2 on: November 19, 2014, 01:07:26 pm »
I do not have any of the math knowledge, nor do I know if any of these items have been created in an OO fashion already in some other library, but the way I would approach this from an object perspective is (this assumes complex and quaternions can not be made up of complex and quaternions) :

Create a class say MathNumbers
Class MathPrimitives that descends from MathNumbers
Classes MathNatural, MathInteger, MathRationals and MathReals all descending from MathPrimitives
Class MathComplex that descend from MathNumbers
Classes MathMainComplex and MathQuaternions that descend from MathComplex.

Where possible, I prefer to descend from an abstract type class (one that I do not intend to instantiate) and not from classes that I will instantiate (past experience has shown me that I will end up either creating the common parent eventually or be forced to write extra code I don't want because the classes start deviating from each other over time).  Just a personal preference.  This is why I would create the common parent for complex and quaternions as opposed to inheriting one from the other.

Also, I just used those names for no reason.  I would really sit down and try to come up with meaningful names that would not conflict with existing names.  I don't like MathComplex and MathMainComplex, but I did not feel like trying to come up with better names.

sfeinst

  • Full Member
  • ***
  • Posts: 230
Re: Looking for guidance on an OO implementation issue
« Reply #3 on: November 19, 2014, 01:57:46 pm »
I probably should have written in that design that the MathMainomplex and MathQuaternions and possibly MathComplex would use MathPrimitives internally.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Looking for guidance on an OO implementation issue
« Reply #4 on: November 19, 2014, 02:22:54 pm »
Well lets see how many primitives do you have in total? Write them down. Do they have anything in common with each other? what is that? What is different between them eg what makes the natural different from a real? How about integer, reals and rationals? Do make your results public here so we comment and refine the design together.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Looking for guidance on an OO implementation issue
« Reply #5 on: November 19, 2014, 02:48:56 pm »
Quote
sin(z) does not make sense
Maybe I don't understand correctly, but the sine of a complex number does make sense: http://en.wikipedia.org/wiki/Sine#Sine_with_a_complex_argument

Look at the following - a complex number z is usually defined as z := a+i*b where a, b are real. For this setup the computation of sin( z ) is defined. However if one restricts a, b to be natural numbers only (or integers or rationals) then the computation of sin( z ) is no longer defined - meaning you either have to state an error in computation or e.g. define a replacement like sin( a+i*b ) = ( 0, 0 ) iff the elements of the complex number must be in N.

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Looking for guidance on an OO implementation issue
« Reply #6 on: November 19, 2014, 02:58:58 pm »
I do not have any of the math knowledge, nor do I know if any of these items have been created in an OO fashion already in some other library, but the way I would approach this from an object perspective is (this assumes complex and quaternions can not be made up of complex and quaternions) :

Create a class say MathNumbers
Class MathPrimitives that descends from MathNumbers
Classes MathNatural, MathInteger, MathRationals and MathReals all descending from MathPrimitives
Class MathComplex that descend from MathNumbers
Classes MathMainComplex and MathQuaternions that descend from MathComplex.

Where possible, I prefer to descend from an abstract type class (one that I do not intend to instantiate) and not from classes that I will instantiate (past experience has shown me that I will end up either creating the common parent eventually or be forced to write extra code I don't want because the classes start deviating from each other over time).  Just a personal preference.  This is why I would create the common parent for complex and quaternions as opposed to inheriting one from the other.

Also, I just used those names for no reason.  I would really sit down and try to come up with meaningful names that would not conflict with existing names.  I don't like MathComplex and MathMainComplex, but I did not feel like trying to come up with better names.

Thanks for the response. Don't worry about the lack on mathematics - your answer is what I was looking for, namely somebody looking at it from an OO perspective. Your assumption is in fact correct - elements of a higher order number (wrt the number hierarchy) can only fall back to lower order numbers. Meaning a complex can at most fall back to reals for it's elements, while quaternions can fall back to complex (which yields so called hyper-complex numbers used afaik in some quantum physics etc.). That at least quarantees an end to recursive resolution which will have to take place if I understood your Approach correct.

Cheers,
MathMan

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Looking for guidance on an OO implementation issue
« Reply #7 on: November 19, 2014, 03:05:01 pm »
Well lets see how many primitives do you have in total? Write them down. Do they have anything in common with each other? what is that? What is different between them eg what makes the natural different from a real? How about integer, reals and rationals? Do make your results public here so we comment and refine the design together.

Thanks for your offer! It will take me some time though to write this down in a concise manner. I'll try to post a response with an attached document within the next week and maybe we can take it on from there? As I will propably not finish the arithmetic layer before end of this year we should have some time to think about a good approach ...

Kind regards,
MathMan

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Looking for guidance on an OO implementation issue
« Reply #8 on: November 19, 2014, 03:12:48 pm »
Well lets see how many primitives do you have in total? Write them down. Do they have anything in common with each other? what is that? What is different between them eg what makes the natural different from a real? How about integer, reals and rationals? Do make your results public here so we comment and refine the design together.

Thanks for your offer! It will take me some time though to write this down in a concise manner. I'll try to post a response with an attached document within the next week and maybe we can take it on from there? As I will propably not finish the arithmetic layer before end of this year we should have some time to think about a good approach ...

Kind regards,
MathMan

Sure take your time the idea is to get a first hand experience in designing from zero your self instead of relying in existing patterns that might or might not fit your requirements.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

sfeinst

  • Full Member
  • ***
  • Posts: 230
Re: Looking for guidance on an OO implementation issue
« Reply #9 on: November 19, 2014, 04:09:59 pm »

Thanks for the response. Don't worry about the lack on mathematics - your answer is what I was looking for, namely somebody looking at it from an OO perspective. Your assumption is in fact correct - elements of a higher order number (wrt the number hierarchy) can only fall back to lower order numbers. Meaning a complex can at most fall back to reals for it's elements, while quaternions can fall back to complex (which yields so called hyper-complex numbers used afaik in some quantum physics etc.). That at least quarantees an end to recursive resolution which will have to take place if I understood your Approach correct.

Cheers,
MathMan

Based on your response, my design will not work as I did not realize quaternions can fall back to complex.  I was not going with recursive, it was making a common ancestor.  Since it is recursive a different design would be needed.  I can work out another design, but what taazz stated about working it out in the forum to learn more is probably a better idea.  So I will hold off comments on this part of the thread and wait for your first design and start working it out from there.

Basile B.

  • Guest
Re: Looking for guidance on an OO implementation issue
« Reply #10 on: November 19, 2014, 04:31:57 pm »
Just a question, why do you want to wrap your things in classes ? Wouldn't be more efficient to create functions and dedicated types, using operator overloading ?
« Last Edit: November 19, 2014, 06:34:22 pm by Basile B. »

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Looking for guidance on an OO implementation issue
« Reply #11 on: November 19, 2014, 05:19:10 pm »
Just a question, why do you want to warp your things in classes ? Wouldn't be more efficient to create functions and dedicated types, using operator overloading ?

Honest answer - I didn't knew it was possible this way too. If it is and it is more efficient to do it this way than via a class based Approach, then I'll have to consider this too, as of course efficiency also has an impact with arbitrary precision calculations.

First I'll compose a documentation as Taazz suggested and then we can take it from there, ok?

Kind regards,
MathMan

Basile B.

  • Guest
Re: Looking for guidance on an OO implementation issue
« Reply #12 on: November 19, 2014, 06:37:35 pm »
Just a question, why do you want to warp your things in classes ? Wouldn't be more efficient to create functions and dedicated types, using operator overloading ?

Honest answer - I didn't knew it was possible this way too. If it is and it is more efficient to do it this way than via a class based Approach, then I'll have to consider this too, as of course efficiency also has an impact with arbitrary precision calculations.

First I'll compose a documentation as Taazz suggested and then we can take it from there, ok?

Kind regards,
MathMan

I meant wrap, not warp.  %)
Btw, it was just a thought, operator overloading is limited in FPC pascal. But this is clearly the way to go (IMO).

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Looking for guidance on an OO implementation issue
« Reply #13 on: November 27, 2014, 04:07:26 pm »
Well lets see how many primitives do you have in total? Write them down. Do they have anything in common with each other? what is that? What is different between them eg what makes the natural different from a real? How about integer, reals and rationals? Do make your results public here so we comment and refine the design together.

Thanks for your offer! It will take me some time though to write this down in a concise manner. I'll try to post a response with an attached document within the next week and maybe we can take it on from there? As I will propably not finish the arithmetic layer before end of this year we should have some time to think about a good approach ...

Kind regards,
MathMan

Sure take your time the idea is to get a first hand experience in designing from zero your self instead of relying in existing patterns that might or might not fit your requirements.

Hello Taazz et. all,

Please find attached a document I have put together to address (I hope) the questions Taazz brought up. Fair warning though

 * it is not the shortest of all documents (roughly 800 lines)
 * my nick is not a random choice meaning the doc could present a dry & terse reading

On the upside I have added a small introductory course in computer arithmetics - so if you allways wanted to dig into but never found the time this might be worthwhile.

Kind regards,
MathMan

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Looking for guidance on an OO implementation issue
« Reply #14 on: December 03, 2014, 10:20:46 am »
Hello all,

I am surprised that my doc has been downloaded 11 times even though I gave a fair warning  :) Seems there is some interest in either math or complex OO topics in this forum - great!

What I am missing though is some kind of feedback on its content  :( Can those who downloaded (and hopefully read) could pls. come up with a short comment? I am open to anything - "still working through" / "you completely missed the point - focus on ..." even a "that's a lecture doc and I don't read these" will do.

Thanks in advance,
MathMan

 

TinyPortal © 2005-2018