For Programmers: Free Programming Magazines  


Home > Archive > Prolog > October 2004 > List quastion









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author List quastion
kimos

2004-10-10, 8:56 am

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
Bill Spight

2004-10-10, 3:56 pm

Dear Kimos,

> 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.
>


Hint: How do you multiply polynomials?

Best wishes,

Bill
ezze

2004-10-10, 3:56 pm

"kimos" <urdad@hotmail.com> a écrit dans le message de news:
ae02c0a1.0410100403.3094ca9a@posting.google.com...
> 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.



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


kimos

2004-10-11, 3:59 pm

"ezze" <ezze@wanadoo.fr> wrote in message news:<4169565a$0$28826$8fcfb975@news.wanadoo.fr>...
> "kimos" <urdad@hotmail.com> a écrit dans le message de news:
> ae02c0a1.0410100403.3094ca9a@posting.google.com...
>
>
> poly([],[]).
> poly([X|L],[X2|L2]):-
> X2 is X*2,
> poly(L,L2).


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
Bill Spight

2004-10-11, 3:59 pm

Dear kimos,

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


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
ezze

2004-10-12, 3:58 pm


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


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)


kimos

2004-10-12, 3:58 pm

Bill Spight <bspight@pacXbell.net> wrote in message news:<416AE319.CF646229@pacXbell.net>...
> 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



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.
Bill Spight

2004-10-12, 3:58 pm

Dear kimos,

> but im still stuck with the second one as u mention "multiply
> polynomials, represented as lists"


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
Sangai

2004-10-13, 3:56 am

Bill Spight <bspight@pacXbell.net> wrote in message news:<416C3295.F1F417B7@pacXbell.net>...
> 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.


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

2004-10-13, 3:59 pm

sangai@freemail.hu (Sangai) wrote in message news:<f3c0ce7.0410122226.296b1e63@posting.google.com>...
> Bill Spight <bspight@pacXbell.net> wrote in message news:<416C3295.F1F417B7@pacXbell.net>...
>
> It could be a little bit easier to solve the problem by representing
> (2x^2 + 3x + 4) with [4,3,2] ...



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

2004-10-13, 3:59 pm

urdad@hotmail.com (kimos) wrote in message news:<ae02c0a1.0410100403.3094ca9a@posting.google.com>...
> Hi There,
> ...
> 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.
>



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

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

2004-10-13, 8:56 pm

kimos wrote:
> sangai@freemail.hu (Sangai) wrote in message news:<f3c0ce7.0410122226.296b1e63@posting.google.com>...
>
>
>
>
> Thats true and thats what im trying to do represent it as [4,3,2].
> but i cant find any answer for it


Hmm.

At first glance, I think I would probably use something like

coeff(a, k)

to represent the polynomial term

a*x^k

For example,

7*x^5 - 9*x^2 + 3

could be represented as

[ coeff(7,5), coeff(9,2), coeff(3,0) ].

or any permutation thereof.

The rule

a*x^i + b*x^i = (a+b)*x^i

would become

coeff(a, i) + coeff(b, i) = coeff(a+b, i)

and the rule

( a*x^i ) * ( b*x^j ) = (a*b) * x^(i+j)

would become

coeff(a, i) * coeff(b, j) = coeff(a*b, i+j)


I have not tested this idea!

--
billh
Sangai

2004-10-14, 3:58 pm

urdad@hotmail.com (kimos) wrote in message news:<ae02c0a1.0410130711.6536fa39@posting.google.com>...
> sangai@freemail.hu (Sangai) wrote in message news:<f3c0ce7.0410122226.296b1e63@posting.google.com>...
>
> Thats true and thats what im trying to do represent it as [4,3,2].
> but i cant find any answer for it


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
kimos

2004-10-14, 3:58 pm

tmp123@menta.net (Pere Montolio) wrote in message news:<d0867bad.0410131013.1d98a3ad@posting.google.com>...
> urdad@hotmail.com (kimos) wrote in message news:<ae02c0a1.0410100403.3094ca9a@posting.google.com>...
>
>
> Which is the expected answer for
> prod_poly([3],[4],Z).
>
> It is Z=[12] or Z=[1,2] ?



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

2004-10-15, 8:56 am

student wrote:
> kimos wrote:
>
>
>
> Hmm.
>
> At first glance, I think I would probably use something like
>
> coeff(a, k)
>
> to represent the polynomial term
>
> a*x^k
>


Oops.

> For example,
>
> 7*x^5 - 9*x^2 + 3
>
> could be represented as
>
> [ coeff(7,5), coeff( -9 , 2), coeff(3,0) ].
>
> or any permutation thereof.
>


etc.
Pere Montolio

2004-10-16, 8:56 am

urdad@hotmail.com (kimos) wrote in message news:<ae02c0a1.0410140615.cf5dea3@posting.google.com>...
> tmp123@menta.net (Pere Montolio) wrote in message news:<d0867bad.0410131013.1d98a3ad@posting.google.com>...
>
>
> Z=[1,2] is the expected answer
> im still stuck with it



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:
Bart Demoen

2004-10-18, 4:00 pm

Pere Montolio wrote:
> urdad@hotmail.com (kimos) wrote in message news:<ae02c0a1.0410140615.cf5dea3@posting.google.com>...
>
>


This thread started on 10 Oct and died on 16 Oct. There was no real
conclusion nor a final answer. I think even the question was never
clear. This suggests that the initial question was homework and that
c.l.p. did not provide a solution. Excellent.

And what did c.l.p. get out of it ? Almost nothing.

But we can get something out if we want to: let c.l.p. resume this
thread and let people send in LP/Prolog programs that implement
arithmetic on polynomials (as suggested by one contributor, the
representation of 3x^2-7x+12 could be [12,-7,3]): the homework deadline
was probably during the wend (yesterday) so don't worry about that
anymore.

Since the OP wants ?- prod_poly([3],[4],Z). to give Z = [2,1] (*)
(s)he might have wanted arithmetic on decimal representations of
numbers rather than arithmetic on polynomials (they differ and are
both interesting). So we could do that too.

Any takers ?

Cheers

Bart Demoen

(*) Z = [1,2] is (in view of the good suggestion for representing
polynomials) of course not so good: OPs don't always know what's best
for them :-)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com