Let's assume some maxima function gave me a fraction as result. For example
(%o1) bla / blubb
where bla and blubb are again more complicated expressions.
Now I want to process numerator and denominator separately. How can I extract them to separate variables?
I know bla/blubb is internally represented as a list in lisp, so I could do
:lisp (setq $oneofthem (nth 2 $%o1));
to get the lisp representation of numerator or denominator. However, I feel the maxima and lisp representation differ in commutation, so I'm not sure in advance if the maxima variable oneofthem contains the first or second factor (either bla or (mtimes blubb -1)).
The main functions are part and inpart. See also
substinpart, pickapart, reveal.
Related
I have a huge expression around 231 terms and each of these expressions has some power of cos(e) or sin(e) and they can be mixed as well, each term also has an r(distance) term in the denominator raised to some power as well.
Here is a small portion of the expression
What I'd like to do is sum the expression over all angle e's and then over all r's and use lambdify and scipy to minimize the expression with respect to 4 other parameters present in the equation.
Things I tried
I have tried to do the sums using sum indexed in scipy but am not
able to make it work, the power bit is tricky also once I have the
sum indexed expression and I expand it how do i pass the list of
angle values at which to calculate the expression
Also since the expression is pretty large I'd like to do the sum indexing etc. in a loop without individually resolving expression for each power.
(If my question is not clear, let me know.)
This is how I finally managed to solve my problem -
Replaced cos(e) and sin(e) with variable cose and sine.
Iterated over the list of angles and used sympy.subs to replace cose and sine with math.cos(e) and math.sin(e) and kept adding the expressions obtained ditto for r as well.
This left me with only p1 and p2 and Q1 and Q2 which was required.
I couldnt use sympy lambdify and sum indexed but this got the job done.
Section 4.7.2 of the MIT/GNU Scheme Reference Manual states that
The IEEE floating-point number specification supports three special ‘numbers’: positive infinity (+inf), negative infinity (-inf), and not-a-number (NaN).
These constants, in addition to being well-defined IEEE floating-point values, are also useful for range arithmetic. However, I’m unable to use them in my programs:
1 ]=> +inf
;Unbound variable: +inf
Generating these values isn’t easy, either: expressions which seem like they ought to evaluate to floating-point infinities simply don’t:
1 ]=> (flo:/ 1. 0.)
;Floating-point division by zero
How can I input or generate infinite floating-point constants in MIT Scheme?
tests/runtime/test-arith.scm suggests using flo:with-exceptions-untrapped:
;;; XXX The nonsense about IDENTITY-PROCEDURE here serves to fake
;;; out bogus constant-folding which needs to be fixed in SF (and
;;; probably LIAR too).
(define (zero)
(identity-procedure 0.))
(define (nan)
(flo:with-exceptions-untrapped (flo:exception:invalid-operation)
(lambda ()
(flo:/ (zero) (zero)))))
(define (inf+)
(flo:with-exceptions-untrapped (flo:exception:divide-by-zero)
(lambda ()
(flo:/ +1. (zero)))))
(define (inf-)
(flo:with-exceptions-untrapped (flo:exception:divide-by-zero)
(lambda ()
(flo:/ -1. (zero)))))
The results display as #[NaN], #[+inf], #[-inf] but cannot be input that way.
I eval a lisp expression in scratch
(+ (/ 1 2) (/ 1 2))
I got a 0.
normally it should be 1.
As Oleg points out, operators usually default to integer arithmetic unless you include floating point arguments (like 1.0).
With respect to your question about rational number support, emacs-calc (which is part of emacs) supports many number types including fractions (i.e. rational numbers), complex numbers, infinite precision integers, etc. Your code must call emacs-calc functions (instead of /, etc.) in order to use calc's arithmetic.
GNU Emacs Calc Manual:
Fractions
Index of Lisp Math Functions
Try this way
(+ (/ 1.0 2) (/ 1.0 2))
According to emacs doc
Function: / dividend divisor &rest divisors
if all the arguments are integers, then the result is an integer too.
You can read all about numbers in elisp here:
C-hig (elisp) Numbers RET
As already indicated by tripleee, it is apparent that the answer is "no".
Emacs calc has rational data type: use colon, like 1:2 == 0.5 or 5:3 == 1 + 2:3 == 1:2:3.
This way Emacs calc simplifies expressions, for example if you deal with display resolutions for 1920:1080 it prints 16:9! If you want 1440p with the 16:9 ratio: 1440 * 16:9 ⇒ 2560.
I'm just starting to learn Lisp and was wondering how to display a rational as a decimal number with lots of digits.
If I use (float x), where x is a rational then it displays about 8 digits or so. But I want to display hundreds of digits.
You will have to implement an algorithm to basically do the long division and calculate the digits yourself. There is no native datatype capable of holding hundreds of decimal digits.
You can use CLISP, an implementation of Common Lisp. As an extension it provides floats with settable precision. See: http://clisp.cons.org/beta/impnotes/num-concepts.html#lfd
There are also systems like Maxima and Axiom that run on top of Common Lisp. These also can compute with high precision reals.
The Common Lisp standard though doesn't provide that.
There may be implementations on which (format nil "~,100F" x) does what you want. But on most this first converts to a float, then computes digits, which loses precision.
It's not too hard to program your own. The idea is to compute the parts before and after the decimal point as integers separately. Here's my proposal:
(defun number->string-with-fixed-decimal-places (x width &optional stream)
"Print an approximation of <x> with <width> digits after the decimal point."
(multiple-value-bind (int dec) (truncate x)
(let ((dec-shifted (truncate (* (abs dec) (expt 10 width)))))
(format stream "~d.~v,vd" int width #\0 dec-shifted))))
I have this problem to work on:
The sum higher order procedure can be generalised even further to capture the idea of combining terms with a fixed operator. The mathematical product operator is a specific example of this idea, with multiplication replacing the addition of the summation operator.
The procedure accumulate, started below, is intended to capture this idea. The combiner parameter represents the operator that is used to reduce the terms, and the base parameter represents the value that is returned when there are no terms left to be combined. For example, if we have already implemented the accumulate procedure, then we could define the sum procedure as:
(define sum (accumulate + 0))
Complete the definition of accumulate so that it behaves according to this description.
(define accumulate
(lambda (combiner base)
(lambda (term start next stop)
(if (> start stop)
...
...))))
I inserted as the last two lines:
base
(combiner base (accumulate (combiner start stop) start next stop))
but, I have no idea if this is correct nor how to actually use the sum procedure to call accumulate and hence sum up numbers.
This is a great way to learn how to fish. Much better
than being given a fish.
Until then, here's how to approach the problem. Write a
function which would do what (accumulate + 0) would do. Don't use the accumulate function; just write a defun which which does what your homework asks. Next, write a function which would do what (accumulate * 1) would do. What are the similarities, what are the differences between the two functions. For the most part, they should be identical except for the occurrence of the + and * operators.
Next, note that the accumulate function is to return a function which will look a lot like the two functions you wrote earlier. Now, using the insight that two functions you wrote are very similar, think how to apply that to the function which (defun accumulate ...) is to return.