| 1 | |
|---|
| 2 | :- protocol(symdiffp). |
|---|
| 3 | |
|---|
| 4 | :- info([ |
|---|
| 5 | author is 'Paulo Moura', |
|---|
| 6 | version is 1.0, |
|---|
| 7 | date is 1999/12/29, |
|---|
| 8 | comment is 'Symbolic differentiation and simplification protocol.', |
|---|
| 9 | source is 'Example based on the Clocksin and Mellish Prolog book.']). |
|---|
| 10 | |
|---|
| 11 | :- public(diff/1). |
|---|
| 12 | :- mode(diff(-expression), one). |
|---|
| 13 | :- info(diff/1, [ |
|---|
| 14 | comment is 'Returns the symbolic differentiation of self.', |
|---|
| 15 | argnames is ['Expression']]). |
|---|
| 16 | |
|---|
| 17 | :- public(simplify/1). |
|---|
| 18 | :- mode(simplify(-expression), one). |
|---|
| 19 | :- info(simplify/1, [ |
|---|
| 20 | comment is 'Returns the symbolic simplification of self.', |
|---|
| 21 | argnames is ['Expression']]). |
|---|
| 22 | |
|---|
| 23 | :- end_protocol. |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | :- object(x, |
|---|
| 28 | implements(symdiffp)). |
|---|
| 29 | |
|---|
| 30 | :- info([ |
|---|
| 31 | author is 'Paulo Moura', |
|---|
| 32 | version is 1.0, |
|---|
| 33 | date is 1999/12/29, |
|---|
| 34 | comment is 'Symbolic differentiation and simplification of a variable.', |
|---|
| 35 | source is 'Example based on the Clocksin and Mellish Prolog book.']). |
|---|
| 36 | |
|---|
| 37 | diff(1). |
|---|
| 38 | |
|---|
| 39 | simplify(x). |
|---|
| 40 | |
|---|
| 41 | :- end_object. |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | :- object(_ + _, |
|---|
| 46 | implements(symdiffp)). |
|---|
| 47 | |
|---|
| 48 | :- info([ |
|---|
| 49 | author is 'Paulo Moura', |
|---|
| 50 | version is 1.0, |
|---|
| 51 | date is 1999/12/29, |
|---|
| 52 | parnames is ['Expression1', 'Expression2'], |
|---|
| 53 | comment is 'Symbolic differentiation and simplification of +/2 expressions.', |
|---|
| 54 | source is 'Example based on the Clocksin and Mellish Prolog book.']). |
|---|
| 55 | |
|---|
| 56 | diff(Diff) :- |
|---|
| 57 | this(X + Y), |
|---|
| 58 | once(diff(X, Y, Diff)). |
|---|
| 59 | |
|---|
| 60 | diff(I, J, 0) :- |
|---|
| 61 | integer(I), |
|---|
| 62 | integer(J). |
|---|
| 63 | |
|---|
| 64 | diff(X, J, DX) :- |
|---|
| 65 | integer(J), |
|---|
| 66 | X::diff(DX). |
|---|
| 67 | |
|---|
| 68 | diff(I, Y, DY) :- |
|---|
| 69 | integer(I), |
|---|
| 70 | Y::diff(DY). |
|---|
| 71 | |
|---|
| 72 | diff(X, Y, DX + DY) :- |
|---|
| 73 | X::diff(DX), |
|---|
| 74 | Y::diff(DY). |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | simplify(S) :- |
|---|
| 78 | this(X + Y), |
|---|
| 79 | once(simplify(X, Y, S)). |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | simplify(I, J, S) :- |
|---|
| 83 | integer(I), |
|---|
| 84 | integer(J), |
|---|
| 85 | S is I + J. |
|---|
| 86 | |
|---|
| 87 | simplify(X, 0, S) :- |
|---|
| 88 | X::simplify(S). |
|---|
| 89 | |
|---|
| 90 | simplify(0, Y, S) :- |
|---|
| 91 | Y::simplify(S). |
|---|
| 92 | |
|---|
| 93 | simplify(X, J, S + J) :- |
|---|
| 94 | integer(J), |
|---|
| 95 | X::simplify(S). |
|---|
| 96 | |
|---|
| 97 | simplify(I, Y, I + S) :- |
|---|
| 98 | integer(I), |
|---|
| 99 | Y::simplify(S). |
|---|
| 100 | |
|---|
| 101 | simplify(X, Y, S) :- |
|---|
| 102 | X::simplify(SX), |
|---|
| 103 | Y::simplify(SY), |
|---|
| 104 | (X + Y \= SX + SY -> |
|---|
| 105 | (SX + SY)::simplify(S) |
|---|
| 106 | ; |
|---|
| 107 | S = SX + SY). |
|---|
| 108 | |
|---|
| 109 | :- end_object. |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | :- object(_ - _, |
|---|
| 114 | implements(symdiffp)). |
|---|
| 115 | |
|---|
| 116 | :- info([ |
|---|
| 117 | author is 'Paulo Moura', |
|---|
| 118 | version is 1.0, |
|---|
| 119 | date is 1999/12/29, |
|---|
| 120 | parnames is ['Expression1', 'Expression2'], |
|---|
| 121 | comment is 'Symbolic differentiation and simplification of -/2 expressions.', |
|---|
| 122 | source is 'Example based on the Clocksin and Mellish Prolog book.']). |
|---|
| 123 | |
|---|
| 124 | diff(Diff) :- |
|---|
| 125 | this(X - Y), |
|---|
| 126 | once(diff(X, Y, Diff)). |
|---|
| 127 | |
|---|
| 128 | diff(I, J, 0) :- |
|---|
| 129 | integer(I), |
|---|
| 130 | integer(J). |
|---|
| 131 | |
|---|
| 132 | diff(X, J, DX) :- |
|---|
| 133 | integer(J), |
|---|
| 134 | X::diff(DX). |
|---|
| 135 | |
|---|
| 136 | diff(I, Y, DY) :- |
|---|
| 137 | integer(I), |
|---|
| 138 | Y::diff(DY). |
|---|
| 139 | |
|---|
| 140 | diff(X, Y, DX - DY) :- |
|---|
| 141 | X::diff(DX), |
|---|
| 142 | Y::diff(DY). |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | simplify(S) :- |
|---|
| 146 | this(X - Y), |
|---|
| 147 | once(simplify(X, Y, S)). |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | simplify(X, X, 0). |
|---|
| 151 | |
|---|
| 152 | simplify(I, J, S) :- |
|---|
| 153 | integer(I), |
|---|
| 154 | integer(J), |
|---|
| 155 | S is I - J. |
|---|
| 156 | |
|---|
| 157 | simplify(X, 0, S) :- |
|---|
| 158 | X::simplify(S). |
|---|
| 159 | |
|---|
| 160 | simplify(0, Y, S) :- |
|---|
| 161 | Y::simplify(S). |
|---|
| 162 | |
|---|
| 163 | simplify(X, J, S - J) :- |
|---|
| 164 | integer(J), |
|---|
| 165 | X::simplify(S). |
|---|
| 166 | |
|---|
| 167 | simplify(I, Y, I - S) :- |
|---|
| 168 | integer(I), |
|---|
| 169 | Y::simplify(S). |
|---|
| 170 | |
|---|
| 171 | simplify(X, Y, S) :- |
|---|
| 172 | X::simplify(SX), |
|---|
| 173 | Y::simplify(SY), |
|---|
| 174 | (X - Y \= SX - SY -> |
|---|
| 175 | (SX - SY)::simplify(S) |
|---|
| 176 | ; |
|---|
| 177 | S = SX - SY). |
|---|
| 178 | |
|---|
| 179 | :- end_object. |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | :- object(_ * _, |
|---|
| 184 | implements(symdiffp)). |
|---|
| 185 | |
|---|
| 186 | :- info([ |
|---|
| 187 | author is 'Paulo Moura', |
|---|
| 188 | version is 1.0, |
|---|
| 189 | date is 1999/12/29, |
|---|
| 190 | parnames is ['Expression1', 'Expression2'], |
|---|
| 191 | comment is 'Symbolic differentiation and simplification of */2 expressions.', |
|---|
| 192 | source is 'Example based on the Clocksin and Mellish Prolog book.']). |
|---|
| 193 | |
|---|
| 194 | diff(Diff) :- |
|---|
| 195 | this(X * Y), |
|---|
| 196 | once(diff(X, Y, Diff)). |
|---|
| 197 | |
|---|
| 198 | diff(I, J, 0) :- |
|---|
| 199 | integer(I), |
|---|
| 200 | integer(J). |
|---|
| 201 | |
|---|
| 202 | diff(0, _, 0). |
|---|
| 203 | |
|---|
| 204 | diff(_, 0, 0). |
|---|
| 205 | |
|---|
| 206 | diff(X, J, J * DX) :- |
|---|
| 207 | integer(J), |
|---|
| 208 | X::diff(DX). |
|---|
| 209 | |
|---|
| 210 | diff(I, Y, I * DY) :- |
|---|
| 211 | integer(I), |
|---|
| 212 | Y::diff(DY). |
|---|
| 213 | |
|---|
| 214 | diff(X, Y, X * DY + DX * Y) :- |
|---|
| 215 | X::diff(DX), |
|---|
| 216 | Y::diff(DY). |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | simplify(S) :- |
|---|
| 220 | this(X * Y), |
|---|
| 221 | once(simplify(X, Y, S)). |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | simplify(I, J, S) :- |
|---|
| 225 | integer(I), |
|---|
| 226 | integer(J), |
|---|
| 227 | S is I * J. |
|---|
| 228 | |
|---|
| 229 | simplify(0, _, 0). |
|---|
| 230 | |
|---|
| 231 | simplify(_, 0, 0). |
|---|
| 232 | |
|---|
| 233 | simplify(1, Y, SY) :- |
|---|
| 234 | Y::simplify(SY). |
|---|
| 235 | |
|---|
| 236 | simplify(X, 1, SX) :- |
|---|
| 237 | X::simplify(SX). |
|---|
| 238 | |
|---|
| 239 | simplify(I, Y, I * SY) :- |
|---|
| 240 | integer(I), |
|---|
| 241 | Y::simplify(SY). |
|---|
| 242 | |
|---|
| 243 | simplify(X, J, J * SX) :- |
|---|
| 244 | integer(J), |
|---|
| 245 | X::simplify(SX). |
|---|
| 246 | |
|---|
| 247 | simplify(X, Y, SX * SY) :- |
|---|
| 248 | X::simplify(SX), |
|---|
| 249 | Y::simplify(SY). |
|---|
| 250 | |
|---|
| 251 | :- end_object. |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | :- object(_ ** _, |
|---|
| 256 | implements(symdiffp)). |
|---|
| 257 | |
|---|
| 258 | :- info([ |
|---|
| 259 | author is 'Paulo Moura', |
|---|
| 260 | version is 1.0, |
|---|
| 261 | date is 1999/12/29, |
|---|
| 262 | parnames is ['Expression', 'Power'], |
|---|
| 263 | comment is 'Symbolic differentiation and simplification of **/2 expressions.', |
|---|
| 264 | source is 'Example based on the Clocksin and Mellish Prolog book.']). |
|---|
| 265 | |
|---|
| 266 | diff(Diff) :- |
|---|
| 267 | this(X ** Y), |
|---|
| 268 | once(diff(X, Y, Diff)). |
|---|
| 269 | |
|---|
| 270 | diff(X, Y, Y * X ** Y2 * DX) :- |
|---|
| 271 | integer(Y), |
|---|
| 272 | Y2 is Y - 1, |
|---|
| 273 | X::diff(DX). |
|---|
| 274 | |
|---|
| 275 | diff(X, Y, Y * X ** Y2 * DX) :- |
|---|
| 276 | Y2 = Y - 1, |
|---|
| 277 | X::diff(DX). |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | simplify(S) :- |
|---|
| 281 | this(X ** Y), |
|---|
| 282 | once(simplify(X, Y, S)). |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | simplify(_, 0, 1). |
|---|
| 286 | |
|---|
| 287 | simplify(X, 1, X). |
|---|
| 288 | |
|---|
| 289 | simplify(X, Y, S ** Y) :- |
|---|
| 290 | integer(Y), |
|---|
| 291 | X::simplify(S). |
|---|
| 292 | |
|---|
| 293 | simplify(X, Y, SX ** SY) :- |
|---|
| 294 | X::simplify(SX), |
|---|
| 295 | Y::simplify(SY). |
|---|
| 296 | |
|---|
| 297 | :- end_object. |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | :- object(log(_), |
|---|
| 302 | implements(symdiffp)). |
|---|
| 303 | |
|---|
| 304 | :- info([ |
|---|
| 305 | author is 'Paulo Moura', |
|---|
| 306 | version is 1.0, |
|---|
| 307 | date is 1999/12/29, |
|---|
| 308 | parnames is ['Expression'], |
|---|
| 309 | comment is 'Symbolic differentiation and simplification of log/1 expressions.', |
|---|
| 310 | source is 'Example based on the Clocksin and Mellish Prolog book.']). |
|---|
| 311 | |
|---|
| 312 | diff(Diff) :- |
|---|
| 313 | this(log(X)), |
|---|
| 314 | once(diff(X, Diff)). |
|---|
| 315 | |
|---|
| 316 | diff(I, 0) :- |
|---|
| 317 | integer(I). |
|---|
| 318 | |
|---|
| 319 | diff(X, DX * X ** -1) :- |
|---|
| 320 | X::diff(DX). |
|---|
| 321 | |
|---|
| 322 | simplify(S) :- |
|---|
| 323 | this(log(X)), |
|---|
| 324 | once(simplify(X, S)). |
|---|
| 325 | |
|---|
| 326 | simplify(1, 0). |
|---|
| 327 | |
|---|
| 328 | simplify(I, Log) :- |
|---|
| 329 | integer(I), |
|---|
| 330 | Log is log(I). |
|---|
| 331 | |
|---|
| 332 | simplify(X, X). |
|---|
| 333 | |
|---|
| 334 | :- end_object. |
|---|