Skip to main content

Algebra and Analytic Geometry in Python

Project description

algepy

English readme version, click to spand.

What is Algepy?

Algepy is a Python Package that allows you to manipulate vectors, points and planes. It can be useful to calculate or verify the results of your operations.

This project is still under development and is not fully developed, it may have some bugs or failures.

Installation

Using Python Package Index (PyPI)

pip install algepy

Manually

git clone https://github.com/manucabral/algepy.git
cd algepy

Vector

To create a vector you simply need to instantiate the Vector class with its components (x, y, z)

By default it will have 3 dimensions but you can specify the dimension as in the following example.

from algepy import Vector
v = Vector(x=1, y=1, z=1)
u = Vector(x=1, y=1, z=1, dimension=2)

Basic operations

To add and subtract you just have to use the + and - operator, both operations returns a vector.

>>> from algepy import Vector
>>> u = Vector(x=1, y=2, z=3)
>>> v = Vector(x=0, y=2, z=5)
>>> u + v
(1,4,8)
>>> u - v
(1,0,-2)

Opposite

To get the opposite of a vector you have to use its opposite method, this method returns a new vector.

>>> from algepy import Vector
>>> u = Vector(x=1, y=2, z=3)
>>> u.opposite()
(-1,-2,-3)

Magnitude

To get magnitude of the vector, you have to use magnitude method, this method returns a decimal number.

>>> from algepy import Vector
>>> u = Vector(x=1, y=2, z=3)
>>> u.magnitude()
3.7416573867739413

Direction Cosine

To get the direction angles of a vector you have to use the direction_cosine method, this method requires that you specify the axis (x, z, y).

The method returns radians by default but you can change it to degrees using the degrees parameter, the same applies with the decimals parameter.

>>> from algepy import Vector
>>> a = Vector(x=2, y=0, z=-2)
>>> a.direction_cosine(axis='x', degrees=True)
45.0
>>> a.direction_cosine(axis='y', degrees=True)
90.0
>>> a.direction_cosine(axis='z', degrees=True)
135.0

Angle between two vectors

To get the angle between two vectors, use the commutative method angle.

The method returns radians by default but you can change it to degrees using the degrees parameter, the same applies with the decimals parameter.

>>> from algepy import Vector
>>> u = Vector(x=1, y=1, z=3)
>>> v = Vector(x=-1, y=0, z=4)
>>> u.angle(v, degrees=True, decimals=3)
36.448
>>> u.angle(v) # resultado en radianes
0.6361

Dot product

To get the dot product between two vectors, use the * operator (do not confuse this operator with the cross product), this operation returns a scalar number.

>>> from algepy import Vector
>>> u = Vector(x=-3, y=5, z=8)
>>> v = Vector(x=1, y=1, z=1)
>>> u * v
10

Perpendicular

To know if a vector is perpendicular to another you have to use the perpendicular method, this method returns a boolean value (True or False)

>>> from algepy import Vector
>>> u = Vector(x=1, y=1, z=3)
>>> v = Vector(x=-1, y=0, z=4)
>>> u.perpendicular(v)
False

Proyection

To get the projection of one vector in the direction of another you have to use the projection method, this method returns a list with two vectors.

w: main vector (u) projected on another vector (v)

n: other vector (v) projected on main vector (u)

The main vector is the vector to which we apply the projection method.

>>> from algepy import Vector
>>> u = Vector(x=1, y=2, z=1)
>>> v = Vector(x=0, y=1, z=-1)
>>> w, n = u.proyection(v)
>>> w
(0.0,0.4999999999999999,-0.4999999999999999) # u on v
>>> n
(1.0,1.5,1.5) # v on u

Cross product

To get the cross product between two vectors, you must use the cross method, this returns the vector resulting from the cross product.

Bear in mind that the vector product is not commutative, since if we change the order of the vectors, the direction and the magnitude of the vector product are preserved, but the sense is reversed.

>>> from algepy import Vector
>>> a = Vector(x=1, y=2, z=3)
>>> b = Vector(x=0, y=2, z=5)
>>> v = a.cross(b)
>>> v
(4,-5,2) # cross product
>>> v.perpendicular(a), v.perpendicular(b)
True, True

Triple product

To get the triple product you have to use the triple method, this returns a number and isn't commutative.

Defined u, v and w When using the method on u.triple(v, w) the cross product between v and w will be applied and then the dot product between u(vx w)

>>> from algepy import Vector
>>> u = Vector(x=1, y=2, z=3)
>>> v = Vector(x=0, y=2, z=5)
>>> w = Vector(x=0, y=0, z=2)
>>> u.triple(v, w)
4
>>> u * v.cross(w) # equivalent

Point

To create a point you simply need to instantiate the Point class with its (x,y,z) components.

You can only use 3-dimensional points.

from algepy import Point
>>> R = Point(x=1, y=1, z=4)
>>> S = Point(x=3, y=0, z=2)

Basic operations

To add and subtract you just have to use the + and - operator, both operations return a point.

Midpoint

To get the midpoint between two points, use the midpoint method, it returns a vector with the components of the midpoint.

from algepy import Point
>>> r = Point(x=1, y=2, z=3)
>>> s = Point(x=3, y=-1, z=2)
>>> r.midpoint(s)
(2.0,0.5,2.5)

Find the vector between two points

To get a vector from two points you have to use the find_vector method, this returns a vector formed from the two points.

from algepy import Point
>>> r = Point(x=1, y=1, z=4)
>>> s = Point(x=3, y=0, z=2)
>>> r.find_vector(s)
(2,-1,-2)

Plane

To create a plane we need the normal vector (vector perpendicular to the plane) and some point that belongs to the plane.

>>> from algepy import Vector, Point, Plane
>>> n = Vector(x=2, y=-3, z=1)
>>> p = Point(x=1, y=3, z=1)
>>> plane = Plane(normal=n, point=p)
>>> plane
π: 2x -3y 1z 6 = 0

If we do not pass the normal vector and the default point so these will be null vectors, we can also manually assign the components of the plane by accessing the properties a, b, c and d.

>>> from algepy import Vector, Point, Plane
>>> plane = Plane()
>>> plane
π: 0x 0y 0z 0 = 0
>>> plane.a = 5
π: 5x 0y 0z 0 = 0

General equation

To get the implicit or general equation of the plane, we simply access the plane object created previously.

>>> plane
π: 2x -3y 1z 6 = 0

Symmetric equation

To get the segmental equation of the plane we must use the symmetric_equation method, for this we need at least to have defined the components of the plane (a, b, c and d).

We can indicate to the method if we want the result as a fraction or by decimals through the fraction parameter

>>> from algepy import Vector, Point, Plane
>>> n = Vector(x=2, y=-3, z=1)
>>> plane = Plane(normal=n)
>>> plane.d = 6
>>> plane.symmetric_equation(fraction=True)
2x/-6 -3y/-6 1z/-6 = 1
>>> plane.symmetric_equation(decimals=3)
x/0.333 y/-0.5 z/0.167 = 1

Plot

Algepy uses pyplot from matplotlib so for this module to work, you need to have this package installed.

For now the plot only supports 3 dimensions, you can try others dimensions but you will have errors.

plot = Plot(name='Example', projection='3d')
plot.show()

Plot a vector

To add a vector to our plot we need to use the add_vector method and also have an origin point for the vector.

Once this is done we can show the graph with the show method.

  from algepy import Vector, Point, Plot
  
  origin = Point(x=0, y=0, z=0)
  a = Vector(x=1, y=2, z=3)
  plot = Plot(name='Vector', projection='3d')
  plot.add_vector(origin=origin, vector=a)
  plot.show()

Plot a point

To add a point to our plot we need to use the add_point method.

Once this is done we can show the graph with the show method.

  from algepy import Point, Plot
  
  p = Point(x=1, y=2, z=3)
  plot = Plot(name='Point', projection='3d')
  plot.add_point(point=p, color='red')
  plot.show()

Plot a plane

To add a plane to our plot we need to use the add_plane method.

Once this is done we can show the graph with the show method.

  from algepy import Vector, Point, Plane
 
  n = Vector(x=2, y=-3, z=1)
  p = Point(x=1, y=3, z=1)
  plane = Plane(normal=n, point=p)
  plot = Plot(projection='3d', range=[-5, 5])
  plot.add_plane(plane=plane, color='red')
  plot.show()

Contributions

All contributions, reports or bug fixes and ideas are welcome. You can go to the issues section and provide your help.

Versión español, click para expander.

¿Qué es algepy?

Algepy es una libreria de python que te permite manipular vectores de hasta 3 dimensiones, te puede ser útil para calcular o verificar los resultados de tus operaciones.

Este proyecto todavía se encuentra en desarrollo y no está completamente desarrollado, puede tener algunos bugs o fallos.

Instalación

Utilizando Python Package Index (PyPI)

pip install algepy

Manualmente

git clone https://github.com/manucabral/algepy.git
cd algepy

Vector

Para definir un vector simplemente necesitas instanciar la clase Vector con sus componentes (x, y, z)

Por defecto tendrá 3 dimensiones pero puedes especificarle la dimensión como en el siguiente ejemplo.

from algepy import Vector
v = Vector(x=1, y=1, z=1)
u = Vector(x=1, y=1, z=1, dimension=2) # ignorará el eje z

Operaciones básica

Para sumar y restar solamente tienes que utilizar el operador + y -, las dos operaciones devuelve un vector.

>>> from algepy import Vector
>>> u = Vector(x=1, y=2, z=3)
>>> v = Vector(x=0, y=2, z=5)
>>> u + v
(1,4,8)
>>> u - v
(1,0,-2)

Opuesto

Para obtener el opuesto de un vector hay que utilizar su método opposite, este método devuelve un nuevo vector.

>>> from algepy import Vector
>>> u = Vector(x=1, y=2, z=3)
>>> u.opposite()
(-1,-2,-3)

Módulo o norma

Para obtener el módulo o la norma del vector hay que utilizar su método magnitude, este método devuelve un número decimal.

>>> from algepy import Vector
>>> u = Vector(x=1, y=2, z=3)
>>> u.magnitude()
3.7416573867739413

Ángulos directores

Para obtener los ángulos directores de un vector hay que utilizar el método direction_cosine, este método requiere que le especifiques el eje obligatoriamente (x, z, y).

El método devuelve por defecto en radianes pero lo puedes cambiar a grados mediante el parámetro degrees, aplica lo mismo con el parámetro decimals.

>>> from algepy import Vector
>>> a = Vector(x=2, y=0, z=-2)
>>> a.direction_cosine(axis='x', degrees=True) # ángulo director respecto al eje x
45.0
>>> a.direction_cosine(axis='y', degrees=True) # ángulo director respecto al eje y
90.0
>>> a.direction_cosine(axis='z', degrees=True) # ángulo director respecto al eje z
135.0

Ángulo entre dos vectores

Para obtener el ángulo que se forma entre dos vectores hay que utilizar el método conmutativo angle.

El método devuelve por defecto en radianes pero lo puedes cambiar a grados mediante el parámetro degrees, aplica lo mismo con el parámetro decimals.

>>> from algepy import Vector
>>> u = Vector(x=1, y=1, z=3)
>>> v = Vector(x=-1, y=0, z=4)
>>> u.angle(v, degrees=True, decimals=3) # resultado en grados con 3 decimales
36.448
>>> u.angle(v) # resultado en radianes
0.6361

Producto escalar

Para obtener el producto escalar entre dos vectores hay que utilizar el operador * (no confundir este operador con el producto vectorial) esta operación devuelve un número escalar.

>>> from algepy import Vector
>>> u = Vector(x=-3, y=5, z=8)
>>> v = Vector(x=1, y=1, z=1)
>>> u * v
10

Perpendicularidad

Para saber si un vector es perpendicular a otro hay que utilizar el método perpendicular, este método devuelve un valor booleano (True o False)

>>> from algepy import Vector
>>> u = Vector(x=1, y=1, z=3)
>>> v = Vector(x=-1, y=0, z=4)
>>> u.perpendicular(v)
False

Proyección de vectores

Para obtener la proyección de un vector en la dirección de otro hay que utilizar el método projection, este método devuelve una lista con dos vectores.

w: vector principal (u) proyectado en otro vector (v)

n: otro vector (v) proyectado en el vector principal (u)

El vector principal es el vector al que le aplicamos el método projection.

>>> from algepy import Vector
>>> u = Vector(x=1, y=2, z=1)
>>> v = Vector(x=0, y=1, z=-1)
>>> w, n = u.proyection(v)
>>> w
(0.0,0.4999999999999999,-0.4999999999999999) # vector u proyectado en v
>>> n
(1.0,1.5,1.5) # vector v proyectado en u

Producto vectorial

Para obtener el producto vectorial entre dos vectores hay que utilizar el método cross, este método devuelve el vector resultado del producto vectorial.

Tener en cuenta que el producto vectorial no es conmutativo, ya que si cambiamos el orden de los vectores se conservan la dirección y el módulo del producto vectorial pero se invierte el sentido.

>>> from algepy import Vector
>>> a = Vector(x=1, y=2, z=3)
>>> b = Vector(x=0, y=2, z=5)
>>> v = a.cross(b)
>>> v
(4,-5,2) # producto vectorial
>>> v.perpendicular(a), v.perpendicular(b)
True, True

Producto mixto

Para obtener el producto mixto hay que utilizar el método triple, este método devuelve un escalar y no es conmutativo así que hay que tener en cuenta lo siguiente.

Definidos u, v y w Cuando se utiliza el método en u.triple(v,w) se aplicará primero el producto vectorial entre v y w para después calcular el producto escalar u(vx w)

>>> from algepy import Vector
>>> u = Vector(x=1, y=2, z=3)
>>> v = Vector(x=0, y=2, z=5)
>>> w = Vector(x=0, y=0, z=2)
>>> u.triple(v, w)
4
>>> u * v.cross(w) # equivalente

Punto

Para definir un punto simplemente necesitas instanciar la clase Point con sus componentes (x, y, z).

Por ahora solamente puedes utilizar puntos de 3 dimensiones.

from algepy import Point
>>> R = Point(x=1, y=1, z=4)
>>> S = Point(x=3, y=0, z=2)

Operaciones básicas

Para sumar y restar solamente tienes que utilizar el operador + y -, las dos operaciones devuelve un punto.

Punto medio

Para obtener el punto medio entre dos puntos hay que utilizar el método midpoint, este devuelve un vector con los componentes del punto medio.

from algepy import Point
>>> r = Point(x=1, y=2, z=3)
>>> s = Point(x=3, y=-1, z=2)
>>> r.midpoint(s)
(2.0,0.5,2.5)

Vector a partir de dos puntos

Para obtener un vector a partir de dos puntos hay que utilizar el método find_vector, este devuelve un vector formado a partir de los dos puntos.

>>> from algepy import Point
>>> r = Point(x=1, y=1, z=4)
>>> s = Point(x=3, y=0, z=2)
>>> r.find_vector(s)
(2,-1,-2)

Plano

Para crear un plano necesitamos el vector normal (vector perpendicular al plano) y algún punto que pertenezca al plano.

>>> from algepy import Vector, Point, Plane
>>> n = Vector(x=2, y=-3, z=1)
>>> p = Point(x=1, y=3, z=1)
>>> plano = Plane(normal=n, point=p)
>>> plano
π: 2x -3y 1z 6 = 0

Si no le pasamos el vector normal y el punto por defecto estos serán vectores nulos, también podemos asignarle manualmente los componentes del plano accediendo a las propiedades a, b, c y d.

>>> from algepy import Vector, Point, Plane
>>> plano = Plane()
>>> plano
π: 0x 0y 0z 0 = 0
>>> plano.a = 5
π: 5x 0y 0z 0 = 0

Ecuación general

Para obtener la ecucación implícita o general del plano simplemente accedemos al objeto plano creado anteriormente.

>>> plano
π: 2x -3y 1z 6 = 0

Ecuación segmentaria

Para obtener la ecucación segmentaria del plano hay que utilizar el método symmetric_equation, para esto necesitamos al menos tener definido los componentes del plano (a, b, c y d).

Al método le podemos indicar si el resultado lo queremos como fracción o por decimales mediante el parámetro fraction

>>> from algepy import Vector, Point, Plane
>>> n = Vector(x=2, y=-3, z=1)
>>> plano = Plane(normal=n)
>>> plano.d = 6
>>> plano.symmetric_equation(fraction=True)
2x/-6 -3y/-6 1z/-6 = 1
>>> plano.symmetric_equation(decimals=3)
x/0.333 y/-0.5 z/0.167 = 1

Gráfico

Algepy utiliza pyplot de matplotlib así que para que este módulo te funcione necesitas tener instalado este paquete.

Por ahora el gráfico solamente soporta 3 dimensiones, puedes intentar con otras pero corres el riesgo de obtener varios errores.

grafico = Plot(name='Ejemplo', projection='3d')
grafico.show()

Gráfico de un vector

Para agregar un vector a nuestro gráfico necesitamos utilizar el método add_vector y además tener un punto de origen para el vector.

Una vez realizado esto podemos mostrar el gráfico con el método show

from algepy import Vector, Point, Plot
  
  origen = Point(x=0, y=0, z=0)
  a = Vector(x=1, y=2, z=3)
  grafico = Plot(name='Vector', projection='3d')
  grafico.add_vector(origin=origen, vector=a)
  grafico.show()

Gráfico de un punto

Para agregar un punto a nuestro gráfico necesitamos utilizar el método add_point

Una vez realizado esto podemos mostrar el gráfico con el método show

  from algepy import Point, Plot
  p = Point(x=1, y=2, z=3)
  grafico = Plot(name='Punto', projection='3d')
  grafico.add_point(point=p, color='red')
  grafico.show()

Gráfico de un plano

Para agregar un plano a nuestro gráfico necesitamos utilizar el método add_plane

Una vez realizado esto podemos mostrar el gráfico con el método show

  from algepy import Vector, Point, Plane
 
  n = Vector(x=2, y=-3, z=1)
  p = Point(x=1, y=3, z=1)
  plano = Plane(normal=n, point=p)
  grafico = Plot(projection='3d', range=[-5, 5])
  grafico.add_plane(plane=plano, color='red')
  grafico.show()

Contribución

Todas las contribuciones, reportes o arreglos de bugs e ideas es bienvenido. Para esto puedes dirigirte al apartado de issues y aportar tu ayuda.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

algepy-0.0.3.tar.gz (18.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page