Recent

Author Topic: Convert program to multiply two polynomials from Python3/C++ to Pascal.  (Read 4617 times)

Fileex

  • Newbie
  • Posts: 5
Hi, could someone help with converting this code to Pascal?. To be honest I dont know Python very well.
https://www.geeksforgeeks.org/multiply-two-polynomials-2/
« Last Edit: January 05, 2021, 01:31:14 pm by Fileex »

Warfley

  • Hero Member
  • *****
  • Posts: 2040
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #1 on: January 05, 2021, 01:47:34 pm »
Python is really easy:
Code: Pascal  [Select][+][-]
  1. prod = [0] * (m + n - 1);
This creates an array of length m + n - 1 filled with 0. M is the length of A and n is the Length of B:
Code: Pascal  [Select][+][-]
  1. // Create Array
  2. SetLength(prod, Length(A) + Length(B) - 1);
  3. // Fill with 0
  4. FillChar(prod, Length(Prod) * SizeOf(Prod[0]), 0);
Code: Pascal  [Select][+][-]
  1. for i in range(m):
This is a for loop from 1 to m-1:
Code: Pascal  [Select][+][-]
  1. for i := 0 to Length(A) - 1 do
Same for:
Code: Pascal  [Select][+][-]
  1. for j in range(n):
becomes:
Code: Pascal  [Select][+][-]
  1. for j := 0 to Length(B) - 1 do
And this is literally the same syntax as in pascal:
Code: Pascal  [Select][+][-]
  1. prod[i + j] += A[i] * B[j];
Lastly this
Code: Pascal  [Select][+][-]
  1. return prod;
Sets the function result and exits the current function.

So the pascal code would look like this:
Code: Pascal  [Select][+][-]
  1. function PolyProduct(const A, B: TPolynome): TPolynome; // assume TPolynome is something like array of Double
  2. var
  3.   i, j: SizeInt;
  4. begin
  5.   SetLength(Result, Length(A) + Length(B) - 1);
  6.   FillChar(Result, Length(Result) * SizeOf(Result[0]), 0);
  7.   for i:=0 to Length(A) - 1 do
  8.     for j:=0 to Length(B) - 1 do
  9.       Result[i + j] := A[i] * B[j];
  10. end;
« Last Edit: January 05, 2021, 01:51:49 pm by Warfley »

Handoko

  • Hero Member
  • *****
  • Posts: 5524
  • My goal: build my own game engine using Lazarus
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #2 on: January 05, 2021, 01:53:06 pm »
Please don't reply to backlink builder. The TS said he don't know Python, so for what reason he needs it convert to Pascal? Spam.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12717
  • FPC developer.
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #3 on: January 05, 2021, 03:05:44 pm »
Please don't reply to backlink builder. The TS said he don't know Python, so for what reason he needs it convert to Pascal? Spam.

I don't think it is spam.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #4 on: January 05, 2021, 03:47:03 pm »
Please don't reply to backlink builder. The TS said he don't know Python, so for what reason he needs it convert to Pascal? Spam.

I can see both sides of that argument. I've certainly found myself converting an algorithm expressed in Python to Pascal before now... and I not only don't know Python well but I explicitly loathe its lack of block markers.

Only significant issue was  mod  behaviour, which has been previously discussed in this forum.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Warfley

  • Hero Member
  • *****
  • Posts: 2040
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #5 on: January 05, 2021, 04:19:40 pm »
Only significant issue was  mod  behaviour, which has been previously discussed in this forum.
Yep, python is much more designed with mathematics in mind than like Pascal or C. The modulo in Pascal or C is probably just the way the hardware implements it, while this doesn't make much sense math wise.

I was really confused the first time I tried to implement discrete math algorithms with the way Pascal computes the modulo, and that effectively you always want to use (x mod d + d) mod d instead of x mod d, and was even more confused when later I learned python and got asked why I write these weird formulas... Well, even though I don't like python syntax wise, there are just a few things python makes very easy and much more convenient than low level languages like Pascal

Handoko

  • Hero Member
  • *****
  • Posts: 5524
  • My goal: build my own game engine using Lazarus
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #6 on: January 05, 2021, 04:29:44 pm »
Usually when a programmer needs to convert code from one language to another language because he wants to migrate the code that he wrote. Or some libraries written by others.

The code provided by TS is not a library, it's just a function, which looks more like a school homework. But that is not his homework because a teacher should not give task for converting code.

If TS is a student of Pascal why should he choose the lesson to convert code from the language he is not familiar? If he want to learn how to do polynomial in Pascal, he can ask it directly. Why does he need to search the example code written by others? Especially from different language?

If TS is a student of Python. Why does he want to convert the code to other language? That could be if only he already mastered Python and want to learn some new languages.

Okay, I could be wrong. But usually when someone has code that he cannot understand, most of the time they copy paste the code to the forum. Why should TS post a link that look spammy?

Have you visit the link? That website is tutorial website, there has many skilled programmers. Or at least teachers who teach programming. It is not hard to find a contact form on the website. Why does TS not contact them when he found something that he cannot understand on that website?

I suggest delete this whole thread. Or modify the first post, delete the link but copy paste the code.
« Last Edit: January 05, 2021, 04:41:57 pm by Handoko »

Warfley

  • Hero Member
  • *****
  • Posts: 2040
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #7 on: January 05, 2021, 04:48:19 pm »
Okay, I could be wrong. But usually when someone has code that he cannot understand, most of the time they copy paste the code to the forum. Why should TS post a link that look spammy?

Have you visit the link? That website is tutorial website, there has many skilled programmers. Or at least teachers who teach programming. It is not hard to find a contact form on the website. Why TS do not contact them when he found something that he cannot understand on that website?

What looks spammy about geeksforgeeks? This is a well known website that has codesnippets and tutorials for most algorithms and other programming related stuff and is always near the top in the google search results. It's one of the most well known programming tutorial resources in the internet. I also find myself often on their page when I have to refresh knowledge of algorithms like A*, because they usually have pretty nice examples often in pseudocode and are one of the top search results. This website is about as spammy as wikipedia is, and I often link to wikipedia.

And about contacting them, this website hosts hundereds if not thousands of tutorials, visited by probably thousands of people a day and is managed by a small team (4 people running the buisness and another 5 main contributors), with a lot of the contributions made by other people on the internet. Do you really think they can (and will) answer every question they get?

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #8 on: January 05, 2021, 04:49:25 pm »
As I've already said, I've been in a very similar situation having ended up looking at somebody's Python example code with limited understanding of the language. Fortunately I was able to sort it out, but would have found it far more difficult had it not been for the earlier discussion of the questionable  mod  implementation.

As for /why/ OP has ended up looking at that source of information, I speculate that he has limited experience and might even be of the "Google says that..." school of Internet users. Any further criticism of his methodology and assumptions should really be down to his teachers/lecturers, since if they care enough to set problems in Pascal they will of course be members of this forum.

I suggest delete this whole thread. Or modify the first post, delete the link but copy paste the code.

And /I/ suggest that you're being uncharacteristically mean.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #9 on: January 05, 2021, 04:57:01 pm »
Yep, python is much more designed with mathematics in mind than like Pascal or C. The modulo in Pascal or C is probably just the way the hardware implements it, while this doesn't make much sense math wise.

I speculate... I believe that Wirth at the time was working on a CDC, (largely) designed by Seymour Cray. Later machines bearing Cray's own name used multiplicative inverses (?) to handle division, and it might be that his earlier ones had similarly quirky implementations of the modulo operator.

I can't comment on how it was handled by other systems of which Wirth had experience, i.e. IBM704 (?), B5500 and to some extent S/360. But all of these are sufficiently old that it's entirely possible for them not to have "done it the right way".

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ASBzone

  • Hero Member
  • *****
  • Posts: 733
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #10 on: January 05, 2021, 08:24:59 pm »

And /I/ suggest that you're being uncharacteristically mean.

MarkMLl


Well, let's see how long it takes before we see further engagement from the OP...
-ASB: https://www.BrainWaveCC.com/

Lazarus v4.3.0.0 (bcf314a670) / FreePascal v3.2.3-46-g77716a79dc (aka fixes)
(Windows 64-bit install w/Win32 and Linux on ARM and x64 cross-compilers via FpcUpDeluxe)

My Systems: Windows 10/11 Pro x64 (Current)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #11 on: January 05, 2021, 08:44:46 pm »
Well, let's see how long it takes before we see further engagement from the OP...

Yes, I agree.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #12 on: January 05, 2021, 10:48:20 pm »
I can't comment on how it was handled by other systems of which Wirth had experience, i.e. IBM704 (?), B5500 and to some extent S/360. But all of these are sufficiently old that it's entirely possible for them not to have "done it the right way".

Noting the tests at https://forum.lazarus.freepascal.org/index.php/topic,48549.0.html

Quote
Unfortunately, Pascal's mod does not satisfy the above property.
For example: 16 mod 12 = 4
however: (16 - 3*12) mod 12 = -8
... (should be)
(16 + abs(-3 *12)) mod 12 = 4

I've just run this fragment of ALGOL through a B5500 emulator:


  WRITE(OUTPUT, <"16 MOD 12: ", I8>, 16 MOD 12);
  WRITE(OUTPUT, <"(16 - 3*12) MOD 12: ", I8>, (16 - 3*12) MOD 12);


The results that appeared on the output cards were


16 MOD 12:        4
(16 - 3 | 12) MOD 12:       -8


So since the machine on which Wirth did his PhD didn't have a divide instruction, it's reasonable to assume that he picked up his "bad habits" working on the B5500 at Stanford. The Burroughs ALGOL manual states that

Y MOD Z = Y - (Z * (SIGN (Y/Z) * ENTIER (ABS (Y/Z))))

but I haven't attempted to reconcile that with the published ALGOL-60 specification or any other variant of reality.

Apologies to OP for hijacking his thread in the interest of historical completeness :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Fileex

  • Newbie
  • Posts: 5
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #13 on: January 06, 2021, 12:34:43 am »
Thanks for the replies.

For everyone wondering about this post.

Sorry for any misunderstanding, not bad intentions for spam or etc

I m studying Graphic Design at the University of Arts. One of my subjects is the basics of the programming, from which I got a homework to do simply program to multiply polynomials in Delphi.

I found this algorithm on another forum, but in other programming language thats why I asked for conversion.

I'm sorry for wasting your time. I'm not very good at programming, but I got this work after only one programming lesson on Univ. Im still no understand it, I would be grateful for your help, and could also offer my help in designing in PS/Illustrator/InDesign.
« Last Edit: January 06, 2021, 12:38:49 am by Fileex »

MarkMLl

  • Hero Member
  • *****
  • Posts: 8551
Re: Convert program to multiply two polynomials from Python3/C++ to Pascal.
« Reply #14 on: January 06, 2021, 09:31:15 am »
I m studying Graphic Design at the University of Arts. One of my subjects is the basics of the programming, from which I got a homework to do simply program to multiply polynomials in Delphi.

I found this algorithm on another forum, but in other programming language thats why I asked for conversion.

I'm sorry for wasting your time. I'm not very good at programming, but I got this work after only one programming lesson on Univ. Im still no understand it, I would be grateful for your help, and could also offer my help in designing in PS/Illustrator/InDesign.

Thanks for the explanation, and hopefully reply #1 has something for you to go on. Please continue the discussion with any definite questions, with the obvious caveat that homework/assignments are ultimately your responsibility.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018