Previous Page Next Page Contents

new -- create a domain element

Introduction

new(T, object1, object2, ...) creates a new element of the domain T with the internal representation object1, object2, ....

Call(s)

new(T, object1, object2, ...)

Parameters

T - a MuPAD domain
object1, object2, ... - arbitrary MuPAD objects

Returns

an element of the domain T.

Related Functions

DOM_DOMAIN, domain, extop, extnops, extsubsop, newDomain, op

Details

Example 1

We create a new domain Time for representing clock times. The internal representation of an object of this domain has two operands: the hour and the minutes. Then we create a new domain element for the time 12:45:

>> Time := newDomain("Time"):
   a := new(Time, 12, 45)
                             new(Time, 12, 45)

The domain type of a is Time, the number of operands is 2, and the operands are 12 and 45:

>> domtype(a), extnops(a)
                                  Time, 2
>> extop(a)
                                  12, 45

We now implement a "new" method for our new domain Time, permitting several input formats. It expects either two integers, the hour and the minutes, or only one integer that represents the minutes, or a rational number or a floating point number, implying that the integral part is the hour and the fractional part represents a fraction of an hour corresponding to the minutes, or no arguments, representing midnight. Additionally, the procedure checks that the arguments are of the correct type:

>> Time::new := proc(HR = 0, MN = 0)
     local m;
   begin
     if args(0) = 2 and domtype(HR) = DOM_INT
        and domtype(MN) = DOM_INT then
       m := HR*60 + MN
     elif args(0) = 1 and domtype(HR) = DOM_INT then
       m := HR
     elif args(0) = 1 and domtype(HR) = DOM_RAT then
       m := trunc(float(HR))*60 + frac(float(HR))*60
     elif args(0) = 1 and domtype(HR) = DOM_FLOAT then
       m := trunc(HR)*60 + frac(HR)*60
     elif args(0) = 0 then
       m := 0
     else
       error("wrong number or type of arguments")
     end_if;
     new(Time, trunc(m/60), trunc(m) mod 60)
   end_proc:

Now we can use this method to create new objects of the domain Time, either by calling Time::new directly, or, preferably, by using the equivalent but shorter call Time(...):

>> Time::new(12, 45), Time(12, 45), Time(12 + 3/4)
          new(Time, 12, 45), new(Time, 12, 45), new(Time, 12, 45)
>> Time(), Time(8.25), Time(1/2)
            new(Time, 0, 0), new(Time, 8, 15), new(Time, 0, 30)

In order to have a nicer output for objects of the domain Time, we also define a "print" method (see the help page for print):

>> Time::print := proc(TM)
   begin
     expr2text(extop(TM, 1)) . ":" .
     stringlib::format(expr2text(extop(TM, 2)), 2, Right, "0")
   end_proc:
>> Time::new(12, 45), Time(12, 45), Time(12 + 3/4)
                            12:45, 12:45, 12:45
>> Time(), Time(8.25), Time(1/2)
                             0:00, 8:15, 0:30

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000