GithubHelp home page GithubHelp logo

evitanrelta / cyvector Goto Github PK

View Code? Open in Web Editor NEW
1.0 0.0 0.0 112 KB

An optimised lightweight module, containing the Vector class for 2D vectors, written in Cython.

License: MIT License

Python 100.00%

cyvector's Introduction

CyVector

An optimised lightweight module, containing the Vector class for 2D vectors, written in Cython.

Getting Started

No additional modules required.

Just import CyVector and use the Vector class

Note: Changing the CyVector.pyd file name will break the module. If you want to change its name, you'll have to recompile it.



Documentation

Properties

x and y

        Returns the x and y values of the 2D vector.

magnitude

        Calculates and returns the magnitude.

Note: The first time magnitude is called, the magnitude is calculated, saved in _magVal and returned. Subsequent magnitude calls will return _magVal, unless the x or y value is changed, whereby the next magnitude call will recalcuate, save and return the new magnitude value again.

unitVec

        Constructs and returns the unit vector.

Note: Similar to magnitude, first time unitVec is called, the unit vector is calculated, saved in _unitVecVal and returned; unless x or y is changed.

copy

        Constructs and returns a new Vector with the same x, y values, and same _magVal and _unitVecVal values (if they've been calculated).


Arithmetic Operators and Methods

Let vec and other be Vector instances, and k be a int or float:

- vec

return Vector(- vec._x, - vec._y)

+ vec

return vec.copy

vec + other, vec += other and vec.add(other)

        Vector addition.

vec - other, vec -= other and vec.subtract(other)

        Vector subtraction.

vec / k, vec /= k and vec.scalarDiv(k)

        Scalar division.

vec * other and vec.dot(other)

        Returns the dot product.

Note: vec *= other raises TypeError

vec * k, k * vec and vec.scalarMul(k)

        Scalar multiplication.


Misc.

Let vec and other be Vector instances:

Indexer

  • vec[0] gets and sets vec.x
  • vec[1] gets and sets vec.y
  • vec[i], where i > 1; raises IndexError

Length

        len(vec) will always return 2.

vec.setValue(newX, newY)

        Sets vec.x and vec.y to newX and newY respectively.

__eq__ and __ne__

        vec == other returns True if vec.x == other.x and vec.y == other.y

__str__

        Returns "<{x}, {y}>", where {x} and {y} are the instance's x and y values.

__repr__

        Returns:

<{x}, {y}>
Magnitude: {magnitude}
Unit Vector: {unitVector}

where {x} and {y} are the instance's x and y values;
if the magnitude has been calculated prior, {magnitude} is the magnitude value, else <Not yet calculated> is displayed;
similarly, {unitVector} is the unit vector or <Not yet calculated>.


Null vectors (ie. vector: <0, 0>) will return the string:

<0, 0>
Magnitude: 0
Unit Vector: <Undefined>


Todo

  • - vec should return Vector output, with output._magVal = vec._magVal and output._unitVecVal = new Vector(-vec._unitVecVal.x, -vec._unitVecVal.y)
  • Add .toNumpyArray()
  • Add list(vec) and tuple(vec)
  • Create optimised list of Vectors; and compare efficiency against 2D numpy array

cyvector's People

Contributors

evitanrelta avatar

Stargazers

Yoann Berenguer 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.