summary refs log tree commit diff stats
path: root/lib/pure/poly.nim
diff options
context:
space:
mode:
authorRobert Persson <r.k.persson@gmail.com>2013-07-07 01:50:10 +0200
committerRobert Persson <r.k.persson@gmail.com>2013-07-07 01:50:10 +0200
commit5a1810081606cb687ec15a5e2efa69cfb74fd460 (patch)
treef7241c1d6be83e678c05b748cb491def28f47f49 /lib/pure/poly.nim
parent0d2f8d5079fc5f120ba376f57efe3981d83d3270 (diff)
downloadNim-5a1810081606cb687ec15a5e2efa69cfb74fd460.tar.gz
Fixed some minor stuff in module poly
Removed the stupid initPolyFromDegree which only served ro re-allocate
results. Also fixed some minor stuff with nil return values in roots.
Diffstat (limited to 'lib/pure/poly.nim')
-rw-r--r--lib/pure/poly.nim24
1 files changed, 9 insertions, 15 deletions
diff --git a/lib/pure/poly.nim b/lib/pure/poly.nim
index 609e58bdc..45e528604 100644
--- a/lib/pure/poly.nim
+++ b/lib/pure/poly.nim
@@ -16,13 +16,6 @@ type
         cofs:seq[float]
 
   
-proc initPolyFromDegree(n:int):TPoly=
-  ## internal usage only
-  ## caller must initialize coefficients of poly
-  ## and possibly  `clean` away zero exponents
-  var numcof=n+1   #num. coefficients is one more than degree
-  result.cofs=newSeq[float](numcof)
-  
 proc degree*(p:TPoly):int=
   ## Returns the degree of the polynomial,
   ## that is the number of coefficients-1
@@ -130,7 +123,7 @@ proc diff*(p:TPoly,x:float):float=
 proc integral*(p:TPoly):TPoly=
   ## Returns a new polynomial which is the indefinite
   ## integral of `p`. The constant term is set to 0.0
-  result=initPolyFromDegree(p.degree+1)
+  newSeq(result.cofs,p.cofs.len+1)
   result.cofs[0]=0.0  #constant arbitrary term, use 0.0
   for i in 1..high(result.cofs):
     result.cofs[i]=p.cofs[i-1]/float(i)
@@ -227,7 +220,7 @@ proc `*` *(p1:TPoly,p2:TPoly):TPoly=
 
 proc `*` *(p:TPoly,f:float):TPoly=
   ## Multiplies the polynomial `p` with a real number
-  result=initPolyFromDegree(p.degree)
+  newSeq(result.cofs,p.cofs.len)
   for i in 0..high(p.cofs):
     result[i]=p.cofs[i]*f
   result.clean
@@ -254,7 +247,7 @@ proc `-` *(p1:TPoly,p2:TPoly):TPoly=
     
 proc `/`*(p:TPoly,f:float):TPoly=
   ## Divides polynomial `p` with a real number `f`
-  result=initPolyFromDegree(p.degree)
+  newSeq(result.cofs,p.cofs.len)
   for i in 0..high(p.cofs):
     result[i]=p.cofs[i]/f
   result.clean
@@ -351,13 +344,12 @@ proc roots*(p:TPoly,tol=1.0e-9,zerotol=1.0e-6,mergetol=1.0e-12,maxiter=1000):seq
   ## `maxiter` can be used to limit the number of iterations for each root.
   ## Returns a (possibly empty) sorted sequence with the solutions.
   var deg=p.degree
-  result= @[]
-  if deg<=0:
-    return nil
-  elif p.degree==1:
+  if deg<=0: #constant only => no roots
+    return @[]
+  elif p.degree==1: #linear
     var linrt= -p.cofs[0]/p.cofs[1]
     if linrt==inf or linrt==neginf:
-      return nil #constant only => no roots
+      return @[] #constant only => no roots
     return @[linrt]
   elif p.degree==2:
     return solveQuadric(p.cofs[2],p.cofs[1],p.cofs[0],zerotol)
@@ -366,8 +358,10 @@ proc roots*(p:TPoly,tol=1.0e-9,zerotol=1.0e-6,mergetol=1.0e-12,maxiter=1000):seq
     # derivative and do a numerical search for root between each min/max
     var range=p.getRangeForRoots()
     var minmax=p.derivative.roots(tol,zerotol,mergetol)
+    result= @[]
     if minmax!=nil: #ie. we have minimas/maximas in this function
       for x in minmax.items:
         addRoot(p,result,range.xmin,x,tol,zerotol,mergetol,maxiter)
         range.xmin=x
     addRoot(p,result,range.xmin,range.xmax,tol,zerotol,mergetol,maxiter)
+
69 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295