Ask Your Question

tiger's profile - activity

2019-03-08 02:00:41 +0200 received badge  Student (source)
2019-03-08 01:56:52 +0200 commented question Why do I need to use real or real() some of the times?

ps: https://gist.github.com/kaovilai/8c5134a433e5288324a1e2708552144f ("library")

2019-03-07 19:21:30 +0200 asked a question Why do I need to use real or real() some of the times?

I am making my own library to deal with polar to Cartesian conversions and for getting radius and angles in degrees. I use .real and .imag quite a bit and have hit a wall when sometimes it would stop working due to error such as "built-in method real_part of sage.symbolic.expression.Expression" not compatible with method or something like that. This is what I came up with as my solution.

 def getReal(im):
    if type(im)==complex:
        return im.real
    return im.real()
def getImag(im):
    if type(im)==complex:
        return im.imag
    return im.real()

This resolves issue with incompatible expressions

My questions are

  • Why is this necessary?
  • How should I go about making my libraries so that it can be imported into other files without copying over the whole source?
  • How do I create methods such as above that will work on types so I can instead of getReal(i) call i.getReal()?

Thanks!