How to resolve the algorithm Vector products step by step in the Groovy programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Vector products step by step in the Groovy programming language
Table of Contents
Problem Statement
A vector is defined as having three dimensions as being represented by an ordered collection of three numbers: (X, Y, Z). If you imagine a graph with the x and y axis being at right angles to each other and having a third, z axis coming out of the page, then a triplet of numbers, (X, Y, Z) would represent a point in the region, and a vector from the origin to the point. Given the vectors: then the following common vector products are defined:
Given the three vectors:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Vector products step by step in the Groovy programming language
Source code in the groovy programming language
def pairwiseOperation = { x, y, Closure binaryOp ->
assert x && y && x.size() == y.size()
[x, y].transpose().collect(binaryOp)
}
def pwMult = pairwiseOperation.rcurry { it[0] * it[1] }
def dotProduct = { x, y ->
assert x && y && x.size() == y.size()
pwMult(x, y).sum()
}
def crossProductS = { x, y ->
assert x && y && x.size() == 3 && y.size() == 3
[x[1]*y[2] - x[2]*y[1], x[2]*y[0] - x[0]*y[2] , x[0]*y[1] - x[1]*y[0]]
}
def rotR = {
assert it && it.size() > 2
[it[-1]] + it[0..-2]
}
def rotL = {
assert it && it.size() > 2
it[1..-1] + [it[0]]
}
def pwSubtr = pairwiseOperation.rcurry { it[0] - it[1] }
def crossProductV = { x, y ->
assert x && y && x.size() == 3 && y.size() == 3
pwSubtr(pwMult(rotL(x), rotR(y)), pwMult(rotL(y), rotR(x)))
}
def test = { crossProduct ->
def scalarTripleProduct = { x, y, z ->
dotProduct(x, crossProduct(y, z))
}
def vectorTripleProduct = { x, y, z ->
crossProduct(x, crossProduct(y, z))
}
def a = [3, 4, 5]
def b = [4, 3, 5]
def c = [-5, -12, -13]
println(" a . b = " + dotProduct(a,b))
println(" a x b = " + crossProduct(a,b))
println("a . (b x c) = " + scalarTripleProduct(a,b,c))
println("a x (b x c) = " + vectorTripleProduct(a,b,c))
println()
}
test(crossProductS)
test(crossProductV)
You may also check:How to resolve the algorithm Levenshtein distance step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Take notes on the command line step by step in the R programming language
You may also check:How to resolve the algorithm State name puzzle step by step in the J programming language
You may also check:How to resolve the algorithm Pascal's triangle step by step in the Excel programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the BASIC programming language