You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you don't understand what the "T" stands for, you can replace it with "float" in your head and read up on "Generics" in Nim.
266
266
267
+
# Optimization
268
+
## Optimization methods
269
+
## 1 dimensional function optimization
270
+
So far only a few methods have been implemented:
271
+
272
+
### One Dimensional optimization methods
273
+
-`steepest_descent` - Standard method for local minimum finding over a 2D plane
274
+
-`conjugate_gradient` - iterative implementation of solving Ax = b
275
+
-`newtons` - Newton-Raphson implementation for 1-dimensional functions
276
+
277
+
## Usage
278
+
Using default parameters the methods need 3 things: the function f(t, y) = y'(t, y), the initial values and the timepoints that you want the solution y(t) at.
279
+
280
+
## Quick Tutorial
281
+
282
+
Say we have some differentiable function and we would like to find one of its roots
283
+
284
+
f = $\frac{1}{3}$x$^{3}$ - 2x$^{2}$ + 3x
285
+
286
+
$\frac{df}{dx}$ = x$^{2}$ - 4x + 3
287
+
288
+
289
+
290
+
If we translate this to code we get:
291
+
292
+
```nim
293
+
import math
294
+
import numericalnim
295
+
296
+
proc f(x:float64): float64 = (1.0 / 3.0) * x ^ 3 - 2 * x ^ 2 + 3 * x
297
+
proc df(x:float64): float64 = x ^ 2 - 4 * x + 3
298
+
```
299
+
300
+
now given a starting point (and optional precision) we can estimate a nearby root
301
+
We know for this function our actual root is 0
302
+
303
+
```nim
304
+
import numericalnim
305
+
var start = 0.5
306
+
result = newtons(f, df, start)
307
+
echo result
308
+
309
+
-1.210640218782444e-23
310
+
```
311
+
Pretty close!
267
312
268
313
# Utils
269
314
I have included a few handy tools in `numericalnim/utils`.
270
315
## Vector
271
316
Hurray! Yet another vector library! This was mostly done for my own use but I figured it could come in handy if one wanted to just throw something together. It's main purpose is to enable you to solve systems of ODEs using your own types (for example arbitrary precision numbers). The `Vector` type is just a glorified seq with operator overload. No vectorization (unless the compiler does it automatically) sadly. Maybe can get OpenMP to work (or you maybe you, dear reader, can fix it :wink).
272
317
The following operators and procs are supported:
318
+
273
319
-`+` : Addition between `Vector`s and floats.
274
320
-`-` : Addition between `Vector`s and floats.
275
321
-`+=`, `-=` : inplace addition and subtraction.
@@ -281,10 +327,12 @@ The following operators and procs are supported:
281
327
-`.*=`, `./=` : inplace elementwise multiplication and division between `Vector`s. (not nested `Vector`s)
282
328
-`-` : negation (-Vector).
283
329
-`dot` : Same as `*` between `Vector`s. It is recursive so it will not be a matrix dot product if nested `Vector`s are used.
284
-
-`abs` : The magnitude of the `Vector`. It is recursive so it may not be one of the usual norms.
330
+
-`abs` : The magnitude of the `Vector`. It is recursive so it may not be one of the usual norms. Equivalent to norm(`Vector`, 2)
285
331
-`[]` : Use `v[i]` to get the i:th element of the `Vector`.
286
332
-`==` : Compares two `Vector`s to see if they are equal.
287
333
-`@` : Unpacks the Vector to (nested) seqs. Works with 1, 2 and 3 dimensional Vectors.
334
+
-`^` : Element-wise exponentiation, works with natural and floating point powers, returns a new Vector object
335
+
-`norm` : General vector norm function
288
336
289
337
A `Vector` is created using the `newVector` proc and is passed an `openArray` of the elements:
290
338
```nim
@@ -340,3 +388,4 @@ If you want to use Arraymancer with NumericalNim, the basics should work but I h
340
388
- Add more integration methods (Gaussian Quadrature on it's way. Done).
341
389
- Make the existing code more efficient and robust.
342
390
- Add parallelization of some kind to speed it up. `Vector` would probably benefit from it.
0 commit comments