Adding two vectors | Linear Algebra with Python

Linear Algebra with Python: Here, we are going to learn how to define and add two given vectors in Python?
Submitted by Anuj Singh, on May 10, 2020

Prerequisite: Linear Algebra | Defining a Vector

In the python code, we will add two vectors. We can add two vectors only and only if the both the vectors are in the same dimensional space. For our code, we consider the vectors in 4-dimensional space.

Python code to add two vectors

#Vectors in Linear Algebra

a = [3, 5, -5, 8]
b = [4, 7, 9, -4]

print("Vector a = ", a)
print("Vector b = ", b)

# This is a 4 dimensional vector
# a list in python is a vector in linear algebra

# adding vectors
sum = []
for i in range(len(a)):
    sum.append(a[i] + b[i])

print("Vector Addition = ", sum)
# This is how we can print a vector in python

Output

Vector a =  [3, 5, -5, 8]
Vector b =  [4, 7, 9, -4]
Vector Addition =  [7, 12, 4, 4]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.