Giter VIP home page Giter VIP logo

symengine.swift's Introduction

SymEngine.swift Build Status

A swift wrapper for SymEngine, a ⚡fast⚡ symbolic manipulation library, written in C++. This wrapper isn't complete yet, but PRs are welcome! Documentation is avaible here.

Quick Start

The easiest way to get started is via Docker. First clone the repository, then build and run the container using:

$ docker build -t symengine.swift .
$ docker run --rm -it symengine.swift /bin/bash

Once the conatiner is started, you can run the example using swift run example.

Getting Started

First import SymEngine:

import SymEngine

Then print the beautiful asci art:

print(SymEngine.asciiArt!)
//  _____           _____         _         
// |   __|_ _ _____|   __|___ ___|_|___ ___ 
// |__   | | |     |   __|   | . | |   | -_|
// |_____|_  |_|_|_|_____|_|_|_  |_|_|_|___|
//       |___|               |___|  

We can create symbols in several different ways:

// A symbol for a variable x
let x: Symbol? = Symbol(name: "x")

// A symbol for an integer.
let one: Symbol = 1
// or equivalently
let one = Symbol(1)

// A symbol for a double. Same thing as integer can be done
// to instantiate it explicitly.
let oneHalf: Symbol = 0.5

// Parse the expression
let expression: Symbol? = Symbol(parse: "3x^2 + 3*y")

However, notice that some of the symbols returned are optional as it is possible an initializer may fail. While the operations and functions in this wrapper have been designed to work with both optionals and non-optionals (returning non-optionals when possible), the compiler will yell at you until attention is paid to the optionality of the values you are working with.

Now we can do some math using these:

let quadratic = x**2 + 4*x +4
let derivative = diff(of: quadratic, withRespectTo: x)!
print(derivative)
// 4 + 2*x

Notice that the exponent is **, rather than ^. Swift doesn't let you override the operators, so we can't copt the caret.

Head over to the documentation for more information.

Installing

  1. Install SymEngine using conda:

    $ conda install symengine -c conda-forge
    
  2. The Swift Package Manager relies on pkgconfig for finding system libraries. Included in the repo are two pkgconfig files. If you installed SymEngine or anaconda (or used miniconda) to a different place, you may need to adjust them. Then, you can either add them to PKG_CONFIG_PATH, or copy the pkgconfig file into a directory already in the path.

  3. Before you can run anything, you need to make sure that the SymEngine library can be found at runtime. On macOS set the fallback dylib path:

    $ export DYLD_FALLBACK_LIBRARY_PATH=$DYLD_FALLBACK_LIBRARY_PATH:[path to libraries]
    

    On linux, look for the right way to add libraries to the system for your distro.

  4. To test that the environment is setup correctly, clone the repository and run the tests.

    $ swift test
    

Swift Package Manager

Add the following to your dependencies:

.package(url: "https://github.com/ianruh/SymEngine.swift.git", from: ...)

Check the tags for the most recent version.

Then add SymEngine as a dependency for each necessary target:

.target(
	...
    .dependencies: [
        .product(name: "SymEngine", package: "SymEngine.swift"),
        ...
    ]
)

Notes

I have only implemented wrappers for the basic functions so far (no matrices or sets yet). I intend to implement them, but only needed the basics to begin with.

symengine.swift's People

Contributors

ianruh avatar

Watchers

 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.