Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Define Your Own Data Types

MuPAD® provides many predefined domains for a wide variety of symbolic and numeric computations. The system is also extensible. If the predefined MuPAD domains do not satisfy your needs, you can create your own domains and define operations on their elements. Also, MuPAD allows you to change the existing library domains, although this approach is not recommended. To create a new domain, use the newDomain function. For example, create the domain TempF that represents the Fahrenheit temperature scale:

TempF := newDomain("TempF"):

Currently the new domain does not have any elements. To create an element of a domain, use the new function. For example, create the new element x of the domain TempF:

x := new(TempF, 15)

To verify that the object x belongs to the domain TempF, use the testtype function. You can also use the domtype or type function:

testtype(x, TempF), domtype(x), type(x)

To check the internal representation of x, use the extop function:

extop(x)

Suppose you want the new method of the domain TempF to check whether an input argument is a valid temperature measurement. In this case, redefine the new method for your domain TempF:

TempF::new := t -> if testtype(t, Type::Real) then
           if t >= -459.67 then
             new(dom, t);
           else
             error("Temperature is below absolute zero.");
           end_if;
         else
           error("Expecting a real number above absolute zero.");
         end_if:

The redefined method checks that an input argument is a number. It also checks that the provided temperature is not below absolute zero. Absolute zero is zero degrees on the Kelvin temperature scale, which is equivalent to -459.67 degrees on the Fahrenheit scale. The redefined method creates new elements in degrees Fahrenheit. Call the method TempF::new directly or use the shorter syntax TempF(newElement):

TempF::new(0), TempF(32)

The TempF::new method also ensures that all new elements represent valid temperatures:

TempF(-500)
Error: Temperature is below absolute zero. [TempF::new]

The TempF::new method requires new elements to be real numbers:

TempF(x + 2)
Error: Expecting a real number above absolute zero. [TempF::new]
TempF(1 + 2*I)
Error: Expecting a real number above absolute zero. [TempF::new]

As a next step, improve the output format for the elements of your new domain. To change the format that MuPAD uses for displaying the elements of a domain, redefine the print method for that domain. For example, when displaying elements of the domain TempF::new, show temperature measurements followed by the units of measurements (degrees Fahrenheit):

TempF::print := proc(TempF)
begin
  expr2text(extop(TempF, 1)).Symbol::deg."F"
end_proc:

Now, MuPAD displays the elements of the domain TempF::new as follows:

TempF(32), TempF(72), TempF(90), TempF(104.8)

Suppose you want to perform additions for the elements of the new domain. By default, arithmetical operations treat the elements of a new domain as identifiers:

TempF(75) + TempF(10)

You can implement arithmetical operations for your domain. For example, define addition for the domain TempF. The result of addition must also belong to the domain TempF:

TempF::_plus := proc()
local elements;
begin
  elements:= map([args()], op, 1);
  new(TempF, _plus(op(elements)))
end_proc:

The system add the elements of your new domain the same way as it adds numbers. The system also displays the degrees Fahrenheit unit for the resulting sum:

TempF(75) + TempF(10)

For further computations, delete the identifier x:

delete x
Was this topic helpful?