Search This Blog

Showing posts with label software. Show all posts
Showing posts with label software. Show all posts

Monday, April 12, 2010

Simulation: Complex Math Parser

Introduction
In my project to build a SPICE program, I ran across a problem; I need to be able to perform complex math from strings.  This has already been done a thousand times before but finding a free, good parser isn't easy.  So I decided to do this myself.  This article will explain briefly the process I'm using to parse a complex math string.

Basic Process
  1. First check the string for incorrect characters.  This can be done while performing step 2
  2. Define every character in the string as a type (ie. 0=operator, 1=real number, 2=imaginary number, 3=left parenthesis, 4=right parenthesis).  Define this into a separate string.  The reason you do this is to take in different number types (real, imaginary, scientific notation, etc.)
  3. Next separate the string into tokens to be evaluated separately.  The most open parenthesis should be it's own token. (ie. str = "2*2j+(20+2j)^12" will turn into tokens "@1*@2+@3^@4") This is only an example.  I doubt i'll have a string like this.
  4. Evaluate each token by recursion through the same function until it outputs a complex number. (a custom class)
  5. With a complex number for the tokens you can then evaluate in precedent order of functions.
  6. Output complex number 
That's the quick and easy of it.  I have only completed steps 1 and 2 so far.  The rest will hopefully be completed in the week.  Hope this is helpful...

Saturday, April 10, 2010

Simulation: My SPICE software

This blog is just to update and tell you guys that I have started a SPICE software package.  Of course it'll be simple and won't do things PSPICE or B2SPICE can do but it's a great way to learn the process easier.  I have already implemented the portion of the program that can simulate current sources and resistors.  You can have as many nodes as you desire and it seems to work perfect (confirmed with B2SPICE).  So I think that's good...

As for the software structure.  I had to create a simple Complex Class and Matrix Class.  The most difficult part was creating the inversion routine for the matrix.  I didn't realize how complicated this was until I started to write it.  I used the slowest and most real, the reduced diagonal method.  I basically used linear algebra directly without any optimizations.

Anyways, this project will take a while.  Once I can develop a simple engine, I'll put a GUI around it and make it better in a number of ways (if possible).  Any suggestions, let me know

-----------------------
Justin Coulston
justin.coulston@gmail.com