## WeBWorK Problems

### add complex numbers to a context

by Alex Jordan -
Number of replies: 4
I have an elaborate context called "FiniteSolutionSets". In this context, I can make something like
Formula("1,2");

And the student can enter any of these things as the answer:
1,2
{1,2}
x = 1 or 2
x = 1 or x = 2
x = 1, 2

and other reasonable things. "x" can be whatever the context's first variable is.

The context setup starts out by copying the Interval context. That choice was made because the Set creator works well there, and that is part of how I get the behavior I want. Assuming I don't need to start over, is there an easy way to make it so that complex numbers can appear in the sets as well? So for example,
{1, i, -1, -i}
or
{1/2 + i sqrt(3)/2, 1/2 - i sqrt(3)/2}

could work just as well as I have it working with real numbers?

I've tried simple things like adding "i" as a constant defined by Complex(i), but that doesn't work.

Note I would be content if I just get "i" and basic arithmetic operations. I don't need the complications like redefining functions like sqrt() etc so that they can accept complex/negative input. I also don't need polar form, if that makes it easier.

### Re: add complex numbers to a context

by Davide Cervone -
Unfortunately, this is not going to be easy to do. The Set class implements a set of reals, and that dependence on reals is fairly fundamental in the underlying code. The Set object is part of the Interval context, and was designed to interoperate with intervals and unions and relies on the wondering of the real line in order to do interval operations like (1,3] - (1,3) = {3}, checking if a union is reduced, and so on. The reliance on reals is fairly deeply ingrained in the implementation of the Set object (as well as Interval and Union objects).

In hindsight, it would have been nice to have a more general set object and a set-of-reals subclass, but at the time the Set class was developed, interval/union/set notation was already a big advancement, and we didn't yet know what uses people were going to put MathObjects to in the long run.

It would be possible to develop a more general set object (that did not interoperate with intervals), and a more general union object as well. But it would be a completely new implementation, and would have to rely on different techniques from the real-based set and union that we currently have.

### Re: add complex numbers to a context

by Andrew Parker -
How about adjusting how { and } are handled in the existing Complex context?

Context->lists->set("{"=>{type=>"List"});

type=>"List" because type=>"Set" invokes the same Reals issue...

It would at least give the appearance of getting the job done ;)

### Re: add complex numbers to a context

by Alex Jordan -
Something like this might help if the rest of the context weren't so complicated. The particulars of everything else that happens with this context rely on the Set construction, and the built in methods available for Set. I'd need to rebuild things to work for List in the same way. It's worth considering.