{"id":3260,"date":"2023-11-26T08:51:41","date_gmt":"2023-11-26T03:21:41","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=3260"},"modified":"2023-11-26T13:27:07","modified_gmt":"2023-11-26T07:57:07","slug":"generating-polynomials-in-prolog","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2023\/11\/26\/generating-polynomials-in-prolog\/","title":{"rendered":"Generating Polynomials in Prolog"},"content":{"rendered":"<p>Polynomial is an important topic in High School maths curriculum. There are many online courses that explain the topic in great details with lots of examples and sample exercises. Wouldn\u2019t it be interesting if we can generate polynomials of given degree programmatically? It turns out that this is not such a hard problem after all.<\/p>\n<p>Out of interest, I decided to experiment with this idea in <em><strong>Prolog<\/strong><\/em>. Why <em><strong>Prolog<\/strong><\/em>? No doubt, this can be implemented in any other language such as <em><strong>Python<\/strong><\/em>, <em><strong>Lisp<\/strong><\/em>, <em><strong>C++<\/strong><\/em>, <em><strong>Rust<\/strong><\/em>, etc., but I chose <em><strong>Prolog<\/strong><\/em> because polynomials have a well-defined syntax and structure, and this can be elegantly represented in <em><strong>Prolog<\/strong><\/em> in terms of <em><strong>Definite Clause Grammar (DCG)<\/strong><\/em> rules. Although <em><strong>DCGs<\/strong><\/em> are typically used as a <em><strong>\u201cparsing\u201d<\/strong><\/em> mechanism, we can use them for <em><strong>\u201cgeneration\u201d<\/strong><\/em> as well. Interested readers may go through my earlier article on <a href=\"https:\/\/www.rangakrish.com\/index.php\/2019\/08\/04\/generating-poetry-in-prolog\/\" target=\"_blank\" rel=\"noopener\"><em><strong>\u201cGenerating Poetry in Prolog\u201d<\/strong><\/em><\/a>.<\/p>\n<p>The core generation logic is shown in the following figure:<\/p>\n<figure id=\"attachment_3261\" aria-describedby=\"caption-attachment-3261\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"3261\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/11\/26\/generating-polynomials-in-prolog\/code1-7\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1.jpg\" data-orig-size=\"1089,590\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1700839380&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Generating Polynomials &amp;#8211; Main Clauses\" data-image-description=\"&lt;p&gt;Generating Polynomials &amp;#8211; Main Clauses&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Generating Polynomials &amp;#8211; Main Clauses&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1-1024x555.jpg\" class=\"wp-image-3261\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1.jpg?resize=600%2C325&#038;ssl=1\" alt=\"Generating Polynomials - Main Clauses\" width=\"600\" height=\"325\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1.jpg?resize=300%2C163&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1.jpg?resize=1024%2C555&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1.jpg?resize=768%2C416&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Code1.jpg?w=1089&amp;ssl=1 1089w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-3261\" class=\"wp-caption-text\"><strong>Generating Polynomials &#8211; Main Clauses<\/strong><\/figcaption><\/figure>\n<p><em><strong>\u201cbasic_expr\u201d<\/strong><\/em> models a single term. The clauses <em><strong>\u201cpolynomial_1\u201d<\/strong><\/em>, <em><strong>\u201cpolynomial_2a\u201d<\/strong><\/em>, and <em><strong>\u201cpolynomial_2b\u201d<\/strong><\/em> cover multiple terms. <em><strong>\u201cpolynomial2\u201d<\/strong><\/em> helps in randomly choosing or ignoring a term of specific degree.<\/p>\n<p>The main clause for polynomial generation is <em><strong>\u201cpolynomial\u201d<\/strong><\/em>. It takes a <em><strong>\u201cvariable\u201d<\/strong><\/em> and <em><strong>\u201cdegree<\/strong><\/em>\u201d as arguments and generates a polynomial of that degree, using the supplied variable.<\/p>\n<p>In line 24, the clause <em><strong>\u201cpolynomial_equation\u201d<\/strong><\/em> builds upon the polynomial by turning it into an equation, where the <em><strong>RHS<\/strong><\/em> is zero.<\/p>\n<p>A few other clauses act as helpers in the generation process. For example, the \u201cadd_operator\u201d randomly chooses a \u201c+\u201d or \u201c-\u201c to combine multiple terms. See this figure:<\/p>\n<figure id=\"attachment_3262\" aria-describedby=\"caption-attachment-3262\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Misc.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3262\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/11\/26\/generating-polynomials-in-prolog\/misc\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Misc.jpg\" data-orig-size=\"797,431\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1700839413&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Helper Code\" data-image-description=\"&lt;p&gt;Helper Code&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Helper Code&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Misc.jpg\" class=\"wp-image-3262\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Misc.jpg?resize=550%2C297&#038;ssl=1\" alt=\"Helper Code\" width=\"550\" height=\"297\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Misc.jpg?resize=300%2C162&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Misc.jpg?resize=768%2C415&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Misc.jpg?w=797&amp;ssl=1 797w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3262\" class=\"wp-caption-text\"><strong>Helper Code<\/strong><\/figcaption><\/figure>\n<p>The sample output, when run inside <em><strong>SICStus Prolog IDE<\/strong><\/em>, looks like this:<\/p>\n<figure id=\"attachment_3263\" aria-describedby=\"caption-attachment-3263\" style=\"width: 250px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Output.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3263\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/11\/26\/generating-polynomials-in-prolog\/output-8\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Output.jpg\" data-orig-size=\"315,472\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1700839176&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Generated Polynomial Equations\" data-image-description=\"&lt;p&gt;Generated Polynomial Equations&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Generated Polynomial Equations&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Output.jpg\" class=\"wp-image-3263\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Output.jpg?resize=250%2C375&#038;ssl=1\" alt=\"Generated Polynomial Equations\" width=\"250\" height=\"375\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Output.jpg?resize=200%2C300&amp;ssl=1 200w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/11\/Output.jpg?w=315&amp;ssl=1 315w\" sizes=\"(max-width: 250px) 100vw, 250px\" \/><\/a><figcaption id=\"caption-attachment-3263\" class=\"wp-caption-text\"><strong>Generated Polynomial Equations<\/strong><\/figcaption><\/figure>\n<p>The <em><strong>\u201cgenerate\u201d<\/strong> <\/em>predicate takes an integer argument and prints that many polynomial equations.<\/p>\n<p>Although this is an example of <em><strong>\u201cunivariate\u201d<\/strong><\/em> polynomial, the program can be extended to handle multiple variables also.<\/p>\n<p>The code has been tested in <a href=\"https:\/\/sicstus.sics.se\/\" target=\"_blank\" rel=\"noopener\"><em><strong>SICStus Prolog 4.8<\/strong><\/em><\/a>. You can download the source <a href=\"https:\/\/www.rangakrish.com\/downloads\/Polynomials.pro\" target=\"_blank\" rel=\"noopener\"><strong><em>here<\/em><\/strong><\/a>.<\/p>\n<p>Have a nice weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Polynomial is an important topic in High School maths curriculum. There are many online courses that explain the topic in great details with lots of examples and sample exercises. Wouldn\u2019t it be interesting if we can generate polynomials of given degree programmatically? It turns out that this is not such a hard problem after all. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[17,147],"tags":[374,148],"class_list":["post-3260","post","type-post","status-publish","format-standard","hentry","category-programming","category-prolog","tag-polynomial-equations","tag-prolog"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-QA","jetpack-related-posts":[{"id":3271,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/12\/10\/generating-polynomials-part-2\/","url_meta":{"origin":3260,"position":0},"title":"Generating Polynomials &#8211; Part 2","author":"admin","date":"December 10, 2023","format":false,"excerpt":"In my last article, I showed how we can use Prolog to generate univariate Polynomials. After I finished that article, I wanted to try Lisp for the same task. Prolog is \u201cdeclarative\u201d, whereas Lisp is primarily \u201cfunctional\u201d (it supports OOP as well), so the implementation will exhibit that difference. Here\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Function to Generate Polynomial","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/12\/code1-300x139.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/12\/code1-300x139.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/12\/code1-300x139.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2433,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/06\/06\/getting-to-know-visual-prolog-10\/","url_meta":{"origin":3260,"position":1},"title":"Getting to Know Visual Prolog 10","author":"admin","date":"June 6, 2021","format":false,"excerpt":"Visual Prolog\u00a0has its roots in Turbo Prolog, which was popularized by Borland in the mid 1980s. It is developed and supported by PDC A\/S, Denmark. So how is Visual Prolog different from other Prolog implementations out there, for example, SWI-Prolog or Sicstus Prolog? Here are the key differences: 1) It\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Visual Prolog Project","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Project-300x183.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Project-300x183.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/06\/Project-300x183.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2804,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/15\/students-assessment-advisor-in-prolog\/","url_meta":{"origin":3260,"position":2},"title":"Students Assessment Advisor in Prolog","author":"admin","date":"May 15, 2022","format":false,"excerpt":"A close relative of mine teaches Maths to school students in different Grades. During a casual chat some time ago, he mentioned that he was trying to adopt an automated approach to selecting assignment problems based on each student's performance so far. Obviously, there are many ways in which this\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"Concept Dependencies","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Concept-Dependencies-300x128.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Concept-Dependencies-300x128.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Concept-Dependencies-300x128.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1025,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/08\/19\/sicstus-prolog-building-a-windows-executable\/","url_meta":{"origin":3260,"position":3},"title":"Sicstus Prolog &#8211; Building a Windows Executable","author":"admin","date":"August 19, 2018","format":false,"excerpt":"In my previous post, I showed how to build a C-based Windows DLL to execute Prolog predicates in the Sicstus Prolog\u00a0engine. Today, I want to show how to build an executable (on Windows) from a C\/C++ program that uses Sicstus prolog engine. The process is quite simple. In order to\u2026","rel":"","context":"In &quot;Natural Language Processing&quot;","block_context":{"text":"Natural Language Processing","link":"https:\/\/www.rangakrish.com\/index.php\/category\/natural-language-processing\/"},"img":{"alt_text":"Creating WordNet Prolog Image","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/08\/Creating-image.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/08\/Creating-image.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/08\/Creating-image.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2832,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/12\/definite-clause-grammars-in-lisp-part-4\/","url_meta":{"origin":3260,"position":4},"title":"Definite Clause Grammars in Lisp &#8211; Part 4","author":"admin","date":"June 12, 2022","format":false,"excerpt":"In a series of articles\u00a0written earlier, I had shown how it is possible to model Definite Clause Grammars (DCG) in LispWorks Lisp (Enterprise Edition). We use defgrammar\u00a0in Common Prolog (available as part of KnowledgeWorks package) to define our grammar rules. Here is a toy English grammar represented using defgrammar: This\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"DCG Using Defgrammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/defgrammar-version-300x177.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/defgrammar-version-300x177.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/defgrammar-version-300x177.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":3555,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/11\/22\/multiobjective-optimization-moo-in-lisp-and-prolog\/","url_meta":{"origin":3260,"position":5},"title":"Multiobjective Optimization (MOO) in Lisp and Prolog","author":"admin","date":"November 22, 2024","format":false,"excerpt":"Recently I came across a nice article\u00a0by Jose Crespo, where the author stresses that the future of programming revolves around the application of math concepts such as Functor, Monads, Folds, etc. In addition, he argues that familiarity with C\/C++ is essential in this modern AI age. The author goes through\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Lisp Code for Pareto Front","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/11\/lisp-code-249x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=3260"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3260\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=3260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=3260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=3260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}