www.digitalmars.com         C & C++   DMDScript  

D - abs property for creal?

reply "John Reimer" <jjreimer telus.net> writes:
Another question for the group:

There doesn't appear to be an abs() method for types creal.  I know this is
a very simple function and done by hand easily enough such as:

import math;

/* no errors caught here */

real abs( creal c) {
    return sqrt( c.re*c.re + c.im*c.im);
}

I don't know if this is correct (casting to real necessary in sqrt
function?) since I'm very much a beginner with D.

Nevertheless, it would be nice to have maybe a property of creals that
renders the "abs" value just like the properties "re" and "im".  Is this
possible?

if there are properties:
    .re
    .im

then the property:
    .abs     is equivalent to the above function.

might also be useful. :)

Thanks,
John
Mar 07 2003
next sibling parent reply "John Reimer" <jjreimer telus.net> writes:
"John Reimer" <jjreimer telus.net> wrote in message
news:b4bhe6$2lfa$1 digitaldaemon.com...
 Another question for the group:

 There doesn't appear to be an abs() method for types creal.  I know this

 a very simple function and done by hand easily enough such as:

 import math;

 /* no errors caught here */

 real abs( creal c) {
     return sqrt( c.re*c.re + c.im*c.im);
 }

I just realized the above is not quite correct since c.im*c.im would render the -1. The proper calculation drops the imaginary i first. Please disregard that :-P. - John
Mar 07 2003
parent reply "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
So you're saying it should be

return sqrt( c.re*c.re - c.im*c.im );

hehe

This is true for any numeric array type:

float x[3];
return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]); // calculate magnitude of
vector

It's pythagoras' theorem for christ's sake... one of the most fundamental
constructs in all of math.  Nowadays it's usually called the vector norm or
the magnitude, but abs works as well.  Length would be another good word for
it.

I would like to have a standard library template function to compute it.

I am not sure it's a good idea for this kind of thing to be a property,
because then when you write a template that takes unknown types and gets its
abs, it's not possible to make every type have an abs property.  There is
confusion between member function call syntax and property get syntax.  It
*is* however possible to make a global overloaded function that takes one of
your types as an argument, so that appears to be the correct approach;  the
one with the least limitations.

Sean

"John Reimer" <jjreimer telus.net> wrote in message
news:b4bk8j$2mvh$1 digitaldaemon.com...
 "John Reimer" <jjreimer telus.net> wrote in message
 news:b4bhe6$2lfa$1 digitaldaemon.com...
 Another question for the group:

 There doesn't appear to be an abs() method for types creal.  I know this

 a very simple function and done by hand easily enough such as:

 import math;

 /* no errors caught here */

 real abs( creal c) {
     return sqrt( c.re*c.re + c.im*c.im);
 }

I just realized the above is not quite correct since c.im*c.im would

 the -1.  The proper calculation drops the imaginary i first.  Please
 disregard that :-P.

 - John

Mar 08 2003
parent reply "John Reimer" <jjreimer telus.net> writes:
"Sean L. Palmer" <seanpalmer directvinternet.com> wrote in message
news:b4dits$mue$1 digitaldaemon.com...
 So you're saying it should be

 return sqrt( c.re*c.re - c.im*c.im );

 hehe

Yes. That was what I was trying to say. :-)
 This is true for any numeric array type:

 float x[3];
 return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]); // calculate magnitude of
 vector

 It's pythagoras' theorem for christ's sake... one of the most fundamental
 constructs in all of math.  Nowadays it's usually called the vector norm

 the magnitude, but abs works as well.  Length would be another good word

 it.

Hmm.. I have no idea what Christ has to do with it... but, yes, it is a basic distance equation also called the pythagorean theorem. I'm sorry you felt you had to point that basic concept out. However, my only concern here was dealing with the complex aspect of it, and whether or not there was the basic abs function available. I was not really concerned as to whether people could identify it's origin. Perhaps I misjudged your manner here. I am new to the list and perhaps should not have presumed to ask these questions until I was better acquainted with the people here or at least they were better acquainted with me.
 I would like to have a standard library template function to compute it.

 I am not sure it's a good idea for this kind of thing to be a property,
 because then when you write a template that takes unknown types and gets

 abs, it's not possible to make every type have an abs property.  There is
 confusion between member function call syntax and property get syntax.  It
 *is* however possible to make a global overloaded function that takes one

 your types as an argument, so that appears to be the correct approach;

 one with the least limitations.

 Sean

Good point. I'm not extrememely knowledgeable as to language design practice; right now it's more "get the job done." But you seem to know what you're talking about. Thanks for your time, John
Mar 08 2003
parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
 This is true for any numeric array type:

 float x[3];
 return sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]); // calculate magnitude


 vector

 It's pythagoras' theorem for christ's sake... one of the most


 constructs in all of math.  Nowadays it's usually called the vector norm

 the magnitude, but abs works as well.  Length would be another good word

 it.

Hmm.. I have no idea what Christ has to do with it... but, yes, it is a basic distance equation also called the pythagorean theorem. I'm sorry

 felt you had to point that basic concept out.  However, my only concern

 was dealing with the complex aspect of it, and whether or not there was

 basic abs function available.  I was not really concerned as to whether
 people could identify it's origin.  Perhaps I misjudged your manner here.

 am new to the list and perhaps should not have presumed to ask these
 questions until I was better acquainted with the people here or at least
 they were better acquainted with me.

I think the point was that the usual (an accepted) name of the functions you call abs is magnitude, modulus or norm depending on the item and if your a mathmation or engineer (j not i) is there a mathmatical 'abs' for a complex number, quaternion, vector, matrix ? I though it was just reals that had an abs; a vector abs I would expect to abs the elements not return magnitude. does D have a conjugate property for complex ? magnitude ::= a * a.conjugate; // (a+ib)*(a-ib) = a*a + b*b;
Mar 08 2003
parent reply "John Reimer" <jjreimer telus.net> writes:
 I think the point was that the usual (an accepted) name of the functions

 call abs
 is magnitude, modulus or norm depending on the item and if your a

 or engineer (j not i)
 is there a mathmatical 'abs' for a complex number, quaternion, vector,
 matrix ?
 I though it was just reals that had an abs;  a vector abs I would expect

 abs the elements not return magnitude.

 does D have a conjugate property for complex ?

 magnitude ::= a * a.conjugate; // (a+ib)*(a-ib) = a*a + b*b;

True, the engineering notation for 'i' is actually 'j'. But it really makes no difference other than convention.. I'm not an expert on these matters, but I do use the features of complex numbers in my electronics studies. The "abs" moniker was not my choice. It was just the convention I noticed that is used for getting the magnitude of a complex number (Python uses it too). As far as I see, abs is fairly standard use for deriving this value when it comes to complex numbers. So my point is that "abs" IS the usual and accepted name for this funciton no matter what it's meaning or name is in other number sets or systems. This is actually the standard mathematical definition of the "absolute value of the complex number": "The modulus or absolute value of a complex number a + bi is | a + bi | = sqrt( a*a + b*b) from A Second Course in Calculus, ISBN 0-12-259662-5 Now this may be a fairly basic "length" expression as Sean mentioned, but it seems that people got confused over the use of the term "absolute value" in complex speech. Sean's tone appeared slightly belittling, thus my response. I really hate to beat this topic to death, but I just wanted to verify that abs is actually very much the correct mathematical term for this value. As for use of "abs" for quaternion's, 3D vectors or whatnot, I'm not sure if they share the idea of "abs" for obtaining the magnitude. I'm sure there's a few such experts in this group anyway so I needn't speculate. A conjugate property would be another useful addition; I agree. But I don't understand your use of the term magnitude here. Later, John
Mar 08 2003
next sibling parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
 magnitude ::= a * a.conjugate; // (a+ib)*(a-ib) = a*a + b*b;


 A conjugate property would be another useful addition; I agree.  But I

 understand your use of the term magnitude here.

that's me not thinking forgot the root. got confused should have been if n = (a+ib) |n| = sqrt(a^2 +b^2) => |n| = sqrt( n * conjugate(n) ) if vector v = {a,b} ||v|| = sqrt(a^2 +b^2) => ||v|| = sqrt( v dot v ) I was reading something on 3d vectors that used |v| to mean v.v and ||v|| to mean sqrt(v.v) last week. anyway what about polar form of complex ? n = (a +ib) = r * (e to_the_power (i*arg)) = |n| (e to_the_power (i*arc_tan(y/x))) isn't that as important a part of complex maths as the a+ib form so creal should have a .magnitude and .arg property anyway.
Mar 08 2003
parent "John Reimer" <jjreimer telus.net> writes:
"Mike Wynn" <mike.wynn l8night.co.uk> wrote in message
news:b4ecf8$12f7$1 digitaldaemon.com...
 magnitude ::= a * a.conjugate; // (a+ib)*(a-ib) = a*a + b*b;


 A conjugate property would be another useful addition; I agree.  But I

 understand your use of the term magnitude here.

that's me not thinking forgot the root. got confused should have been

Ah, I see now.
 if n = (a+ib)
 |n| = sqrt(a^2 +b^2) => |n| = sqrt( n * conjugate(n) )

 if vector v = {a,b}
 ||v|| = sqrt(a^2 +b^2) => ||v|| = sqrt( v dot v )

 I was reading something on 3d vectors that used
 |v| to mean v.v and ||v|| to mean sqrt(v.v)
 last week.

I haven't investigate 3D vectors to deeply, but natrually 2D vector math and complex phasor math are understandably similar. They're just mapped to different "spaces.", and have there own associated rules. To be honest, I'm not familiar with the double bar notation with vectors. Interesting.
 anyway what about polar form of complex ?
 n = (a +ib) = r * (e to_the_power (i*arg)) = |n| (e to_the_power
 (i*arc_tan(y/x)))
 isn't that as important  a part of complex maths as the a+ib form
 so creal should have a .magnitude and .arg property anyway.

Yes, you bet; polar form is just as important. Polar form a la Euler , 'abs' and 'arg' properties, should be available to make the complex type more complete. Later, John
Mar 08 2003
prev sibling parent reply "Sean L. Palmer" <seanpalmer directvinternet.com> writes:
Sorry I came off as "belittling".  Not my intention.  I was mainly pointing
out the lack of such basic vector algebra functions in D, and the limiting
focus on complex numbers to the exclusion of the more general case of
arbitrary dimension vectors.

I've seen the term "abs" used before to describe vector norm, and in fact it
makes perfect sense (mapping functions of reals over higher-order vectors);
just in my line of work most people use the word "norm" or "magnitude".

I either want

A) quaternions and 3d vectors included as basic types in D

or

B) complex to be removed to the standard library and applications
programmers given the ability to do everything that complex does now.  This
is very close to being implemented already;  the one thing lacking is that
we cannot make new types such as "imaginary".  Also there's no way we can do
so with the same performance characteristics (especially in builds without
inlining) that builtin types have.

I think I've beat this horse to death already.  My words seem to be falling
on deaf ears.

Sean

"John Reimer" <jjreimer telus.net> wrote in message
news:b4e743$vuj$1 digitaldaemon.com...
 I think the point was that the usual (an accepted) name of the functions

 call abs
 is magnitude, modulus or norm depending on the item and if your a

 or engineer (j not i)
 is there a mathmatical 'abs' for a complex number, quaternion, vector,
 matrix ?
 I though it was just reals that had an abs;  a vector abs I would expect

 abs the elements not return magnitude.

 does D have a conjugate property for complex ?

 magnitude ::= a * a.conjugate; // (a+ib)*(a-ib) = a*a + b*b;

True, the engineering notation for 'i' is actually 'j'. But it really

 no difference other than convention..  I'm not an expert on these matters,
 but I do use the features of complex numbers in my electronics studies.

 "abs" moniker was not my choice.  It was just the convention I noticed

 is used for getting the magnitude of a complex number (Python uses it

 As far as I see, abs is fairly standard use for deriving this value when

 comes to complex numbers.  So my point is that "abs" IS the usual and
 accepted name for this funciton no matter what it's meaning or name is in
 other number sets or systems.

 This is actually the standard mathematical definition of the  "absolute
 value of the complex number":

     "The modulus or absolute value of a complex number a + bi is

                             | a + bi | = sqrt( a*a + b*b)

 from A Second Course in Calculus, ISBN 0-12-259662-5

 Now this may be a fairly basic "length" expression as Sean mentioned, but

 seems that people got confused over the use of  the term "absolute value"

 complex speech.  Sean's tone appeared slightly belittling, thus my

 I really hate to beat this topic to death, but I just wanted to verify

 abs is actually very much the correct mathematical term for this value.

 As for use of "abs" for quaternion's, 3D vectors or whatnot, I'm not sure

 they share the idea of "abs" for obtaining the magnitude.  I'm sure

 a few such experts in this group anyway so I needn't speculate.

 A conjugate property would be another useful addition; I agree.  But I

 understand your use of the term magnitude here.

 Later,

 John

Mar 09 2003
parent Bill Cox <bill viasic.com> writes:
Sean L. Palmer wrote:
 Sorry I came off as "belittling".  Not my intention.  I was mainly pointing
 out the lack of such basic vector algebra functions in D, and the limiting
 focus on complex numbers to the exclusion of the more general case of
 arbitrary dimension vectors.
 
 I've seen the term "abs" used before to describe vector norm, and in fact it
 makes perfect sense (mapping functions of reals over higher-order vectors);
 just in my line of work most people use the word "norm" or "magnitude".
 
 I either want
 
 A) quaternions and 3d vectors included as basic types in D
 
 or
 
 B) complex to be removed to the standard library and applications
 programmers given the ability to do everything that complex does now.  This
 is very close to being implemented already;  the one thing lacking is that
 we cannot make new types such as "imaginary".  Also there's no way we can do
 so with the same performance characteristics (especially in builds without
 inlining) that builtin types have.
 
 I think I've beat this horse to death already.  My words seem to be falling
 on deaf ears.
 
 Sean

If it helps, I think you've got a reasonable point of view. I'm a bit more of a minimalist than Walter. I would have kept out many of the features he's incorporated, including support for complex numbers, even though I sometimes use them in programming. I fault C++ for wasting years of the industry's time while compiler writers worked on implementing the standard. I don't yet have the D compiler on my Linux system (I'm waiting for the GNU tie-in), but from the spec, it seems able to support both complex numbers and vectors in standard libraries. However, there will always be a speed penalty for add-ons like this. Also, you wont get any cool custom syntax for it. What can you do? No language can be all things to all people. Bill
Mar 09 2003
prev sibling next sibling parent reply "John Reimer" <jjreimer telus.net> writes:
I'm having another problem.  I cannot figure out how to construct a complex
expression legally...
The last I tried caused the compiler to crash:

Internal error: ..\ztc\cg87.c 1219

.. Basic complex expressions are easy like:

creal a = 1.0 + 2.0i;

But I cannot create an expression like this (which are important in circuit
calculations):

creal Z1;

Z1 = (1/R1) + (2.0i*PI*F*(C1+C2));

where R1, PI, F, C1 and C2 are all "real."

As you can see I tried putting the imaginary 'i' next to one of the values
in an attempt to force that side of the expression to be imaginary.  Didn't
work...  I also tried putting the i at the end of the expression... Didn't
work either (compiler complains in this case of undefined i).  The above
expression seems to crash the compiler. Casting the imaginary portion of the
above expression to 'ireal' doesn't seem to work either.

So how do I make a proper complex expression with more complicated
subexpressions.

Python does it this way...

Z1 = complex(1/R1, (2.0*pi*F*(C1+C2))

which works just fine...

The short program listing is available if anyone is interested to see why
the compiler crashed...

Thanks,

John
Mar 07 2003
parent reply "Walter" <walter digitalmars.com> writes:
"John Reimer" <jjreimer telus.net> wrote in message
news:b4bnbb$2oe8$1 digitaldaemon.com...
 The short program listing is available if anyone is interested to see why
 the compiler crashed...

I'm interested. Please post or email it to me. Thanks, -Walter
Apr 14 2003
parent reply John Reimer <jjreimer telus.net> writes:
Walter wrote:
 "John Reimer" <jjreimer telus.net> wrote in message
 news:b4bnbb$2oe8$1 digitaldaemon.com...
 
The short program listing is available if anyone is interested to see why
the compiler crashed...

I'm interested. Please post or email it to me. Thanks, -Walter

Hi Walter, Um, I should say was available... A major computer malfunction several weeks ago has left me without the source, though I should be able to cook up the problem again for you. I'll see if I can get some source up in the next few days... Thanks, John
Apr 17 2003
parent reply John Reimer <jjreimer telus.net> writes:
John Reimer wrote:

 Walter wrote:
 
 "John Reimer" <jjreimer telus.net> wrote in message
 news:b4bnbb$2oe8$1 digitaldaemon.com...

 The short program listing is available if anyone is interested to see 
 why
 the compiler crashed...

I'm interested. Please post or email it to me. Thanks, -Walter

Hi Walter, Um, I should say was available... A major computer malfunction several weeks ago has left me without the source, though I should be able to cook up the problem again for you. I'll see if I can get some source up in the next few days... Thanks, John

Ok, Rebuilt and sent. I used D compiler 0.61. Hope that helps. John
Apr 17 2003
parent "Walter" <walter digitalmars.com> writes:
Got it. Thanks!
Apr 18 2003
prev sibling next sibling parent reply "Jon Allen" <jallen minotstateu.edu> writes:
I'm not sure how the creal is represented in memory, but assuming it's just
two 80 bit floats (IEEE 754, or whatever the standard is) right next to
eachother, and assuming the comiler won't cry foul if you try to treat a
pointer as an array (it works in C, i'm not sure about D) you could save a
LOT of processor time doing something like this:

creal abs(creal c)
{
    ubyte* cbytes=cast(byte*)&c;

    //this gives the absolute value of the real part (i'm guessing)
    cbytes[9]&=0x7F;

    //this gives the absolute value of the imaginary part (i'm guessing)
    cbytes[19]&=0x7F;

    return c;
}

Of course this isn't portable at all and there are probably assembly
instructions that would do it for you, but I don't know them.  You could
also just do a test, if it's less than zero then multiply by -1, that would
still be portable and leagues faster just because square roots are even
slower than multiplication.

"John Reimer" <jjreimer telus.net> wrote in message
news:b4bhe6$2lfa$1 digitaldaemon.com...
 Another question for the group:

 There doesn't appear to be an abs() method for types creal.  I know this

 a very simple function and done by hand easily enough such as:

 import math;

 /* no errors caught here */

 real abs( creal c) {
     return sqrt( c.re*c.re + c.im*c.im);
 }

 I don't know if this is correct (casting to real necessary in sqrt
 function?) since I'm very much a beginner with D.

 Nevertheless, it would be nice to have maybe a property of creals that
 renders the "abs" value just like the properties "re" and "im".  Is this
 possible?

 if there are properties:
     .re
     .im

 then the property:
     .abs     is equivalent to the above function.

 might also be useful. :)

 Thanks,
 John

Mar 08 2003
parent reply "John Reimer" <jjreimer telus.net> writes:
"Jon Allen" <jallen minotstateu.edu> wrote in message
news:b4cbfn$r9$1 digitaldaemon.com...
 I'm not sure how the creal is represented in memory, but assuming it's

 two 80 bit floats (IEEE 754, or whatever the standard is) right next to
 eachother, and assuming the comiler won't cry foul if you try to treat a
 pointer as an array (it works in C, i'm not sure about D) you could save a
 LOT of processor time doing something like this:

 creal abs(creal c)
 {
     ubyte* cbytes=cast(byte*)&c;

     //this gives the absolute value of the real part (i'm guessing)
     cbytes[9]&=0x7F;

     //this gives the absolute value of the imaginary part (i'm guessing)
     cbytes[19]&=0x7F;

     return c;
 }

 Of course this isn't portable at all and there are probably assembly
 instructions that would do it for you, but I don't know them.  You could
 also just do a test, if it's less than zero then multiply by -1, that

 still be portable and leagues faster just because square roots are even
 slower than multiplication.

Ok, thanks for the tip, Jon. I only wish it were that simple. I assumed that since the language provided complex numbers that everything would be ready to go as far as manipulating them was concerned. I guess not quite yet or maybe I just don't know how it's supposed to work. But I think your solution is not quite what I was getting at. The "abs" as in absolute value is not the typical mathematical absolute value when complex numbers are concerned. Perhaps you were thinking that I wanted the non-negative values of both real and imaginary parts? . Not so I'm afraid... I'm looking for the resultant magnitude of a complex pair (also referred as the absolute value in complex math for some weird reason) which is the pythagorean sum of the real and imaginary parts, or the squareroot of the sum of the squares of the magnitudes of the real and imaginary parts (phew, what a mouthful). Complex numbers are used in electronics AC analysis and are often called phasors. They are also represented in polar form in which case the complex parts are converted to a magnitude (abs) and a relative phase angle. It becomes necessary to convert between formats now and again. As far as the lowlevel optimization goes, that's not really my concern yet... I just want it to work :-P. The implementor of the language's complex numbers should do those optimizations for me, I think :-). I guess I still have to figure out how to do all this in D. Python does it seamlessly and provides all the necessary methods out of the box. D must not be there yet... :-) Oh well, it's a neat language, still. Thanks for the suggestion though; that's some pretty slick D hacking. :-) Later, John.
Mar 08 2003
parent reply "Jon Allen" <jallen minotstateu.edu> writes:
Eh, don't I feel stupid, seems like something a young math major should know
doesn't it?  That's what I get for trying to be too clever I guess.  :-)

"John Reimer" <jjreimer telus.net> wrote in message
news:b4ch6i$5o6$1 digitaldaemon.com...
 Ok, thanks for the tip, Jon. I only wish it were that simple.  I assumed
 that since the language provided complex numbers that everything would be
 ready to go as far as manipulating them was concerned.  I guess not quite
 yet or maybe I just don't know how it's supposed to work.

 But I think your solution is not quite what I was getting at.  The "abs"

 in absolute value is not the typical mathematical absolute value when
 complex numbers are concerned.  Perhaps you were thinking that I wanted

 non-negative values of both real and imaginary parts? .

 Not so I'm afraid... I'm looking for the resultant magnitude of a complex
 pair (also referred as the absolute value in complex math for some weird
 reason) which is the pythagorean sum of the real and imaginary parts, or

 squareroot of the sum of the squares of the magnitudes of the real and
 imaginary parts (phew, what a mouthful).  Complex numbers are used in
 electronics AC analysis and are often called phasors.  They are also
 represented in polar form in which case the complex parts are converted to

 magnitude (abs) and a relative phase angle.  It becomes necessary to

 between formats now and again.

 As far as the lowlevel optimization goes, that's not really my concern
 yet... I just want it to work :-P.  The implementor of the language's
 complex numbers should do those optimizations for me, I think :-).

 I guess I still have to figure out how to do all this in D.  Python does

 seamlessly and provides all the necessary methods out of the box.  D must
 not be there yet... :-) Oh well, it's a neat language, still.

 Thanks for the suggestion though; that's some pretty slick D hacking. :-)

 Later,

 John.

Mar 09 2003
parent "John Reimer" <jjreimer telus.net> writes:
"Jon Allen" <jallen minotstateu.edu> wrote in message
news:b4f0j5$1d4u$1 digitaldaemon.com...
 Eh, don't I feel stupid, seems like something a young math major should

 doesn't it?  That's what I get for trying to be too clever I guess.  :-)

:-D No problem. That just happened to be one of the very few areas that I knew something of what I was talking about :-). Later, John
Mar 09 2003
prev sibling parent reply Burton Radons <loth users.sourceforge.net> writes:
John Reimer wrote:
 Another question for the group:
 
 There doesn't appear to be an abs() method for types creal.  I know this is
 a very simple function and done by hand easily enough such as:
 
 import math;
 
 /* no errors caught here */
 
 real abs( creal c) {
     return sqrt( c.re*c.re + c.im*c.im);
 }
 
 I don't know if this is correct (casting to real necessary in sqrt
 function?) since I'm very much a beginner with D.

It's syntactually correct, but better is: real abs(creal c) { return math2.hypot(c.re, c.im); } As sqrt uses double, and hypot can do some overflow/underflow minimisation.
 Nevertheless, it would be nice to have maybe a property of creals that
 renders the "abs" value just like the properties "re" and "im".  Is this
 possible?

Yes, and it should be in. Feedback on what users of complex need is valuable, as there aren't many serious users of it here. Don't expect anything in the near future, however.
Mar 08 2003
next sibling parent reply "John Reimer" <jjreimer telus.net> writes:
 It's syntactually correct, but better is:

     real abs(creal c) {
        return math2.hypot(c.re, c.im);
     }

 As sqrt uses double, and hypot can do some overflow/underflow

That does look good :-). But I've never seen a math2 module before. Is it now part of phobos?
 Nevertheless, it would be nice to have maybe a property of creals that
 renders the "abs" value just like the properties "re" and "im".  Is this
 possible?

Yes, and it should be in. Feedback on what users of complex need is valuable, as there aren't many serious users of it here. Don't expect anything in the near future, however.

Thanks, I understand what you're saying. Just checking to see if there were some answers that I didn't know about yet. Thanks for your response, Burton. Much appreciated. Later, John
Mar 08 2003
parent reply Burton Radons <loth users.sourceforge.net> writes:
John Reimer wrote:
It's syntactually correct, but better is:

    real abs(creal c) {
       return math2.hypot(c.re, c.im);
    }

As sqrt uses double, and hypot can do some overflow/underflow

minimisation. That does look good :-). But I've never seen a math2 module before. Is it now part of phobos?

Yeah, it's a large set of routines that Pavel Minayev wrote. It should probably be merged with math. For your later concern, this is how Python implements complex abs.
Mar 08 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Burton Radons" <loth users.sourceforge.net> wrote in message
news:b4eavm$11s2$1 digitaldaemon.com...
 John Reimer wrote:
It's syntactually correct, but better is:

    real abs(creal c) {
       return math2.hypot(c.re, c.im);
    }

As sqrt uses double, and hypot can do some overflow/underflow

minimisation. That does look good :-). But I've never seen a math2 module before. Is


 now part of phobos?

Yeah, it's a large set of routines that Pavel Minayev wrote. It should probably be merged with math. For your later concern, this is how Python implements complex abs.

It's now in math.hypot. Also, math.hypot handles all the overflow/underflow cases correctly.
Apr 14 2003
parent reply John Reimer <jjreimer telus.net> writes:
Walter wrote:

 "Burton Radons" <loth users.sourceforge.net> wrote in message
 news:b4eavm$11s2$1 digitaldaemon.com...
 
John Reimer wrote:

It's syntactually correct, but better is:

   real abs(creal c) {
      return math2.hypot(c.re, c.im);
   }

As sqrt uses double, and hypot can do some overflow/underflow

minimisation. That does look good :-). But I've never seen a math2 module before. Is


it
now part of phobos?

Yeah, it's a large set of routines that Pavel Minayev wrote. It should probably be merged with math. For your later concern, this is how Python implements complex abs.

It's now in math.hypot. Also, math.hypot handles all the overflow/underflow cases correctly.

Great! I almost missed these posts. I didn't realize you would respond to stuff so far back ;-). Thanks, John
Apr 17 2003
parent "Walter" <walter digitalmars.com> writes:
"John Reimer" <jjreimer telus.net> wrote in message
news:b7n8ij$19lt$2 digitaldaemon.com...
 I almost missed these posts.  I didn't realize you would respond to
 stuff so far back ;-).

I'm waay behind <g>.
Apr 18 2003
prev sibling parent reply Olaf Rogalsky <olaf.rogalsky theorie1.physik.uni-erlangen.de> writes:
Burton Radons wrote:
 Yes, and it should be in.  Feedback on what users of complex need is
 valuable, as there aren't many serious users of it here.  Don't expect

Hello Burton, First of all, a little rant. When thinking about complex numbers, please don't confuse them with vectors. You can't multply vectors*, but you CAN multiply complex numbers. As a physisist I am permanently using complex numbers. There is nothing complex or imaginary about them. They are just numbers, but numbers where you don't need to care when taking roots. Here is what I would like to see: 1) I don't see much use for a purely imaginary type, drop it! 2) I don't see much use for integer complex types, don't include it! 3) Properties: z := re + im*I, and x a real number z.abs() sqrt(re*re+im*im) z.abs2() re*re+im*im z.arg() atan2(re, im) z.phase() z/z.abs() z.re() re z.im() im*I z.rim() im z.conj() re-im*I z.I() z*I = -im + re*I I don't care about the given names, as long as they are short :-). 4) Functions on complex: Basic builtin: +, -, *, /, sqr := z*z, inv := 1/z, scalar(z1, z2) := z1.conj()*z2 Library: exp, cos, sin, tan, cosh, sinh, tanh ln, acos, asin, atan, acosh, asinh, atanh, pow(z1, z2), log(z1, z2), sqrt 5) Constructors: z = creal(re, im); z = creal.polar(modulus, argument); The distinction between properties and functions is somewhat arbitrary. In my eyes, a property shall return some known information about the variable, but what is known depends on the implementation of that variable (e.g., wether the compiler stores complex numbers in cartesian or polar format). One can argue with good reasons to move some of the above properties to the functions section. I have no objections against doing so. Greetings, Olaf *) At least, if you don't think of them as elements of a Clifford algebra. -- +----------------------------------------------------------------------+ I Dr. Olaf Rogalsky Institut f. Theo. Physik I I I Tel.: 09131 8528440 Univ. Erlangen-Nuernberg I I Fax.: 09131 8528444 Staudtstrasse 7 B3 I I rogalsky theorie1.physik.uni-erlangen.de D-91058 Erlangen I +----------------------------------------------------------------------+
Mar 10 2003
parent reply "John Reimer" <jjreimer telus.net> writes:
 First of all, a little rant. When thinking about complex numbers, please
 don't confuse them with vectors. You can't multply vectors*, but you CAN
 multiply complex numbers. As a physisist I am permanently using complex
 numbers.  There is nothing complex or imaginary about them. They are just
 numbers, but numbers where you don't need to care when taking roots.

Did someone confuse vectors with complex numbers? I didn't notice... In another response, I think we were just talking about the similarities.
 Here is what I would like to see:
 1) I don't see much use for a purely imaginary type, drop it!
 2) I don't see much use for integer complex types, don't include it!
 3) Properties: z := re + im*I, and x a real number
      z.abs()      sqrt(re*re+im*im)
      z.abs2()     re*re+im*im
      z.arg()      atan2(re, im)
      z.phase()    z/z.abs()
      z.re()       re
      z.im()       im*I
      z.rim()      im
      z.conj()     re-im*I
      z.I()        z*I = -im + re*I
    I don't care about the given names, as long as they are short :-).
 4) Functions on complex:
    Basic builtin:
      +, -, *, /,
      sqr := z*z, inv := 1/z, scalar(z1, z2) := z1.conj()*z2
    Library:
      exp, cos,  sin,  tan,  cosh,  sinh,  tanh
      ln, acos, asin, atan, acosh, asinh, atanh,
      pow(z1, z2), log(z1, z2), sqrt
 5) Constructors:
    z = creal(re, im);
    z = creal.polar(modulus, argument);

 The distinction between properties and functions is somewhat arbitrary.
 In my eyes, a property shall return some known information about the
 variable, but what is known depends on the implementation of that
 variable (e.g., wether the compiler stores complex numbers in cartesian
 or polar format). One can argue with good reasons to move some of the
 above properties to the functions section. I have no objections against
 doing so.

Well, that sums up the needs quite nicely :-). Nice to have an expert give an opinion. Thanks. Later, John PS Apologies to Olaf. I sent email direct to him by mistake -- hit the wrong button, darn:-P
Mar 10 2003
parent reply Olaf Rogalsky <olaf.rogalsky theorie1.physik.uni-erlangen.de> writes:
John Reimer wrote:
 Did someone confuse vectors with complex numbers?  I didn't notice... In
 another response, I think we were just talking about the similarities.

thinks of them as a clever way to make 2D vector manipulations. Nothing personal though, Sean. I just don't agree, that complex numbers should only be included into the core language iff vectors are included as well. One reason, that many "numeric" people stuck with Fortran instead of C was the lack of complex numbers in C. (The other reason of course is the HUGE amount of quality numeric libraries for Fortran). Olaf -- +----------------------------------------------------------------------+ I Dr. Olaf Rogalsky Institut f. Theo. Physik I I I Tel.: 09131 8528440 Univ. Erlangen-Nuernberg I I Fax.: 09131 8528444 Staudtstrasse 7 B3 I I rogalsky theorie1.physik.uni-erlangen.de D-91058 Erlangen I +----------------------------------------------------------------------+
Mar 10 2003
parent "John Reimer" <jjreimer telus.net> writes:
"Olaf Rogalsky" <olaf.rogalsky theorie1.physik.uni-erlangen.de> wrote in
message news:3E6CD6EC.B3845F79 theorie1.physik.uni-erlangen.de...
 John Reimer wrote:
 Did someone confuse vectors with complex numbers?  I didn't notice... In
 another response, I think we were just talking about the similarities.

thinks of them as a clever way to make 2D vector manipulations. Nothing personal though, Sean. I just don't agree, that complex numbers should only be included into the core language iff vectors are included as well. One reason, that many "numeric" people stuck with Fortran instead of C was the lack of complex numbers in C. (The other reason of course is the HUGE amount of quality numeric libraries for Fortran).

Oh yes, now I see. True, true. I was so caught up in arguing my points in previous posts that I didn't really think of how the vector topics were being introduced. The distinction is significant in this regard. That is an interesting note about Fortran and very true. Complex math has been around in the computer field for quite awhile and has many uses, so it's inclusion as a D type seems to be the first logical addition. Quaternions and 3D vectors are much more specialized in there application in comparison, are they not? So I can see why they are not being so quickly adopted as a intrinsic type here. But with the huge explosion in computer graphics technology and development, I can see why people are looking to see their (vectors) use as more of a staple in computer languages. Maybe someday someone will decide that that vectors are now deemed worthy of inclusion, just as there was a point in the past where complex numbers proved critical for inclusion in some languages. I don't know if we've reached that point yet, though, with D. I really see no problem with keeping complex in and vectors out. That does not compromise consistancy, since as you say complex numbers are just numbers with a few interesting properties. They fit the spot quite well. Later, John
Mar 10 2003