Complex/imaginary numbers in elisp?

不羁岁月 提交于 2021-02-08 04:08:37

问题


Does elisp support imaginary numbers is any way? I'm trying to follow an online math course with lisp interaction mode buffer running. Are there "higher math" modules/libraries for Emacs elisp?


回答1:


Emacs includes calc, a comprehensive calculator which supports complex numbers.

The manual is here: C-hig (calc) RET

Other nodes of particular note are:

  • (calc) Complex Numbers
  • (calc) Complex Number Functions
  • (calc) Calling Calc from Your Programs



回答2:


I just found a nice example of calling Calc: Mastering Emacs; Fun with Emacs Calc It talks about embedding Calc in elisp code with the calc-eval function:

ELISP> (calc-eval "1+2")
"3"

It gives an example of calling Calc with calc-eval inside elisp code.




回答3:


Unfornutabilly emacs lisp doesn't have native complex numbers. It only has as described in manualenter link description here:

Integer Basics: Representation and range of integers.
Float Basics: Representation and range of floating point. 

Also you cannot overlap the basic math functions to use another type of number see this discussion

Unfortunately you can't practically advise functions that have been assigned bytecode opcodes. This includes * and most of the other math operators. Your advise won't fire for any functions that have been byte-compiled. This caught me by surprise a few years back: The Limits of Emacs

So it is not easy to add this fearture, fortunatelly emacs lisp is a lisp descendant, so you can have other many solutions.

There is a package that you can use complex numbers:

The "cplx" package represents complex numbers as a pair of floats in a cons cell.

And also add the basic operations for numbers interacting wirh emacs lisp numbers.

Also as pointed in @phils answer, inside emacs exists calc, that is described as and avanced desk calculator, based in HP-S28, it is also an algebra system written in Elisp, that you can extend easilly, and support all types of numbers, you can use calc or use it inside emacs lisp programs

Expressions as strings:

ELISP> (calc-eval "1 + 2i + 3")
"2 i + 4"

ELISP> (calc-eval "1 + 2i * 3")
"6 i + 1" 

or use directly the data types

ELISP> (calcFunc-add 3 '(float 5 0) '(cplx 4 5))
(cplx
 (float 12 0)
 5)

please read carefully the emacs lisp calc manual and take a look at some blogs talking of this topic like this one



来源:https://stackoverflow.com/questions/28629797/complex-imaginary-numbers-in-elisp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!