|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Python Everything:\n", |
| 8 | + "8) **Showing polynomials in Jupyter Notebook**\n", |
| 9 | + "<br>Also, writing functions to add and multiply two polynomials $p(x)$ and $q(x).$\n", |
| 10 | + "<br>**Hint:** Here, polynomials are represented by *lists*.\n", |
| 11 | + "<br>\n", |
| 12 | + "<br> $p(x)=a_0+a_1x+a_2x^2+a_3x^3+...+a_nx^n$\n", |
| 13 | + "# نمایش بُلنامیها، در جوپیتر نوتبوک\n", |
| 14 | + "By Hamed Shah-Hosseini<br>\n", |
| 15 | + "Explanation in English:<br>\n", |
| 16 | + "https://www.pinterest.com/HamedShahHosseini/programming-languages/python\n", |
| 17 | + "<br> Explanation in Persian: https://www.instagram.com/words.persian\n", |
| 18 | + "<br>The code is at: https://github.com/ostad-ai/Python-Everything" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": 27, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "from IPython.display import Math, display" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": 28, |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "# the function to show polynomials represented by lists\n", |
| 37 | + "def show(poly,variable='x'):\n", |
| 38 | + " s=''\n", |
| 39 | + " for power,coef in enumerate(poly):\n", |
| 40 | + " if coef==0: continue\n", |
| 41 | + " if power==0:\n", |
| 42 | + " s+=f'{coef}+'\n", |
| 43 | + " elif power==1:\n", |
| 44 | + " s+=f'{coef}{variable}+'\n", |
| 45 | + " else:\n", |
| 46 | + " s+=f'{coef}{variable}^{{{power}}}+'\n", |
| 47 | + " display(Math(s[:-1]))" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "markdown", |
| 52 | + "metadata": {}, |
| 53 | + "source": [ |
| 54 | + "ِExample of displaying polynomials:<br>\n", |
| 55 | + "نمونهای از نمایش بُلنامیها" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": 30, |
| 61 | + "metadata": {}, |
| 62 | + "outputs": [ |
| 63 | + { |
| 64 | + "data": { |
| 65 | + "text/latex": [ |
| 66 | + "$\\displaystyle 1+3x+7x^{2}+11x^{3}+3x^{11}$" |
| 67 | + ], |
| 68 | + "text/plain": [ |
| 69 | + "<IPython.core.display.Math object>" |
| 70 | + ] |
| 71 | + }, |
| 72 | + "metadata": {}, |
| 73 | + "output_type": "display_data" |
| 74 | + } |
| 75 | + ], |
| 76 | + "source": [ |
| 77 | + "poly1=[1,3,7,11,0,0,0,0,0,0,0,3]\n", |
| 78 | + "show(poly1)" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": 12, |
| 84 | + "metadata": {}, |
| 85 | + "outputs": [ |
| 86 | + { |
| 87 | + "data": { |
| 88 | + "text/latex": [ |
| 89 | + "$\\displaystyle 4+8x^{2}+10x^{4}$" |
| 90 | + ], |
| 91 | + "text/plain": [ |
| 92 | + "<IPython.core.display.Math object>" |
| 93 | + ] |
| 94 | + }, |
| 95 | + "metadata": {}, |
| 96 | + "output_type": "display_data" |
| 97 | + }, |
| 98 | + { |
| 99 | + "data": { |
| 100 | + "text/latex": [ |
| 101 | + "$\\displaystyle 4+8y^{2}+10y^{4}$" |
| 102 | + ], |
| 103 | + "text/plain": [ |
| 104 | + "<IPython.core.display.Math object>" |
| 105 | + ] |
| 106 | + }, |
| 107 | + "metadata": {}, |
| 108 | + "output_type": "display_data" |
| 109 | + } |
| 110 | + ], |
| 111 | + "source": [ |
| 112 | + "poly2=[4,0,8,0,10]\n", |
| 113 | + "show(poly2,'x')\n", |
| 114 | + "show(poly2,'y')" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": 13, |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "# adding two polynomials P and Q\n", |
| 124 | + "# P and Q are represented by lists\n", |
| 125 | + "def add_polys(P,Q):\n", |
| 126 | + " m=len(P)\n", |
| 127 | + " n=len(Q)\n", |
| 128 | + " if m < n:\n", |
| 129 | + " result=Q.copy()\n", |
| 130 | + " for i in range(m):\n", |
| 131 | + " result[i]+=P[i]\n", |
| 132 | + " else:\n", |
| 133 | + " result=P.copy()\n", |
| 134 | + " for i in range(n):\n", |
| 135 | + " result[i]+=Q[i]\n", |
| 136 | + " return result" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "markdown", |
| 141 | + "metadata": {}, |
| 142 | + "source": [ |
| 143 | + "Example of adding two polynomials: <br>\n", |
| 144 | + "نمونهای از افزا کردن دو بُلنامی" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": 21, |
| 150 | + "metadata": {}, |
| 151 | + "outputs": [ |
| 152 | + { |
| 153 | + "data": { |
| 154 | + "text/latex": [ |
| 155 | + "$\\displaystyle 1+3x+7x^{2}+11x^{3}+3x^{11}$" |
| 156 | + ], |
| 157 | + "text/plain": [ |
| 158 | + "<IPython.core.display.Math object>" |
| 159 | + ] |
| 160 | + }, |
| 161 | + "metadata": {}, |
| 162 | + "output_type": "display_data" |
| 163 | + }, |
| 164 | + { |
| 165 | + "data": { |
| 166 | + "text/latex": [ |
| 167 | + "$\\displaystyle 4+8x^{2}+10x^{4}$" |
| 168 | + ], |
| 169 | + "text/plain": [ |
| 170 | + "<IPython.core.display.Math object>" |
| 171 | + ] |
| 172 | + }, |
| 173 | + "metadata": {}, |
| 174 | + "output_type": "display_data" |
| 175 | + }, |
| 176 | + { |
| 177 | + "name": "stdout", |
| 178 | + "output_type": "stream", |
| 179 | + "text": [ |
| 180 | + "The summation:\n" |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "data": { |
| 185 | + "text/latex": [ |
| 186 | + "$\\displaystyle 5+3x+15x^{2}+11x^{3}+10x^{4}+3x^{11}$" |
| 187 | + ], |
| 188 | + "text/plain": [ |
| 189 | + "<IPython.core.display.Math object>" |
| 190 | + ] |
| 191 | + }, |
| 192 | + "metadata": {}, |
| 193 | + "output_type": "display_data" |
| 194 | + } |
| 195 | + ], |
| 196 | + "source": [ |
| 197 | + "poly1_add_poly2=add_polys(poly1,poly2)\n", |
| 198 | + "show(poly1)\n", |
| 199 | + "show(poly2)\n", |
| 200 | + "print('The summation:')\n", |
| 201 | + "show(poly1_add_poly2)" |
| 202 | + ] |
| 203 | + }, |
| 204 | + { |
| 205 | + "cell_type": "code", |
| 206 | + "execution_count": 20, |
| 207 | + "metadata": {}, |
| 208 | + "outputs": [], |
| 209 | + "source": [ |
| 210 | + "# multiplying two polynomials P and Q\n", |
| 211 | + "# P and Q are represented by lists\n", |
| 212 | + "def mult_polys(P,Q):\n", |
| 213 | + " m=len(P)\n", |
| 214 | + " n=len(Q)\n", |
| 215 | + " R=[0]*(m+n-1)\n", |
| 216 | + " for i in range(m):\n", |
| 217 | + " for j in range(n):\n", |
| 218 | + " R[i+j]+=P[i]*Q[j]\n", |
| 219 | + " return R" |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "cell_type": "markdown", |
| 224 | + "metadata": {}, |
| 225 | + "source": [ |
| 226 | + "Example of multiplying two polynomials: <br>\n", |
| 227 | + "نمونهای از چندینکرد دو بُلنامی" |
| 228 | + ] |
| 229 | + }, |
| 230 | + { |
| 231 | + "cell_type": "code", |
| 232 | + "execution_count": 23, |
| 233 | + "metadata": {}, |
| 234 | + "outputs": [ |
| 235 | + { |
| 236 | + "data": { |
| 237 | + "text/latex": [ |
| 238 | + "$\\displaystyle 1+3x+7x^{2}+11x^{3}+3x^{11}$" |
| 239 | + ], |
| 240 | + "text/plain": [ |
| 241 | + "<IPython.core.display.Math object>" |
| 242 | + ] |
| 243 | + }, |
| 244 | + "metadata": {}, |
| 245 | + "output_type": "display_data" |
| 246 | + }, |
| 247 | + { |
| 248 | + "data": { |
| 249 | + "text/latex": [ |
| 250 | + "$\\displaystyle 4+8x^{2}+10x^{4}$" |
| 251 | + ], |
| 252 | + "text/plain": [ |
| 253 | + "<IPython.core.display.Math object>" |
| 254 | + ] |
| 255 | + }, |
| 256 | + "metadata": {}, |
| 257 | + "output_type": "display_data" |
| 258 | + }, |
| 259 | + { |
| 260 | + "name": "stdout", |
| 261 | + "output_type": "stream", |
| 262 | + "text": [ |
| 263 | + "The multiplication:\n" |
| 264 | + ] |
| 265 | + }, |
| 266 | + { |
| 267 | + "data": { |
| 268 | + "text/latex": [ |
| 269 | + "$\\displaystyle 4+12x+36x^{2}+68x^{3}+66x^{4}+118x^{5}+70x^{6}+110x^{7}+12x^{11}+24x^{13}+30x^{15}$" |
| 270 | + ], |
| 271 | + "text/plain": [ |
| 272 | + "<IPython.core.display.Math object>" |
| 273 | + ] |
| 274 | + }, |
| 275 | + "metadata": {}, |
| 276 | + "output_type": "display_data" |
| 277 | + } |
| 278 | + ], |
| 279 | + "source": [ |
| 280 | + "show(poly1); show(poly2)\n", |
| 281 | + "poly1_mult_poly2=mult_polys(poly1,poly2)\n", |
| 282 | + "print('The multiplication:')\n", |
| 283 | + "show(poly1_mult_poly2)" |
| 284 | + ] |
| 285 | + }, |
| 286 | + { |
| 287 | + "cell_type": "code", |
| 288 | + "execution_count": 25, |
| 289 | + "metadata": {}, |
| 290 | + "outputs": [ |
| 291 | + { |
| 292 | + "name": "stdout", |
| 293 | + "output_type": "stream", |
| 294 | + "text": [ |
| 295 | + "The multiplication:\n" |
| 296 | + ] |
| 297 | + }, |
| 298 | + { |
| 299 | + "data": { |
| 300 | + "text/latex": [ |
| 301 | + "$\\displaystyle 4.0+12.0x+36.0x^{2}+68.0x^{3}+66.0x^{4}+118.0x^{5}+70.0x^{6}+110.0x^{7}+12.0x^{11}+24.0x^{13}+30.0x^{15}$" |
| 302 | + ], |
| 303 | + "text/plain": [ |
| 304 | + "<IPython.core.display.Math object>" |
| 305 | + ] |
| 306 | + }, |
| 307 | + "metadata": {}, |
| 308 | + "output_type": "display_data" |
| 309 | + } |
| 310 | + ], |
| 311 | + "source": [ |
| 312 | + "# you can check the multiplication with numpy polynomial\n", |
| 313 | + "from numpy.polynomial.polynomial import polymul\n", |
| 314 | + "print('The multiplication:')\n", |
| 315 | + "show(polymul(poly1,poly2))" |
| 316 | + ] |
| 317 | + }, |
| 318 | + { |
| 319 | + "cell_type": "code", |
| 320 | + "execution_count": null, |
| 321 | + "metadata": {}, |
| 322 | + "outputs": [], |
| 323 | + "source": [] |
| 324 | + } |
| 325 | + ], |
| 326 | + "metadata": { |
| 327 | + "kernelspec": { |
| 328 | + "display_name": "Python 3", |
| 329 | + "language": "python", |
| 330 | + "name": "python3" |
| 331 | + }, |
| 332 | + "language_info": { |
| 333 | + "codemirror_mode": { |
| 334 | + "name": "ipython", |
| 335 | + "version": 3 |
| 336 | + }, |
| 337 | + "file_extension": ".py", |
| 338 | + "mimetype": "text/x-python", |
| 339 | + "name": "python", |
| 340 | + "nbconvert_exporter": "python", |
| 341 | + "pygments_lexer": "ipython3", |
| 342 | + "version": "3.8.3" |
| 343 | + } |
| 344 | + }, |
| 345 | + "nbformat": 4, |
| 346 | + "nbformat_minor": 4 |
| 347 | +} |
0 commit comments