How to resolve the algorithm Vector step by step in the Python programming language
How to resolve the algorithm Vector step by step in the Python programming language
Table of Contents
Problem Statement
Implement a Vector class (or a set of functions) that models a Physical Vector. The four basic operations and a pretty print function should be implemented.
The Vector may be initialized in any reasonable way.
The four operations to be implemented are:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Vector step by step in the Python programming language
The provided Python code defines a Vector
class with various mathematical operations and properties. Let's break down the code step by step:
-
Vector Class:
-
The
Vector
class is initialized with two parameters:m
(the angular coefficient) andvalue
(the magnitude of the vector). -
It calculates and stores essential properties of the vector, including the
angle
,x
component, andy
component.
-
-
Addition (
:math:vector1 + vector2
):- The
__add__
method defines the vector addition operation. - It calculates the final x and y components by adding the respective components of the two vectors.
- The final magnitude and angular coefficient are computed using these components to create a new
Vector
instance.
- The
-
Negation (
:math:(- vector)
):- The
__neg__
method defines the vector negation operation, which simply returns a new vector with the same magnitude but opposite sign in the y-component.
- The
-
Subtraction (
:math:vector1 - vector2
):- The
__sub__
method defines the vector subtraction operation. - It calls the
__add__
method with a negated version of the second vector, effectively performing vector subtraction.
- The
-
Multiplication by a Scalar (
:math:vector * scalar
):- The
__mul__
method defines vector multiplication by a scalar. - It returns a new vector with the same angular coefficient but the magnitude multiplied by the scalar.
- The
-
Division by a Scalar (
:math:vector / scalar
):- The
__div__
method defines vector division by a scalar. - It calls the
__mul__
method with the reciprocal of the scalar, effectively performing vector division.
- The
-
Representation (
:math:repr(vector)
):- The
__repr__
method returns a formatted string containing the vector's properties, including the angular coefficient, angle, value, x component, and y component.
- The
-
Additional Functions:
-
The
hypotenuse
function calculates the hypotenuse of a right-angled triangle from its legs. -
The
pytagoras
function calculates the square root of the sum of squares, which is used to calculate the value of the vector from its components.
-
-
Example Usage:
- The example usage section of the code demonstrates various operations on vector instances, including addition, subtraction, multiplication, and division by scalars.
In summary, this code provides a class (Vector
) for manipulating and performing mathematical operations on two-dimensional vectors in Python. It includes methods for addition, negation, subtraction, scalar multiplication, and scalar division, as well as properties to access the vector's angular coefficient, angle, magnitude, and x and y components.
Source code in the python programming language
class Vector:
def __init__(self,m,value):
self.m = m
self.value = value
self.angle = math.degrees(math.atan(self.m))
self.x = self.value * math.sin(math.radians(self.angle))
self.y = self.value * math.cos(math.radians(self.angle))
def __add__(self,vector):
"""
>>> Vector(1,10) + Vector(1,2)
Vector:
- Angular coefficient: 1.0
- Angle: 45.0 degrees
- Value: 12.0
- X component: 8.49
- Y component: 8.49
"""
final_x = self.x + vector.x
final_y = self.y + vector.y
final_value = pytagoras(final_x,final_y)
final_m = final_y / final_x
return Vector(final_m,final_value)
def __neg__(self):
return Vector(self.m,-self.value)
def __sub__(self,vector):
return self + (- vector)
def __mul__(self,scalar):
"""
>>> Vector(4,5) * 2
Vector:
- Angular coefficient: 4
- Angle: 75.96 degrees
- Value: 10
- X component: 9.7
- Y component: 2.43
"""
return Vector(self.m,self.value*scalar)
def __div__(self,scalar):
return self * (1 / scalar)
def __repr__(self):
"""
Returns a nicely formatted list of the properties of the Vector.
>>> Vector(1,10)
Vector:
- Angular coefficient: 1
- Angle: 45.0 degrees
- Value: 10
- X component: 7.07
- Y component: 7.07
"""
return """Vector:
- Angular coefficient: {}
- Angle: {} degrees
- Value: {}
- X component: {}
- Y component: {}""".format(self.m.__round__(2),
self.angle.__round__(2),
self.value.__round__(2),
self.x.__round__(2),
self.y.__round__(2))
from __future__ import annotations
import math
from functools import lru_cache
from typing import NamedTuple
CACHE_SIZE = None
def hypotenuse(leg: float,
other_leg: float) -> float:
"""Returns hypotenuse for given legs"""
return math.sqrt(leg ** 2 + other_leg ** 2)
class Vector(NamedTuple):
slope: float
length: float
@property
@lru_cache(CACHE_SIZE)
def angle(self) -> float:
return math.atan(self.slope)
@property
@lru_cache(CACHE_SIZE)
def x(self) -> float:
return self.length * math.sin(self.angle)
@property
@lru_cache(CACHE_SIZE)
def y(self) -> float:
return self.length * math.cos(self.angle)
def __add__(self, other: Vector) -> Vector:
"""Returns self + other"""
new_x = self.x + other.x
new_y = self.y + other.y
new_length = hypotenuse(new_x, new_y)
new_slope = new_y / new_x
return Vector(new_slope, new_length)
def __neg__(self) -> Vector:
"""Returns -self"""
return Vector(self.slope, -self.length)
def __sub__(self, other: Vector) -> Vector:
"""Returns self - other"""
return self + (-other)
def __mul__(self, scalar: float) -> Vector:
"""Returns self * scalar"""
return Vector(self.slope, self.length * scalar)
def __truediv__(self, scalar: float) -> Vector:
"""Returns self / scalar"""
return self * (1 / scalar)
if __name__ == '__main__':
v1 = Vector(1, 1)
print("Pretty print:")
print(v1, end='\n' * 2)
print("Addition:")
v2 = v1 + v1
print(v1 + v1, end='\n' * 2)
print("Subtraction:")
print(v2 - v1, end='\n' * 2)
print("Multiplication:")
print(v1 * 2, end='\n' * 2)
print("Division:")
print(v2 / 2)
You may also check:How to resolve the algorithm Factorial step by step in the WebAssembly programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Wren programming language
You may also check:How to resolve the algorithm Time a function step by step in the Erlang programming language
You may also check:How to resolve the algorithm Random number generator (device) step by step in the Fortran programming language
You may also check:How to resolve the algorithm Problem of Apollonius step by step in the OCaml programming language