WeBWorK Problems

add complex numbers to a context

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.
In reply to Alex Jordan

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.
In reply to Davide Cervone

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 ;)
In reply to Andrew Parker

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.
In reply to Davide Cervone

Re: add complex numbers to a context

by Alex Jordan -
I was afraid of this, after seeing all that goes in to making complex versions of other contexts.

In my situation, the form of an answer matters. Like "2 i sqrt(63)/6" is not good enough; it should be "i sqrt(7)". So I'm using bizarro arithmetic to get that done.

The point is, things like "i^2" should not appear in answers in this context, and it's not important to know that is equal to -1. So I wonder now if I can make "i" be a constant, equal to some thing that someone would never guess, like exp(e)/ln(pi). Then I could have answers like I mentioned.

There could be some tolerance issues, like "1000000000+i" could be within the tolerance of "1000000000". And there could be cosmetic issues if the set union operator likes to order its elements. So {1,1-i,1+i} would become {1-i,1,1+i}.

With your accumulated wisdom and experience, can you point out other bad consequences to faking "i" as a real constant this way?