Skip to content

bl0ckeduser/symdiff

Repository files navigation

==============================================================================
	This is a symbolic differentiation program
	
   It features an infix REPL, a tree pattern-matcher/substitutor,
   and some preloaded derivative rules (the fanciest of which is
   probably the chain rule)

   This is a toy/self-education project by "blockeduser", started May 2013.
   And being maintained through the future, because a project is never over,
   only abandoned. Users are extremely talented at coming with new desired
   features. According to my economy course, the world has "infinite needs".
   Well there you go right there. Unemployment is evil. You have to fix
   bugs every day till you kick the bucket if you want things to be golden.
   There is no royal route.

   It is very far from perfect and could be improved upon.
   It is coded in hopefully-not-too-bad C.
   As of January 2016, doing a bunch of complicated examples then quitting
   leaks no memory according to valgrind.

   Much of it is strongly based on examples given in the SICP 
   ("Structure and Interpretation of Computer Programs") book and videos
==============================================================================

Program use
-----------

The program is a REPL. Equations are taken as rules and added to the list 
of known rules. Expressions are reduced using the set of rules known so
far. When an expression typed in cannot be reduced, nothing is printed back.

All the lines of all the files in the directory "rules" will be loaded 
as rules at startup, automatically. Some basic derivative, trigonometry, and
algebraic simplification rules are already included. Derivatives are
taken through the syntax 'diff(expression, variable)'.

Many algebraic simplifications are also "hard-coded" into the program.

(The directory "pres-rules" contains rules for presentation of results).

The file "SYNTAX" describes the syntax used for rules and expressions.

To take derivatives, the syntax is diff(expression, variable).

		***************************************

Here are some quick examples:

]=> diff(log((1+x)*(1+x^2)^2*(1+x^3)^3), x)
(1 + (1 + x) * (9 * x ^ 2) / (1 + x ^ 3) 
	+ (1 + x) * (4 * x) / (1 + x ^ 2)) / (1 + x)

]=> diff(sin(x * cos(x + log(x))), x)
cos(x * cos(x + log(x))) * (-1 * x * sin(x + log(x)) 
	* (1 + 1 / x) + cos(x + log(x)))

]=> diff(e^u + e^(cos(u) * sin(u)), u)
e ^ u + e ^ (cos(u) * sin(u)) * (cos(u) ^ 2 + -sin(u) ^ 2)

]=> diff(z^log(sqrt(z)) * z / cos(z), z)
(z ^ (log(z) / 2) * (1 + log(z))) / cos(z) + z ^ (log(z) / 2) 
	* cos(z) ^ -2 * z * sin(z)

]=> diff(z^z^z, z)
z ^ (z ^ z) * (log(z) * z ^ z * (1 + log(z)) + z ^ (z - 1))

]=> diff(z^x^y, x)
z ^ (x ^ y) * x ^ (y - 1) * log(z) * y

A quick example for N'th order derivatives, which were added January 2016:

]=> ndiff(sin(x), x, 100)
sin(x)

It can find that in about a second on 1.50 GHz Celeron cheap laptop

Yes it's that fast ;-)


Acknowledgements
----------------

This program is partially based on some things:

	1) The SICP video about pattern matching
	   ("4A: Pattern Matching and Rule-based Substitution",
	    available on MIT's website via an excessively long URL)

	2) The SICP text example about symbolic differentiation
	   http://mitpress.mit.edu/sicp/full-text/sicp/book/node39.html

	   The file 'rules/basic-deriv-rules' contains exactly the 
	   derivative rules presented in that example, rewritten in 
	   this program's syntax. Credit is given in the rule-file's
	   comments, of course.

	3) My wannabe parser/compiler project from december 2012
		(itself based on my wannabe-regexes from
		 august 2012, which handle the lexing)
	   This makes the infix REPL interface possible.
	   The bulk of some of the starting boilerplate code in 
           this program was from that project, until it was modified.
           There you go, ``CODE REUSE'' !!! Didn't need no OOP gimmicks!!!

	4) "Numerical computing with IEEE floating point arithmetic"
	   by Michael L. Overton, SIAM 2001, pages 73-75: has provided
	   a technique to make the float testing module marginally better.

Bugs
----

- Matcher can't deal with certain patterns, see file 'SYNTAX'.
  This isn't a user-facing bug, really, especially not the locked-down GUI

Fixed bugs
----------

- Memory leakage over time; nothing is ever really free()d. (Might fix one day)
  UPDATE: now fixed via a cheapo GC.
  Compile with the flag LEAK_STRESS_TEST and the program will run in a loop 
  constantly doing the derivative 'diff(z^z^z, z)'. Memory usage stays
  stable over time.
- Rather inefficient. On a 400 MHz system, the command 'diff(x^2, x)' 
  took 1.667 second to complete, and the command
  'diff(z^log(sqrt(z)) * z / cos(z), z)' took 38.728 seconds.
  UPDATE: since commit 147cdea87850438bd2006e54389a0c594d19cf99,
  these times have shrunk to 0.135s and 0.737s respectively :)
- Parser crash (segfault) on certain instances of illegal syntax 
  (I should really fix this)
  actually i think this has been fixed, kudos to knipil, some bluemoons ago
  refer commit b56d81008ce068935029aa1600e5c28e1d368856

Notes
-----

Unfortunately, there are several other projects by other people with
the same name, for example one which is a GPL'd a symbolic differentiator
dating to 2003; one which is a commercial symbolic differentiator;
and one which is a serious research project in a different field.

I'm not trying to steal anybody's trademarks. I'm just trying to write
code for fun and learning.

Also, I've made a closed-source-freeware step-by-step derivatives GUI
desktop application based on this code.
 
 http://www.softpedia.com/get/Science-CAD/Step-by-step-Derivative-Calculator.shtml

There's now also a Windows 8/10 "app" version of the step-by-step GUI
available:

 http://apps.microsoft.com/windows/en-us/app/step-by-step-derivative/e161f170-bb36-498e-9200-03217e4ec572

Developing this app was somewhat of an interesting challenge.

The app has been used by users all around the world and licensed by Microsoft
to education OEM partners. There have been lots of satisfied customers! :D
It is a convenient distribution channel let's say that to stay neutral

I am not an advertiser I am a programmer

About

symbolic differentiator, partly based on SICP examples. does basic algebraic simplifications on result.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages