The biggest role would be

`ComplexRole`

, capturing the idea of numbers with real and imaginary parts (either or both of which might be zero), and implementing a slew of standard mathematical functions that work on complex numbers. Then there would be a

`subset RealRole of ComplexRole where { $^z.im == 0.0 }`

. The most notable added method would be `Num`

, but there would probably also be a lot of RealRole-specific mathematical functions, as they can be a lot simpler than the complex equivalents.I don't know how to express is as a

`where`

, but the RationalRole subset of RealRole would mostly just add numerator and denominator methods. And finally we would have `subset IntegerRole of RationalRole where { $^q.denominator == 1 }`

.The idea would be that the classes would be built up in the opposite order than this (or not related at all), using the logical computer programming approach, with these roles applied on top to give a mathematical structure. So you'd define a rational class as two integers, then define numerator and denominator methods for the RationalRole, a Num method for the RealRole, and an imaginary method for the ComplexRole.

I'm not sure how practical this is to actually implement in Perl 6, but it seems like it would be a nice way to capture these notions. I'm presuming most of the mathematical functions would be implemented on ComplexRole and RealRole, so this would provide a simple way of plugging any new math types you defined into a full library of math functions.

Update: I've started work on implementing B-spline knot vectors and basis functions in Perl 6, but it will be a few more days before I have something worth posting.

## No comments:

## Post a Comment