Commit Graph

37 Commits

Author SHA1 Message Date
Angel Ezquerra
d8e1504ed1 Add Complex version of almostEqual function (#23549)
This adds a version of `almostEqual` (which was already available for
floats) thata works with `Complex[SomeFloat]`.

Proof that this is needed is that the first thing that the complex.nim
runnable examples block did before this commit was define (an
incomplete) `almostEqual` function that worked with complex values.
2024-05-08 14:53:01 -06:00
Angel Ezquerra
857b35c602 Additional speed ups of complex.pow (#23264)
This PR speeds up complex.pow when both base and exponent are real; when
only the exponent is real; and when the base is Euler's number. These
are some pretty common cases which appear in many formulas. The speed
ups are pretty significant. According to my measurements (using the
timeit library) when both base and exponent are real the speedup is ~2x;
when only the exponent is real it is ~1.5x and when the base is Euler's
number it is ~2x.

There is no measurable difference when using other exponents which makes
sense since I refactored the code a little to reduce the total number of
branches that are needed to get to the final "fallback" branch, and
those branches have less comparisons. Anecdotally the fallback case
feels slightly faster, but the improvement is so small that I cannot
claim an improvement. If it is there it is perhaps in the order of 3 or
4%.

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2024-01-29 09:05:05 +01:00
Angel Ezquerra
83f2708909 Speed up complex.pow when the exponent is 2.0 or 0.5 (#23237)
This PR speeds up the calculation of the power of a complex number when
the exponent is 2.0 or 0.5 (i.e the square and the square root of a
complex number). These are probably two of (if not) the most common
exponents. The speed up that is achieved according to my measurements
(using the timeit library) when the exponent is set to 2.0 or 0.5 is >
x7, while there is no measurable difference when using other exponents.

For the record, this is the function I used to mesure the performance:

```nim
import std/complex
import timeit

proc calculcatePows(v: seq[Complex], factor: Complex): seq[Complex] {.noinit, discardable.} =
  result = newSeq[Complex](v.len)
  for n in 0 ..< v.len:
    result[n] = pow(v[n], factor)

let v: seq[Complex64] = collect:
  for n in 0 ..< 1000:
    complex(float(n))

echo timeGo(calculcatePows(v, complex(1.5)))
echo timeGo(calculcatePows(v, complex(0.5)))
echo timeGo(calculcatePows(v, complex(2.0)))
```

Which with the original code got:

> [177μs 857.03ns] ± [1μs 234.85ns] per loop (mean ± std. dev. of 7
runs, 1000 loops each)
> [128μs 217.92ns] ± [1μs 630.93ns] per loop (mean ± std. dev. of 7
runs, 1000 loops each)
> [136μs 220.16ns] ± [3μs 475.56ns] per loop (mean ± std. dev. of 7
runs, 1000 loops each)

While with the improved code got:

> [176μs 884.30ns] ± [1μs 307.30ns] per loop (mean ± std. dev. of 7
runs, 1000 loops each)
> [23μs 160.79ns] ± [340.18ns] per loop (mean ± std. dev. of 7 runs,
10000 loops each)
> [19μs 93.29ns] ± [1μs 128.92ns] per loop (mean ± std. dev. of 7 runs,
10000 loops each)

That is, the new optimized path is 5.6 (23 vs 128 us per loop) to 7.16
times faster (19 vs 136 us per loop), while the non-optimized path takes
the same time as the original code.
2024-01-20 06:39:49 +01:00
Angel Ezquerra
fbfd4decca 'j' format specifier docs (#22928)
This is a small improvement on top of PR #22924, which documents the new
'j' format specifier for Complex numbers. In addition to that it moves
the handling of the j specifier into the function that actually
implements it (formatValueAsComplexNumber), which seems a little
cleaner.

---------

Co-authored-by: Angel Ezquerra <angel_ezquerra@keysight.com>
Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
2023-11-17 10:22:57 +01:00
Angel Ezquerra
52784f32bb Add strformat support for Complex numbers (#22924)
Before this change strformat used the generic formatValue function for
Complex numbers. This meant that it was not possible to control the
format of the real and imaginary components of the complex numbers.

With this change this now works:
```nim
import std/[complex, strformat]
let c = complex(1.05000001, -2.000003)
echo &"{c:g}"
# You now get: (1.05, -2)
# while before you'd get a ValueError exception (invalid type in format string for string, expected 's', but got g)
```

The only small drawback of this change is that I had to import complex
from strformat. I hope that is not a problem.

---------

Co-authored-by: Angel Ezquerra <angel_ezquerra@keysight.com>
2023-11-10 05:29:55 +01:00
ringabout
4d11d0619d complete std prefixes for stdlib (#22887)
follow up https://github.com/nim-lang/Nim/pull/22851
follow up https://github.com/nim-lang/Nim/pull/22873
2023-10-30 17:03:04 +01:00
Dan Rose
c2cdc752c8 Simpler complex division implementation (#20088) 2022-09-01 17:44:07 +02:00
Dan Rose
cc81866da1 Implement complex sgn (#20087)
Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
2022-08-28 22:17:18 -04:00
Andreas Rumpf
a9b62de895 CIs: attempt to use csources_v1 (#16282)
* CIs: attempt to use csources_v1
* also updated the BSDs
* also updated azure pipelines
* std modules should not itself use the 'std/' import dir...
* compiler has to be careful with std/ for v1 booting
2021-04-21 07:41:33 +02:00
konsumlamm
0c4bd65e8d Improve documentation for complex (#16588)
* Improve documentation for complex

Add missing doc comments

* Add runnableExample

Add links for principal values
Optimize `-`
Change var to let

* Use std prefix for imports
2021-01-05 17:50:15 +01:00
ee7
140ebe6019 complex.nim: Use func everywhere (#16294) 2020-12-09 10:57:12 +01:00
flywind
dd57d46f2f complex minor improvement (#16086) 2020-11-21 12:20:33 -08:00
Andreas Rumpf
e58c2d261c [backport] fix #12528, fix #12525: incorrect generic type resolution for default values (#12538) 2019-10-28 16:56:38 +01:00
narimiran
6c994b2498 [backport] run nimpretty on numbers stuff 2019-09-30 13:58:05 +02:00
Palash Nigam
f28a47ea7b fixes #11834 (#12000) 2019-08-23 07:42:33 +02:00
Andreas Rumpf
8317de7648 complex.nim: reformating [other] 2019-06-09 22:22:50 +02:00
Arne Döring
c2c05f5e72 fix #9639 (#9640) 2018-11-07 10:58:48 +01:00
Arne Döring
cc5b8c6ad2 Generic Complex type (#9590)
* remove `**`
* const `im` can now be used with Complex64
* converters from float|int to Complex are replaced by procs
* converters between various Complex types must stay to allow usage
of `im` with Complex64
* limit types for `+`, `-`, `/`, and `*` between Complex and float
* add `pow` for Complex and a number
* complex type changes
* unpublish approximation function
2018-11-05 20:27:46 +01:00
Koki Fushimi
3acedd4cd9 Add imaginary unit. (#7922) 2018-06-01 11:20:28 -04:00
Araq
bbb0fd4eb7 remove deprecated stuff from the stdlib; introduce better deprecation warnings 2018-05-05 21:45:07 +02:00
Adam Strzelecki
43bddf62dd lib: Trim .nim files trailing whitespace
via OSX: find . -name '*.nim' -exec sed -i '' -E 's/[[:space:]]+$//' {} +
2015-09-04 23:03:56 +02:00
Charles Blake
840f80e45c Fix buggy rect(), doc comment, and unit test. 2015-02-28 19:21:52 -05:00
Jonathan Edwards
b1e0d2058b Addition of some complex hyperbolic functions 2015-02-28 16:12:36 -05:00
def
00e82c2fc6 Extend complex to convert to/from polar coordinates 2015-02-16 20:44:23 +01:00
gmpreussner
defbd10c19 Added complex conjugate 2015-01-24 22:57:19 -05:00
Araq
338598d9d0 fixes #495 2014-12-26 02:09:15 +01:00
Araq
bae9a0ceac more modules updated 2014-08-28 01:30:12 +02:00
Araq
11b6958755 big rename 2014-08-27 23:42:51 +02:00
def
b1a494e8b9 Add missing complex arithmetic procs 2014-07-14 14:44:58 +02:00
Grzegorz Adam Hankiewicz
72a3e21f28 Removes executable bit for text files. 2013-03-16 23:53:07 +01:00
Araq
37741f28fd additions to complex module 2011-01-07 00:32:15 +01:00
Andreas Rumpf
b2ad7b30dc bugfix: complex.nim compiles 2010-04-04 18:43:57 +02:00
Andreas Rumpf
c5d8a5c1da Bugfix: empty echo statement 2010-03-20 01:07:34 +01:00
Andreas Rumpf
40ea1d0330 fixed pango/pangoutils new wrappers 2010-02-26 01:26:16 +01:00
rumpf_a@web.de
40a5d6c3b9 continued work on html/xmlparser 2010-02-14 00:29:35 +01:00
Andreas Rumpf
66a7e3d37c added tools and web dirs 2009-09-15 23:22:22 +02:00
Andreas Rumpf
4d4b3b1c04 version0.7.10 2009-06-08 08:06:25 +02:00