# consider the following list with 4 elements 
L = [1,-2,0,5]
print(L)

# 3 * v
v = [1,-2,0,5]
print("v is",v)
# we use the same list for the result
for i in range(len(v)):
    v[i] = 3 * v[i]
print("3v is",v)

# -0.6 * u
# reinitialize the list v
v = [1,-2,0,5]
for i in range(len(v)):
    v[i] = -0.6 * v[i]
print("0.6v is",v)

u = [-3,-2,0,-1,4]
v = [-1,-1,2,-3,5]
result=[]
for i in range(len(u)):
    result.append(u[i]+v[i])

print("u+v is",result)

# print the result vector similarly to a column vector
print() # print an empty line
print("the elements of u+v are")
for j in range(len(result)):
    print(result[j])

from random import randrange
#
# your solution is here
#
from random import randrange

dimension = 7

# create u and v as empty lists 
u = []
v = []

for i in range(dimension):
    u.append(randrange(-10,11)) # add a randomly picked number to the list u
    v.append(randrange(-10,11)) # add a randomly picked number to the list v

# print both lists
print("u is",u)
print("v is",v)
#r=randrange(-10,11) # randomly pick a number from the list {-10,-9,...,-1,0,1,...,9,10}


#
# your solution is here
#
# please execute the cell for Task 1 to define u and v
# create a result list 

# the first method
result=[]

# fill it with zeros
for i in range(dimension): 
    result.append(0)

print("by using the first method, the result vector is initialized to",result)

# the second method
# alternative and shorter solution for creating a list with zeros
result = [0] * 7 

print("by using the second method, the result vector is initialized to",result)

# calculate 3u-2v
for i in range(dimension):
    result[i] = 3 * u[i] - 2 * v[i]

# print all lists
print("u is",u)
print("v is",v)
print("3u-2v is",result)

v = [-1,-3,5,3,1,2]

length_square=0
for i in range(len(v)):
    print(v[i],":square ->",v[i]**2) # print each entry and its square value
    length_square = length_square + v[i]**2 # sum up the square of each entry

length = length_square ** 0.5 # take the square root of the summation of the squares of all entries
print("the summation is",length_square)
print("then the length is",length)

# for square root, we can also use built-in function math.sqrt
print() # print an empty line
from math import sqrt
print("the square root of",length_square,"is",sqrt(length_square))

#
# your solution is here
#
u = [1,-2,-4,2]

fouru=[4,-8,-16,8]

len_u = 0
len_fouru = 0
for i in range(len(u)):
    len_u = len_u + u[i]**2 # adding square of each value 
    len_fouru = len_fouru + fouru[i]**2 # adding square of each value

len_u = len_u ** 0.5 # taking square root of the summation
len_fouru = len_fouru ** 0.5 # taking square root of the summation

# print the lengths
print("length of u is",len_u)
print("4 * length of u is",4 * len_u)
print("length of 4u is",len_fouru)

#
# your solution is here
#
from random import randrange

u = [1,-2,-4,2]

print("u is",u)

r = randrange(9) # r is a number in {0,...,8}
r = r + 1 # r is a number in {1,...,9}
r = r/10 # r is a number in {1/10,...,9/10}

print()
print("r is",r)

newu=[]

for i in range(len(u)):
    newu.append(-1*r*u[i])

print()   
print("-ru is",newu)
print()

length = 0

for i in range(len(newu)):
    length = length + newu[i]**2 # adding square of each number
    print(newu[i],"->[square]->",newu[i]**2)
    
print()    
print("the summation of squares is",length)    
length = length**0.5 # taking square root
print("the length of",newu,"is",length)