Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Types in Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg62779] Re: Re: Types in Mathematica
  • From: "Steven T. Hatton" <hattons at globalsymmetry.com>
  • Date: Mon, 5 Dec 2005 03:37:38 -0500 (EST)
  • References: <200511191053.FAA16418@smc.vnet.net> <dlp2ci$le$1@smc.vnet.net> <200511200950.EAA04496@smc.vnet.net> <dls4vp$mmc$1@smc.vnet.net> <dm1ak3$i1n$1@smc.vnet.net> <dmjrb8$5u6$1@smc.vnet.net> <dmm2tp$nmo$1@smc.vnet.net> <200512031040.FAA06695@smc.vnet.net> <dmtbuu$fo6$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Kristen W Carlson wrote:

> 5. If I am extending built-in or user-defined functions (like Plus),
> should I restrict users to using a constructor to do that, or extend
> the existing function in the context of my data type with an upvalue?

I'm not sure what you mean here. Can you provide examples?

 
> But below is an example that admittedly is borderline; altho I wrote
> it as extending Part with an upvalue to extract the numerator or
> denominator of a rational number (created as a new datatype as an
> exercise from M220 the programming course), I can see the other point
> of view--not allowing Part to do this.
> ....
> The datatype is rational[n_Integer, d_Integer] with n and d
> type-checked as integers ....
> 
> makeRational[n_Integer, 0] :=
>   Print["This rational is undefined."] (* better than just getting
> input back as output *)
> 
> (* vs. this approach which Maeder wrote that does automatic type
> conversion;  for a negative denominator, it calls the function again
> and just reverses the signs of the numerator and denominator; that way
> negative rationals are always in the format rational[-n, d]  *)
> 
> makeRational[n_Integer, d_Integer?Negative] := makeRational[-n, d]
> ...
> denominator::"denominator will extract the denominator of the
> rational. You can also use Part[ rational ] , which has been modified
> to work correctly in the context of this package."
> ...
> rational /: (r_rational)[[1]] := numerator[r];  (* numerator is the
> constructor *)
> 
> rational /: (r_rational)[[2]] := denominator[r]; (* denominator is the
> constructor *)

Do you mean "selector" where you wrote "constructor" in the last two
expressions? 

Thanks for posting that example.  It was instructive.

-- 
The Mathematica Wiki: http://www.mathematica-users.org/
Math for Comp Sci http://www.ifi.unizh.ch/math/bmwcs/master.html
Math for the WWW: http://www.w3.org/Math/


  • Prev by Date: Re: Turning on numerical computation
  • Next by Date: Re: How would you evaluate this limit in Mathematica
  • Previous by thread: Re: Types in Mathematica
  • Next by thread: Re: Re: Types in Mathematica