Code Monkey home page Code Monkey logo

basemath's Introduction

BaseMath

Basic math functions for float and double arrays in Swift for Mac or Linux, with no dependencies, via the BaseVector protocol. They are generally around 3-5x faster than standard swift loops or maps, since they use pointers, which avoids the overhead of Swift's copy-on-write checking. The following functions are provided (all also have a version suffixed with _ and a version prefixed with sum - see below of details):

  • Binary functions,: sqr, abs, min, max, pow, atan2, copysign, fdim, fmax, fmin, hypot, nextafter, add, sub, mul, div, subRev, divRev
  • Unary functions,: acos, acosh, asin, asinh, atan, atanh, cbrt, cos, cosh, erf, erfc, exp, exp2, expm1, log, log10, log1p, log2, logb, nearbyint, rint, sin, sinh, tan, tanh, tgamma

Use it with Swift Package Manager by adding to your Package.swift:

dependencies: [
    .package(url:"https://github.com/jph00/BaseMath.git", from: "1.0.0"),
]

For reasonable performance, compile with make (which is also required if you make changes to the gyb templates) or use:

swift build -Xswiftc -Ounchecked -Xcc -ffast-math -Xcc -O2 -Xcc -march=native

This library is used by SwiftyMKL, which adds more optimized versions of the functions from Intel's Performance Libraries, along with various linear algebra and statistical functions.

Math functions from Foundation (which in turn provides the functions available in math.h) are used, except for sum() (and sum${f}(), where ${f} is a function name from math.h), which are written in C, since reductions in Swift are currently not vectorized. The standard math operators are also provided, including optimized assignment versions. Functions with _ suffix are in-place. In addition the sqr function is provided. Note that abs is called fabs in C versions since that's what it's called in math.h.

Because the library uses pointers, Swift's copy-on-write and let immutability are bypassed. Use the provided copy() method to get a real copy of an Array.

To avoid surprises, you might prefer to use the provided AlignedStorage struct, which supports much of the same functionality as Array, but doesn't use copy-on-write, and aligns memory for (sometimes) better performance. In addition, UnsafeMutableBufferPointer is extended to conform to BaseVector so it also gets the same functionality.

After import BaseVector you'll find that all the standard unary and binary math functions have been added to Array for floats and doubles, along with reduction versions of each which start with sum (e.g sumabs, sumcos, etc).

See the test suite for examples of use (note that tests won't run on Mac due to objective-c xctest issues). For more information on background and implementation details, see [this article] (TBA).

basemath's People

Contributors

armcknight avatar codeofrobin avatar jph00 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.