Convert program to multiply two polynomials from Python3/C++ to Pascal. lazarus / 21-General ---------------------------------------------------------------------- Fileex · 2021-01-05 12:27 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/ ---------------------------------------------------------------------- Warfley · 2021-01-05 12:47 Python is really easy: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Create ArraySetLength(prod, Length(A) + Length(B) - 1);// Fill with 0FillChar(prod, Length(Prod) * SizeOf(Prod[0]), 0);Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for i in range(m): This is a for loop from 1 to m-1: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for i := 0 to Length(A) - 1 doSame for: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for j in range(n):becomes: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for j := 0 to Length(B) - 1 doAnd this is literally the same syntax as in pascal: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i prod[i + j] += A[i] * B[j];Lastly this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i return prod; Sets the function result and exits the current function. So the pascal code would look like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function PolyProduct(const A, B: TPolynome): TPolynome; // assume TPolynome is something like array of Doublevar  i, j: SizeInt;begin  SetLength(Result, Length(A) + Length(B) - 1);  FillChar(Result, Length(Result) * SizeOf(Result[0]), 0);  for i:=0 to Length(A) - 1 do    for j:=0 to Length(B) - 1 do      Result[i + j] := A[i] * B[j];end; ---------------------------------------------------------------------- Handoko · 2021-01-05 12:53 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 · 2021-01-05 14:05 Quote from: Handoko on January 05, 2021, 01:53:06 pmPlease 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 · 2021-01-05 14:47 Quote from: Handoko on January 05, 2021, 01:53:06 pmPlease 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 ---------------------------------------------------------------------- Warfley · 2021-01-05 15:19 Quote from: MarkMLl on January 05, 2021, 03:47:03 pmOnly 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 · 2021-01-05 15:29 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. ---------------------------------------------------------------------- Warfley · 2021-01-05 15:48 Quote from: Handoko on January 05, 2021, 04:29:44 pmOkay, 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 · 2021-01-05 15:49 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. Quote from: Handoko on January 05, 2021, 04:29:44 pmI 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 ---------------------------------------------------------------------- MarkMLl · 2021-01-05 15:57 Quote from: Warfley on January 05, 2021, 04:19:40 pmYep, 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 ---------------------------------------------------------------------- ASBzone · 2021-01-05 19:24 Quote from: MarkMLl on January 05, 2021, 04:49:25 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... ---------------------------------------------------------------------- MarkMLl · 2021-01-05 19:44 Quote from: ASBzone on January 05, 2021, 08:24:59 pmWell, let's see how long it takes before we see further engagement from the OP... Yes, I agree. MarkMLl ---------------------------------------------------------------------- MarkMLl · 2021-01-05 21:48 Quote from: MarkMLl on January 05, 2021, 04:57:01 pmI 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 QuoteUnfortunately, 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 ---------------------------------------------------------------------- Fileex · 2021-01-05 23:34 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. ---------------------------------------------------------------------- MarkMLl · 2021-01-06 08:31 Quote from: Fileex on January 06, 2021, 12:34:43 amI 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 ---------------------------------------------------------------------- Fileex · 2021-01-07 00:09 I've got some problems. When I try to create dynamic array there is  "Identifier not found" error, when the array is declared there is "does not to be initialized ". If not use th third array double for loop is multipling every term of first polynomial with every term of second polynomial but i dont know how to simplyfiy this polynominal by summing numbers of the same power. ---------------------------------------------------------------------- Handoko · 2021-01-07 02:32 Quote from: Fileex on January 07, 2021, 01:09:59 amwhen the array is declared there is "does not to be initialized ". Did you mean "does not seem to be initialized"? All dynamic arrays need to be initialized before using them. You can use SetLength to initialize the array: https://www.freepascal.org/docs-html/rtl/system/setlength.html You can study the example posted by Warfley on post #1: https://forum.lazarus.freepascal.org/index.php/topic,52772.msg389713.h tml#msg389713 ---------------------------------------------------------------------- Bart · 2021-01-07 10:40 Quote from: Handoko on January 07, 2021, 03:32:55 amAll dynamic arrays need to be initialized before using them. You can use SetLength to initialize the array: That is a misconception. SetLength just changes the length of te array, if you enlarge it it ensures that new elements are effectively "zero-ed" out. You initialize the array by assigning a value to it, either via it's constructor or by setting it to nil. This misconception leads people to complain theys still get the error if they use SetLength when they think it initializes the array. Some Pascal dev can give you an example where this will lead to unexpected results. Ik keep forgetting that example. There is a reason the compiler warns you. Initialize it properly. (OK, it the array is a global variable it will be zero-ed out at program startup, effectively setting the array to nil.) Bart ---------------------------------------------------------------------- Peter H · 2021-01-07 11:40 I think, because the OP, Fileex wrote this homework was given after the first and only programming lesson, dynamic arrays are too advanced, because to understand them, you need basic knowledge about arrays, array indicies and memory allocation and how to use or how NOT to use them as procedure parameters. I would propose: First you must know how to multiply polynoms by hand with pencil and paper and what a polynom is. Without this knowledge you cannot write a program. Then, to get started, use arrays of fixed size, where the array indicies are the same as the polynom exponents, for simplicity. For example  the coefficients a,b,c,d of a polynom ax^3+bx^2+cx^1+dx^0 can be stored in an array var   poly: array [0..3] of double; if you need a more generic solution with dynamic arrays you can do this later, when you got started. Dynamic arrays are a problem by themself, and for this you need more than one programming lesson. The site initially recommended has a typo, they give the formula  multiply(A[0..m-1], B[0..n01]) but it must read  multiply(A[0..m-1], B[0..n-1]) Dont let you confuse this. Apart from this the problem is discussed in depth at a very high level and is too much for a first programming lesson. ---------------------------------------------------------------------- Fileex · 2021-01-07 12:34 For fixed size arrays i ve got program like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  St, St2, i, j:integer;  A: Array[0..100] of Longint;  B: Array[0..100] of Longint;  Result : Array[0..100] of Longint;Begin  write('St=');  readln(St);            for i:=0 to St do            readln(A[i]);  write('St2=');  readln(St2);            for j:=0 to St2 do            readln(B[j]);             for i:=0 to St1 do                for j:=0 to St2  do                    begin                    Result[i + j] := A[i] * B[j];                    write(Result[i + j], ' ');                  end;  readln;end.   St, St2 are degrees of the polynomials. The program returns the result of multiplication of each element of the polynomials, but i dont know how to simplify the resulting polynomial. I noticed that these are elements for which the sum of i + j is the same. ---------------------------------------------------------------------- Peter H · 2021-01-07 13:09 The program is ok so far (as a biginners project), but the algorithm you implemented is not correct or not complete. All partial results of same exponent and index must be added. There is nothing to simplyfy the algorithm is incomplete. The site you cited there is an example in "C" emphasized in a grey box and you can use this as a template. Hint: There is a line prod[i+j] += A*B[j]; "+=" means "summing up" in C/C++, in plain Pascal or Delphi you write Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i prod[i+j] := A[i]*B[j] + prod[i+j] ; First you should set all elements of your result array to zero, and then sum up the partial results of equal index and exponent, where (i+j) is the same. (if this hard to understand excuse my english, its not native ) ---------------------------------------------------------------------- Fileex · 2021-01-08 20:38 Thanks to everyone, everything works well. ---------------------------------------------------------------------- egsuh · 2021-01-11 06:02 Purely from logical view, Arrays of A and B should have the same integer (should be 0) as start, and incremented by 1 in order for the previous codes to work as are.