SymPy.jl: Julia interface to SymPy via PyCall

SymPy Package to bring Python's SymPy functionality into Julia via PyCall

SymPy (http://sympy.org/) is a Python library for symbolic mathematics.

With the excellent PyCall package of julia, one has access to the many features of the SymPy library from within a Julia session.

This SymPy package provides a light interface for the features of the SymPy library that makes working with SymPy objects a bit easier.

The documentation inludes an introduction document and a version of the SymPy tutorial translated from the Python syntax into Julia.

Installation

To use this package, both Python and its SymPy library must be installed on your system. If PyCall is installed using Conda (which is the default if no system python is found), then the underlying SymPy library will be installed via Conda when the package is first loaded. Otherwise, installing both Python and the SymPy library (which also requires mpmath) can be done by other means. In this case, the Anaconda distribution is suggested, as it provides a single installation of Python that includes SymPy and many other scientific libraries that can be profitably accessed within Julia via PyCall. (Otherwise, install Python then download the SymPy library from https://github.com/sympy/sympy/releases and install.)

To upgrade the underlying sympy library, which has new releases at a rate similar to Julia, when installed with Conda, the following commands are available:

using Pkg
Pkg.add("Conda") #  if needed
using Conda
Conda.update()

The PyCall interface to SymPy

The only point to this package is that using PyCall to access SymPy is somewhat cumbersome. The following is how one would define a symbolic value x, take its sine, then evaluate the symboic expression for x equal pi, say:

using PyCall
sympy = pyimport("sympy")  #
x = sympy.Symbol("x")      # PyObject x
y = sympy.sin(x)           # PyObject sin(x)
z = y.subs(x, sympy.pi)    # PyObject 0
convert(Float64, z)        # 0.0

The sympy object imported on the second line provides the access to much of SymPy's functionality, allowing access to functions (sympy.sin), properties, modules (sympy), and classes (sympy.Symbol, sympy.Pi). The Symbol and sin operations are found within the imported sympy module and, as seen, are referenced with Python's dot call syntax, as implemented in PyCall through a specialized getproperty method.

SymPy's functionality is also found through methods bound to an object of a certain class. The subs method of the y object is an example. Such methods are also accessed with Python's dot-call syntax. The call above substitutes a value of sympy.pi for the symbolic variable x. This leaves the object as a PyObject storing a number which can be brought back into julia through conversion, in this case through an explicit convert call.

Alternatively, PyCall now has a * method, so the above could also be done with:

x = sympy.Symbol("x")
y = sympy.pi * x
z = sympy.sin(y)
convert(Float64, z.subs(x, 1))

With the SymPy package this gets replaced by a more julian syntax:

using SymPy
x = symbols("x")               # or  @syms x
y = sin(pi*x)
y(1)                           # Does y.subs(x, 1). Use y(x=>1) to be specific as to which symbol to substitute

The object x we create is of type Sym, a simple proxy for the underlying PyObject. The package overloads the familiar math functions so that working with symbolic expressions can use natural julia idioms. The final result here is a symbolic value of 0, which prints as 0 and not PyObject 0. To convert it into a numeric value within Julia, the N function may be used, which acts like the float conversion, only there is an attempt to preserve the variable type.

(There is a subtlety, the value of pi here (an Irrational in Julia) is converted to the symbolic PI, but in general won't be if the math constant is coerced to a floating point value before it encounters a symbolic object. It is better to just use the symbolic value PI, an alias for sympy.pi used above.)


SymPy has a mix of function calls (as in sin(x)) and method calls (as in y.subs(x,1)). The function calls are from objects in the base sympy module. When the SymPy package is loaded, in addition to specialized methods for many generic Julia functions, such as sin, a priviledged set of the function calls in sympy are imported as generic functions narrowed on their first argument being a symbolic object, as constructed by @syms, Sym, or symbols. (Calling import_from(sympy) will import all the function calls.)

The basic usage follows these points:

generic methods from Julia and imported functions in the sympy namespace are called through fn(object)

SymPy methods are called through Python's dot-call syntax: object.fn(...)

Contructors, like sympy.Symbol, and other non-function calls from sympy are qualified with sympy.Constructor(...). Such qualified calls are also useful when the first argument is not symbolic.

So, these three calls are different,

sin(1), sin(Sym(1)), sympy.sin(1)

The first involves no symbolic values. The second and third are related and return a symbolic value for sin(1). The second dispatches on the symbolic argument Sym(1), the third has no dispatch, but refers to a SymPy function from the sympy object. Its argument, 1, is converted by PyCall into a Python object for the function to process.

In the initial example, slightly rewritten, we could have issued:

x = symbols("x")
y = sin(pi*x)
y.subs(x, 1)

The first line calls a provided alias for sympy.symbols which is defined to allow a string (or a symbol) as an argument. The second, dispatches to sympy.sin, as pi*x is symbolic-- x is, and multiplication promotes to a symbolic value. The third line uses the dot-call syntax of PyCall to call the subs method of the symbolic y object.

Not illustrated above, but classes and other objects from SymPy are not brought in by default, and can be accessed using qualification, as in sympy.Function (used, as is @syms, to define symbolic functions).

Download Details: 

Author: JuliaPy
Source Code: https://github.com/JuliaPy/SymPy.jl 
License: MIT license

#julia #python #sympy 

What is GEEK

Buddha Community

SymPy.jl: Julia interface to SymPy via PyCall

SymPy.jl: Julia interface to SymPy via PyCall

SymPy Package to bring Python's SymPy functionality into Julia via PyCall

SymPy (http://sympy.org/) is a Python library for symbolic mathematics.

With the excellent PyCall package of julia, one has access to the many features of the SymPy library from within a Julia session.

This SymPy package provides a light interface for the features of the SymPy library that makes working with SymPy objects a bit easier.

The documentation inludes an introduction document and a version of the SymPy tutorial translated from the Python syntax into Julia.

Installation

To use this package, both Python and its SymPy library must be installed on your system. If PyCall is installed using Conda (which is the default if no system python is found), then the underlying SymPy library will be installed via Conda when the package is first loaded. Otherwise, installing both Python and the SymPy library (which also requires mpmath) can be done by other means. In this case, the Anaconda distribution is suggested, as it provides a single installation of Python that includes SymPy and many other scientific libraries that can be profitably accessed within Julia via PyCall. (Otherwise, install Python then download the SymPy library from https://github.com/sympy/sympy/releases and install.)

To upgrade the underlying sympy library, which has new releases at a rate similar to Julia, when installed with Conda, the following commands are available:

using Pkg
Pkg.add("Conda") #  if needed
using Conda
Conda.update()

The PyCall interface to SymPy

The only point to this package is that using PyCall to access SymPy is somewhat cumbersome. The following is how one would define a symbolic value x, take its sine, then evaluate the symboic expression for x equal pi, say:

using PyCall
sympy = pyimport("sympy")  #
x = sympy.Symbol("x")      # PyObject x
y = sympy.sin(x)           # PyObject sin(x)
z = y.subs(x, sympy.pi)    # PyObject 0
convert(Float64, z)        # 0.0

The sympy object imported on the second line provides the access to much of SymPy's functionality, allowing access to functions (sympy.sin), properties, modules (sympy), and classes (sympy.Symbol, sympy.Pi). The Symbol and sin operations are found within the imported sympy module and, as seen, are referenced with Python's dot call syntax, as implemented in PyCall through a specialized getproperty method.

SymPy's functionality is also found through methods bound to an object of a certain class. The subs method of the y object is an example. Such methods are also accessed with Python's dot-call syntax. The call above substitutes a value of sympy.pi for the symbolic variable x. This leaves the object as a PyObject storing a number which can be brought back into julia through conversion, in this case through an explicit convert call.

Alternatively, PyCall now has a * method, so the above could also be done with:

x = sympy.Symbol("x")
y = sympy.pi * x
z = sympy.sin(y)
convert(Float64, z.subs(x, 1))

With the SymPy package this gets replaced by a more julian syntax:

using SymPy
x = symbols("x")               # or  @syms x
y = sin(pi*x)
y(1)                           # Does y.subs(x, 1). Use y(x=>1) to be specific as to which symbol to substitute

The object x we create is of type Sym, a simple proxy for the underlying PyObject. The package overloads the familiar math functions so that working with symbolic expressions can use natural julia idioms. The final result here is a symbolic value of 0, which prints as 0 and not PyObject 0. To convert it into a numeric value within Julia, the N function may be used, which acts like the float conversion, only there is an attempt to preserve the variable type.

(There is a subtlety, the value of pi here (an Irrational in Julia) is converted to the symbolic PI, but in general won't be if the math constant is coerced to a floating point value before it encounters a symbolic object. It is better to just use the symbolic value PI, an alias for sympy.pi used above.)


SymPy has a mix of function calls (as in sin(x)) and method calls (as in y.subs(x,1)). The function calls are from objects in the base sympy module. When the SymPy package is loaded, in addition to specialized methods for many generic Julia functions, such as sin, a priviledged set of the function calls in sympy are imported as generic functions narrowed on their first argument being a symbolic object, as constructed by @syms, Sym, or symbols. (Calling import_from(sympy) will import all the function calls.)

The basic usage follows these points:

generic methods from Julia and imported functions in the sympy namespace are called through fn(object)

SymPy methods are called through Python's dot-call syntax: object.fn(...)

Contructors, like sympy.Symbol, and other non-function calls from sympy are qualified with sympy.Constructor(...). Such qualified calls are also useful when the first argument is not symbolic.

So, these three calls are different,

sin(1), sin(Sym(1)), sympy.sin(1)

The first involves no symbolic values. The second and third are related and return a symbolic value for sin(1). The second dispatches on the symbolic argument Sym(1), the third has no dispatch, but refers to a SymPy function from the sympy object. Its argument, 1, is converted by PyCall into a Python object for the function to process.

In the initial example, slightly rewritten, we could have issued:

x = symbols("x")
y = sin(pi*x)
y.subs(x, 1)

The first line calls a provided alias for sympy.symbols which is defined to allow a string (or a symbol) as an argument. The second, dispatches to sympy.sin, as pi*x is symbolic-- x is, and multiplication promotes to a symbolic value. The third line uses the dot-call syntax of PyCall to call the subs method of the symbolic y object.

Not illustrated above, but classes and other objects from SymPy are not brought in by default, and can be accessed using qualification, as in sympy.Function (used, as is @syms, to define symbolic functions).

Download Details: 

Author: JuliaPy
Source Code: https://github.com/JuliaPy/SymPy.jl 
License: MIT license

#julia #python #sympy 

LibSndFile.jl: Julia interface to Libsndfile

LibSndFile.jl

LibSndFile.jl is a wrapper for libsndfile, and supports a wide variety of file and sample formats. The package uses the FileIO load and save interface to automatically figure out the file type of the file to be opened, and the file contents are represented as a SampleBuf. For streaming I/O we support FileIO's loadstreaming and savestreaming functions as well. The results are represented as SampleSource (for reading), or SampleSink (for writing) subtypes. These buffer and stream types are defined in the SampledSignals package.

Note that the load/save/etc. interface is exported from FileIO, and LibSndFile registers itself when the loaded, so you should bring in both packages. LibSndFile doesn't export any of its own names.

julia> using FileIO: load, save, loadstreaming, savestreaming
julia> import LibSndFile
julia> load("audiofile.wav")
2938384-frame, 1-channel SampleBuf{FixedPointNumbers.Fixed{Int16,15}, 2}
66.63002267573697s sampled at 44100.0Hz
▆▅▆▆▆▆▆▅▆▆▆▇▇▇▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▇▆▆▆▆▆▇▆▇▆▇▆▆▆▅▆▆▆▆▆▆▅▆▆▅▆▅▆▆▇▇▇▇▆▆▆▆▆▆▇▆▆▆▆▆▆▆▇▆▇▂

Examples

Read ogg file, write first 1024 samples of the first channel to new wav file

x = load("myfile.ogg")
save("myfile_short.wav", x[1:1024])

Read file, write the first second of all channels to a new file

x = load("myfile.ogg")
save("myfile_short.wav", x[0s..1s, :])

Read stereo file, write mono mix

x = load("myfile.wav")
save("myfile_mono.wav", x[:, 1] + x[:, 2])

Plot the left channel

x = load("myfile.wav")
plot(x[:, 1]) # plots with samples on the x axis
plot(domain(x), x[:, 1]) # plots with time on the x axis

Plot the spectrum of the left channel

x = load("myfile.wav")
f = fft(x) # returns a FrequencySampleBuf
fmin = 0Hz
fmax = 10000Hz
fs = Float32[float(f_i) for f_i in domain(f[fmin..fmax])]
plot(fs, abs.(f[fmin..fmax]).data, xlim=(fs[1],fs[end]), ylim=(0,20000))

Load a long file as a stream and plot the left channel from 2s to 3s

stream = loadstreaming("myfile.ogg")
x = read(stream, 4s)[2s..3s, 1]
close(stream)
plot(domain(x), x)

To handle closing the file automatically (including in the case of unexpected exceptions), we support the do block syntax

data = loadstreaming("data/never_gonna_give_you_up.ogg") do f
    read(f)
end

Supported Formats

See the libsndfile homepage for details, but in summary it supports reading and writing:

  • Microsoft WAV
  • Ogg/Vorbis
  • FLAC
  • SGI / Apple AIFF / AIFC
  • RAW
  • Sound Designer II SD2
  • Sun / DEC / NeXT AU / SND
  • Paris Audio File (PAF)
  • Commodore Amiga IFF / SVX
  • Sphere Nist WAV
  • IRCAM SF
  • Creative VOC
  • Soundforge W64
  • GNU Octave 2.0 MAT4
  • GNU Octave 2.1 MAT5
  • Portable Voice Format PVF
  • Fasttracker 2 XI
  • HMM Tool Kit HTK
  • Apple CAF

Note not all file formats support all samplerates and bit depths. Currently LibSndFile.jl supports WAV, Ogg Vorbis, and FLAC files. Please file an issue if support for other formats would be useful.

Related Packages

  • SampledSignals.jl provides the basic stream and buffer types used by this package.
  • MP3.jl supports reading and writing MP3 files
  • WAV.jl is a pure-julia package supporting the WAV file format.
  • Opus.jl wraps libopus and allows you to read and write Opus audio.
  • PortAudio.jl can be used to interface with your sound card to record and play audio.

A Note on Licensing

libsndfile is licensed under the LGPL, which is very permissive providing that libsndfile is dynamically linked. LibSndFile.jl is licensed under the MIT license, allowing you to statically compile the wrapper into your Julia application. Remember that you must still abide by the terms of the libsndfile license when using this wrapper, in terms of whether libsndfile is statically or dynamically linked.

Note that this is to the best of my understanding, but I am not an attorney and this should not be considered legal advice.

Download Details:

Author: JuliaAudio
Source Code: https://github.com/JuliaAudio/LibSndFile.jl 
License: MIT license

#julia #audio #interface 

OCCA.jl: Julia interface Into OCCA

OCCA.jl

OCCA is a cross platform single-instruction-multiple-data (SIMD) threading library that is retargetable to multiple backends such as pthreads, openmp, opencl, and cuda. OCCA.jl is a Julia interface into OCCA. The main OCCA repository can be found here.

Installation and testing.

Pkg.add("OCCA");
#This takes a minute because no precompiled OCCA binaries exist.
#OCCA will build with no parallel backends by default because 
#reliable backend detection is under development.
#To rebuild with e.g. opencl and cuda you would run
using OCCA;
OCCA.rebuildwith!(opencl=true,cuda=true);

#To run tests for all compiled backends, run:
Pkg.test("OCCA");

#If a backend is not compiled, that test will simply pass without doing anything.
#OCCA will default to serial mode if no backend is installed, so the tests
#still provide some information about correctness of the test kernels (ignoring
#parallel issues such as race conditions and deadlocks)

An example script.

addVectors.okl

kernel void addVectors(const int entries,
                       const float *a,
                       const float *b,
                       float *ab){
  for(int group = 0; group < ((entries + 15) / 16); ++group; outer0){
    for(int item = 0; item < 16; ++item; inner0){
      const int N = (item + (16 * group));

      if(N < entries)
        ab[N] = a[N] + b[N];
    }
  }
}

advectors.jl


    
    infostring   = "mode = OpenMP  , schedule = compact, chunk = 10";

    entries = 5

    device = OCCA.Device(infostring);

    a  = Float32[1 - i for i in 1:entries]
    b  = Float32[i     for i in 1:entries]
    ab = Array(Float32,(length(a),));

    correctvals = [1.0 for i in 1:entries];

    o_a  = OCCA.malloc(device, a);
    o_b  = OCCA.malloc(device, b);
    o_ab = OCCA.malloc(device, ab);

    addvectors = OCCA.buildkernel(device,"addVectors.okl","addVectors")

    OCCA.runkernel!(addvectors,entries,o_a,o_b,o_ab)

    OCCA.memcpy!(ab, o_ab)



Known issues

-The build script does not work for Windows, this is under development. -If OCCA kernel file uses shared memory and you target OpenCL+CPU, it will crash. This appears to be an OpenCL problem and not an OCCA problem.

Contributing

Contributing code Fork this repository on Github, make desired changes, and submit pull request.

Helping with tests and builds It would be enormously helpful if issues could be opened with any build or test failures, along with the specs of the machines on which the builds or tests failed.

Editor Issues .OKL files have a nearly-C grammar, and so most syntax highlighting modules designed for vanilla C will also do a decent job highlighting .OKL files.

Download Details:

Author: ReidAtcheson
Source Code: https://github.com/ReidAtcheson/OCCA.jl 
License: MIT license

#julia #interface 

Monty  Boehm

Monty Boehm

1656408660

Word2Vec.jl: Julia interface to Word2vec

Word2Vec 

Julia interface to word2vec

Word2Vec takes a text corpus as input and produces the word vectors as output. Training is done using the original C code, other functionalities are pure Julia. 

Installation

Pkg.add("Word2Vec")

Note: Only linux and OS X are supported.

Functions

All exported functions are documented, i.e., we can type ? functionname to get help. For a list of functions, see here.

Examples

We first download some text corpus, for example http://mattmahoney.net/dc/text8.zip.

Suppose the file text8 is stored in the current working directory. We can train the model with the function word2vec.

julia> word2vec("text8", "text8-vec.txt", verbose = true)
Starting training using file text8
Vocab size: 71291
Words in train file: 16718843
Alpha: 0.000002  Progress: 100.04%  Words/thread/sec: 350.44k  

Now we can import the word vectors text8-vec.txt to Julia.

julia> model = wordvectors("./text8-vec")
WordVectors 71291 words, 100-element Float64 vectors

The vector representation of a word can be obtained using get_vector.

julia> get_vector(model, "book")'
100-element Array{Float64,1}:
 -0.05446138539336186
  0.001090934639284009
  0.06498087707990222
  ⋮
 -0.0024113040415322516
  0.04755140828570571
  0.039764719065723826

The cosine similarity of book, for example, can be computed using cosine_similar_words.

julia> cosine_similar_words(model, "book")
10-element Array{String,1}:
 "book"
 "books"
 "diary"
 "story"
 "chapter"
 "novel"
 "preface"
 "poem"
 "tale"
 "bible"

Word vectors have many interesting properties. For example, vector("king") - vector("man") + vector("woman") is close to vector("queen").

5-element Array{String,1}:
 "queen"
 "empress"
 "prince"
 "princess"
 "throne"

References

Tomas Mikolov, Kai Chen, Greg Corrado, and Jeffrey Dean, "Efficient Estimation of Word Representations in Vector Space", In Proceedings of Workshop at ICLR, 2013. [pdf]

Tomas Mikolov, Ilya Sutskever, Kai Chen, Greg Corrado, and Jeffrey Dean. "Distributed Representations of Words and Phrases and their Compositionality", In Proceedings of NIPS, 2013. [pdf]

Tomas Mikolov, Wen-tau Yih, and Geoffrey Zweig, "Linguistic Regularities in Continuous Space Word Representations", In Proceedings of NAACL HLT, 2013. [pdf]

Acknowledgements

The design of the package is inspired by Daniel Rodriguez (@danielfrg)'s Python word2vec interface.

Reporting Bugs

Please file an issue to report a bug or request a feature.

See demo for more details.

Author: JuliaText
Source Code: https://github.com/JuliaText/Word2Vec.jl 
License: View license

#julia #text #interface 

LTspice.jl: Julia interface to LTspice

LTspice.jl

LTspice.jl provides a julia interface to LTspiceTM. Several interfaces are provided.

  1. A dictionary like interface to access parameters and measurements by name.
  2. An array interface, which is primarily for measurements of stepped simulations.
  3. Simulations can be called like functions.

Installation

LTspice.jl is currently unregistered. It can be installed using Pkg.clone.

Pkg.clone("https://github.com/cstook/LTspice.jl.git")

The julia documentation section on installing unregistered packages provides more information.

LTspice.jl is compatible with julia 1.0

Example 1

Import the module.

using LTspice

Create an instance of LTspiceSimulation.

example1 = LTspiceSimulation("example1.asc",tempdir=true)

Access parameters and measurements using their name as the key.

Set a parameter to a new value.

example1["Rload"] = 20.0  # set parameter Rload to 20.0

Read the resulting measurement.

loadpower = example1["Pload"] # run simulation, return Pload

Circuit can be called like a function

loadpower = example1(100.0)  # pass Rload, return Pload

Use Optim.jl to perform an optimization on a LTspice simulation

using Optim
result = optimize(rload -> -example1(rload)[1],10.0,100.0)
rload_for_maximum_power = example1["Rload"]

Supported Platforms

LTspice.jl works on windows and linux with LTspice under wine. Osx is not supported.

Download Details:

Author: cstook
Source Code: https://github.com/cstook/LTspice.jl 
License: View license

#julia #interface