Git Product home page Git Product logo

abbreviatedtypes.jl's Introduction

AbbreviatedTypes

CI Codecov

AbbreviatedTypes defines compact type aliases of frequently used types with long names, e.g., AbsVecFloat for AbstractVector{Float64}. The "abbreviated" type names are useful in making function signatures more compact.

Change since Version 0.3

Before Version 0.3, AbbreviatedTypes defined the type aliases internally and exported them so that they can be used in the package importing AbbreviatedTypes. Since Version 0.3, AbbreviatedTypes does not define the type aliases internally; instead, it defines the type aliases directly in the packages importing AbbreviatedTypes. This allows the users to tailor the behavior of AbbreviatedTypes for individual packages importing it. Specifically, the users can define Float as any of the concrete subtypes of AbstractFloat, i.e., Float16, Float32, or Float64. This is useful when you want to use AbsVecFloat as an abstract vector of Float32 in one package (e.g., written for GPU) and of Float64 in another package. The different definitions of Float in different packages do not conflict with one another because they live in different namespaces.

Usage

After using AbbreviatedTypes, call the macro @define_types_with() with the concrete subtype of AbstractFloat. For example,

module MyPkg

using AbbreviatedTypes; @define_types_with(Float64)  # Float == Float64

function myfun(x::AbsVecFloat)  # AbsVecFloat == AbstractVector{Float64}
    ...
end

The technique of using a macro to inject definitions in a calling module is discussed in this discourse thread.

For the definitions of available type aliases, see the code here. The usage of the type aliases should be trivial, but I would like to note the special usage of the aliases of StaticArray types as constructors. For example, SInt{K} is an alias of SVector{K,Int}, but SInt can be also used as a constructor without specifying the length K:

x1 = SInt{3}(1, 2, 3)  # [1, 2, 3]
x2 = SInt(1, 2, 3)  # [1, 2, 3]
@assert x1 == x2

Similar type aliases SBool, SFloat, and SComplexF are defined. When used as constructors, these automatically convert the arguments to the corresponding element type:

SBool(1, 0)  # [true, false]
SInt(1.0, 2.0, 3.0)  # [1, 2, 3]
SFloat(1, 2, 3)  # [1.0, 2.0, 3.0]
SComplexF(1, 2, 3)  # [1.0 + 0.0im, 2.0 + 0.0im, 3.0 + 0.0im]

Absolute and relative error tolerances for floating point arithmetic

In double-precision floating point arithmetic, eps(Float64) and Base.rtoldefault(Float64) are commonly used as absolute and relative error tolerances. In addition to type aliases, AbbreviatedTypes defines τₐ₀ and τᵣ₀ as eps(Float) and Base.rtoldefault(Float), so that they can be used as the absolute and relative error tolerances for the user-defined Float type through @define_types_with().

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.