Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

CLisp: Using mapcar for polynomial multiplication

$
0
0
(common lisp, sorry I forgot to put it in the title, cant edit it as far as I know)
Generally speaking, I know how to use mapcar, but this particular situation has me stumped.

Assume that we have two terms, and two lists (each list has two terms)

you know how if you were to multiply a polynomial, you take the first term in the first list, and distribute it across every item in the second list, then goto the second item in the first list and multiply it to everything in the second list etc?

How would you do that in lisp?


I am using a structure that looks like:
(defstruct term coef exp)



the code:
(defun poly-mul (p q)

(labels (
	 (mlt (t1 t2)
	   (make-term 
	    :coef (* (term-coef t1)(term-coef t2))
	    :exp  (+ (term-exp t1)(term-exp t2))))
	    
	    (pml (t3 t4)
	    (if (null t3) nil
	    (cons (mapcar #'mlt  (list (car t3)) t4) (pml (cdr t3) t4))))
	    
	    
	    
	    )
(pml p q)

 ))




input: (poly-mul (list (make-term :coef 1 :exp 3) (make-term :coef 2 :exp 2))(list (make-term :coef 1 :exp 4) (make-term :coef 2 :exp 3)))

-
output: ((#S(TERM :COEF 1 :EXP 7)) (#S(TERM :COEF 2 :EXP 6)))

the input in more readable terms is: p:(1^3,2^2) q:(1^4, 2^3)

what I expect is something like 1^7, 2^6, 2^6, 4^5 or (1^7, 2^6) (2^6, 4^5)
(I intentionally omitted #S(term :coef :exp) etc)

im not sure why my code doesnt goto the next term in the first list properly, hopefully someone can help

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>