#!/usr/bin/env python # -*- coding: utf-8 -*- # *************************************************************************** # * Copyright (C) 2015, Paul Lutus * # * * # * This program is free software; you can redistribute it and/or modify * # * it under the terms of the GNU General Public License as published by * # * the Free Software Foundation; either version 2 of the License, or * # * (at your option) any later version. * # * * # * This program is distributed in the hope that it will be useful, * # * but WITHOUT ANY WARRANTY; without even the implied warranty of * # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * # * GNU General Public License for more details. * # * * # * You should have received a copy of the GNU General Public License * # * along with this program; if not, write to the * # * Free Software Foundation, Inc., * # * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * # *************************************************************************** # Created on Nov 12, 2015 11:43:51 AM from sympy import * import re,sys,os # in LaTeX rendering, this separates # certain two-letter variables for readability def padspace(s): return re.sub(r'(fv|ir|pv|pb|pmt|np)',r'\1\,',s) var('pv fv np pmt ir pb') # the base equation from which all others are derived bfa = ((pb*ir+1)*pmt-(ir+1)**np*((pb*ir*pmt)+pmt+ir*pv))/ir bfe = fv - bfa print('Base financial equation:\n') eq = Equality(fv,bfa) pprint(eq) print('In LaTeX form:\n') pprint(padspace(latex(eq))) for lat in (True,False): print('\n*** Result for %s' % ('viewing','Webpage')[lat]) results = [] # for payment at end|beginning for p in (0,1): print('\n*** Payment at %s\n' % ('End','Beginning')[p]) # solve for each variable for v in (pv,fv,np,pmt): q = solve(bfe,v)[0].subs({pb:p}).simplify() results.append(q) # create insertable LaTeX block for Web page if(lat): print(r'$$') print(str(v) + ' = ' + padspace(latex(q))) print(r'$$') else: pprint(Equality(v,q)) # declare functional forms of the equations fpv = lambdify((fv,np,pmt,ir),results[0]) ffv = lambdify((pv,np,pmt,ir),results[1]) fnp = lambdify((pv,fv,pmt,ir),results[2]) fpmt = lambdify((pv,fv,np,ir),results[3]) ppmt = fpmt(0,100000,200,0.01) print('\nExample problem:\n') # test the functions for consistency # using an example problem print(ffv(0,200,ppmt,0.01)) print(fpv(100000,200,ppmt,0.01)) print(fpmt(0,100000,200,0.01)) print(fnp(0,100000,ppmt,0.01)) print('\nRound Robin Calculation:\n') y = ffv(0,200,ppmt,0.01) print(y) x = fpv(y,200,ppmt,0.01) print(x)