List quastion

List quastion

Post by urda » Mon, 11 Oct 2004 21:03:38


Hi There,

I want to make a predicate that do the following:

poly([1,2,3],2,Z).

Z= [2,4,6].

so it multiply the 2 with the list.

and another predicate that do the following:

prod_poly([1,2,3],[3,2,1],Z).

Z= [3,9,4,8,3]

this one see the elements in the list as 1 number so in this example
it will multiply 123 * 321 and return the answer in list Z. this
one is hard i hope someone can tell me how.

Thanks in advance
Kimos
 
 
 

List quastion

Post by Bill Spigh » Tue, 12 Oct 2004 00:07:48

Dear Kimos,


Hint: How do you multiply polynomials?

Best wishes,

Bill

 
 
 

List quastion

Post by ezze » Tue, 12 Oct 2004 00:34:17


XXXX@XXXXX.COM ...


poly([],[]).
poly([X|L],[X2|L2]):-
X2 is X*2,
poly(L,L2).
 
 
 

List quastion

Post by urda » Wed, 13 Oct 2004 03:50:24


ezze thanks for replay but this isnt what i want. my poly is with 3 inputs
poly([],[],[]).


Bill Spight i dont get your hint. how do you multiply polynomials ?

hope someone can help im realy stuck with thos 2 predicates
 
 
 

List quastion

Post by Bill Spigh » Wed, 13 Oct 2004 04:45:02

Dear kimos,


Example:

(5x^2 + 3x + 2) * (-4x^2 - 2x + 1) = y

prod_poly([5, 3, 2], [-4, -2, 1], Y).

Now the example you gave is not exactly that, but appears to assume that
x = 10, and carries partial products. However, given the name,
"prod_poly", I suspect that the meat of your assignment is to write a
program to multiply polynomials, represented as lists. That is something
that you learned to do in high school, I suspect.

Best wishes,

Bill
 
 
 

List quastion

Post by ezze » Thu, 14 Oct 2004 00:34:35


sure,
poly([],_,[]).
poly([X|L],V,[X2|L2]):-
X2 is X*V,
poly(L,V,L2).

its the first predicate youre looking for (in your first message)
 
 
 

List quastion

Post by urda » Thu, 14 Oct 2004 03:36:01


Dear Bill

thanks for the replay.

i solved the first predicate afer hard work

poly([],N,[]).
poly([X|L],N,[X2|L2]):-
X2 is X*N,
poly(L,L2).

but im still stuck with the second one as u mention "multiply
polynomials, represented as lists" they didnt learned prolog im doing
this for my graduation project.
 
 
 

List quastion

Post by Bill Spigh » Thu, 14 Oct 2004 04:36:37

Dear kimos,


Did you take algebra, and learn how to multiply (x^2 - x + 3) and (2X^2
+ 3x - 10)? What steps did you take to do that multiplication?

If you did not take algebra, think back to when you learned arithmetic.
What steps did you take to multiply 234 and 637? They are equvalent to
(2x^2 + 3x + 4) times (6x^2 + 3x + 7), where x = 10.

Good luck!

Bill
 
 
 

List quastion

Post by sanga » Thu, 14 Oct 2004 15:26:57


It could be a little bit easier to solve the problem by representing
(2x^2 + 3x + 4) with [4,3,2] ...
 
 
 

List quastion

Post by urda » Fri, 15 Oct 2004 00:11:52


Thats true and thats what im trying to do represent it as [4,3,2].
but i cant find any answer for it
 
 
 

List quastion

Post by tmp12 » Fri, 15 Oct 2004 03:13:56


Which is the expected answer for
prod_poly([3],[4],Z).

It is Z=[12] or Z=[1,2] ?
 
 
 

List quastion

Post by sanga » Fri, 15 Oct 2004 16:55:36


You can generate already (from the example above)
[28,21,14] (= [4,3,2] * 7) and
[12, 9, 6] (= [4,3,2] * 3)

You have to add them together only. Just don't forget, they mean
28 + 21x + 14x^2
_0_+ 12x + 9x^2 + 6x^3 ...

HTH
 
 
 

List quastion

Post by urda » Fri, 15 Oct 2004 23:15:56


Z=[1,2] is the expected answer
im still stuck with it
 
 
 

List quastion

Post by studen » Sat, 16 Oct 2004 17:01:29


Oops.


etc.
 
 
 

List quastion

Post by tmp12 » Sun, 17 Oct 2004 18:12:33


Thus, I thong you have to posible strategies:
a) The one the teachers doesn't like, but usually the best one:
convert your list to integer, multiply them, and convert the integer
to list
(it is perfect if your prolog has no limitation in integer's size).

b) Implement a multiply algorithm:
- a) Revert the order of list elements.
- b) Take first digit of one list and multiply the other list by it
("poly" can be now used).
- c) Do a recursive call.
- d) Add the result of "b)" to "c)", shiting "c)" (in order to
multiply by 10). Ruquirement, you need to implement an "addition"
rule: