Giter VIP home page Giter VIP logo

Comments (4)

dpvc avatar dpvc commented on July 26, 2024

Well, one way to look at it is that the complex square root is multiple valued, and both of these are square roots of -4 so both are valid. But that doesn't tell you why the computations produce different ones in the two cases. Here the reason:

In the Complex context, all computations are performed using complex numbers, even if they started out as reals, so (-2)^2 is really (-2+0i)^(-2+0i). The complex power z^w is computed using exp(w * log(z)), with log(z) as log(mod(z)) + arg(z)*i, and arg(a + bi) being atan2(b,a) (arctangent of b/a, where the signs of a and b are taken into account to get an angle between -pi and pi). That means the branch cut for log(z) is along the negative real axis, which is right where -4 lies, and so the computation is sensitive to slight changes in the complex part of the value.

Now getting back to (2+0i)*(-2+0i), this turns out to be a + bi where a = -4 and b = -9.79717439317883e-16 a number very close, but not quite equal to, zero. But most important, it is negative, so that means (-2)^2 - 8 is across the branch cut from -4, and arg((-2)^2) is -pi (or very close to it) while arg(-4) is pi. That means log((-2)^2 - 8) = log(4) - pi*i while log(-4) = log(4) + pi * i, which leads to sqrt(-4) = 2i while sqrt((-2)^2 - 8) = -2i.

Such issues are inherent in the fact the sqrt(), log(), arg() and so on are multiple-valued. One could perhaps improve things by using different branch cuts for arg() (say the positive x-axis when a + bi has a < 0, but this would still be problematic when everything is close to 0). It might be worth giving that a try.

from pg.

drgrice1 avatar drgrice1 commented on July 26, 2024

Even though (-2i)^2 = (2i)^2 = -4, the usual convention with a square root is that the square root of a real number returns the positive result. So if a student enters the answer sqrt((-2)^2-8) or sqrt(-4), it is rather unexpected to have the system tell them that first is incorrect and the second is not when 2i is the correct answer, and simplification is not expected.

from pg.

dpvc avatar dpvc commented on July 26, 2024

The problem is that the computed (-2)^2 isn't a real number (it is slightly off), so that doesn't happen. To fix that aspect of it, the Complex context would have to be made more sophisticated (in having it not promote all values to complex numbers before doing the computations). That is possible, but would take considerable reworking of the internals of the Complex context.

I was only trying to indicate why the result is what it is, not that it is ideal.

Also, my suggestion about changing arg() for negative real parts is useless, as that just changes the branch cut to along the imaginary axis. D'oh!

from pg.

dpvc avatar dpvc commented on July 26, 2024

I suppose one possible approach would be to convert -9.79717439317883e-16 to actually being 0 (using the zeroLevelTol and zeroLevel values, so they can be configured by the problem author).

from pg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.