2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,30 +1,45 @@
Define a vector 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.
A vector is defined as having three dimensions as being represented by an ordered collection of three numbers:   (X, Y, Z).
Given vectors <code>A = (a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>); B = (b<sub>1</sub>, b<sub>2</sub>, b<sub>3</sub>);</code> and <code>C = (c<sub>1</sub>, c<sub>2</sub>, c<sub>3</sub>);</code> then the following common vector products are defined:
* '''The dot product'''
: A • B = <code>a<sub>1</sub>b<sub>1</sub> + a<sub>2</sub>b<sub>2</sub> + a<sub>3</sub>b<sub>3</sub>;</code> a scalar quantity
* '''The cross product'''
: A x B = <code>(a<sub>2</sub>b<sub>3</sub> - a<sub>3</sub>b<sub>2</sub>, a<sub>3</sub>b<sub>1</sub> - a<sub>1</sub>b<sub>3</sub>, a<sub>1</sub>b<sub>2</sub> - a<sub>2</sub>b<sub>1</sub>);</code> a vector quantity
* '''The scalar triple product'''
: A • (B x C); a scalar quantity
* '''The vector triple product'''
: A x (B x C); a vector quantity
If you imagine a graph with the &nbsp; '''x''' &nbsp; and &nbsp; '''y''' &nbsp; axis being at right angles to each other and having a third, &nbsp; '''z''' &nbsp; axis coming out of the page, then a triplet of numbers, &nbsp; (X, Y, Z) &nbsp; would represent a point in the region, &nbsp; and a vector from the origin to the point.
;Task description
Given the three vectors: <code>a = (3, 4, 5); b = (4, 3, 5); c = (-5, -12, -13)</code>:
Given the vectors:
<big> A = (a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>) </big>
<big> B = (b<sub>1</sub>, b<sub>2</sub>, b<sub>3</sub>) </big>
<big> C = (c<sub>1</sub>, c<sub>2</sub>, c<sub>3</sub>) </big>
then the following common vector products are defined:
* '''The dot product''' &nbsp; &nbsp; &nbsp; (a scalar quantity)
:::: <big> A • B = a<sub>1</sub>b<sub>1</sub> &nbsp; + &nbsp; a<sub>2</sub>b<sub>2</sub> &nbsp; + &nbsp; a<sub>3</sub>b<sub>3</sub> </big>
* '''The cross product''' &nbsp; &nbsp; &nbsp; (a vector quantity)
:::: <big> A x B = (a<sub>2</sub>b<sub>3</sub>&nbsp; - &nbsp; a<sub>3</sub>b<sub>2</sub>, &nbsp; &nbsp; a<sub>3</sub>b<sub>1</sub> &nbsp; - &nbsp; a<sub>1</sub>b<sub>3</sub>, &nbsp; &nbsp; a<sub>1</sub>b<sub>2</sub> &nbsp; - &nbsp; a<sub>2</sub>b<sub>1</sub>) </big>
* '''The scalar triple product''' &nbsp; &nbsp; &nbsp; (a scalar quantity)
:::: <big> A • (B x C) </big>
* '''The vector triple product''' &nbsp; &nbsp; &nbsp; (a vector quantity)
:::: <big> A x (B x C) </big>
;Task:
Given the three vectors:
a = ( 3, 4, 5)
b = ( 4, 3, 5)
c = (-5, -12, -13)
# Create a named function/subroutine/method to compute the dot product of two vectors.
# Create a function to compute the cross product of two vectors.
# Optionally create a function to compute the scalar triple product of three vectors.
# Optionally create a function to compute the vector triple product of three vectors.
# Compute and display: <code>a • b</code>
# Compute and display: <code>a x b</code>
# Compute and display: <code>a • b x c</code>, the scaler triple product.
# Compute and display: <code>a • b x c</code>, the scalar triple product.
# Compute and display: <code>a x b x c</code>, the vector triple product.
;References:
* [[Dot product]] here on RC.
* A starting page on Wolfram Mathworld is {{Wolfram|Vector|Mulitplication}}.
* Wikipedias [[wp:Dot product|dot product]], [[wp:Cross product|cross product]] and [[wp:Triple product|triple product]] entries.
;C.f.
* [[Quaternion type]]
;References:
* &nbsp; A starting page on Wolfram MathWorld is &nbsp; {{Wolfram|Vector|Multiplication}}.
* &nbsp; Wikipedia &nbsp; [[wp:Dot product|dot product]],
: &nbsp; Wikipedia &nbsp; [[wp:Cross product|cross product]]
: &nbsp; Wikipedia &nbsp; [[wp:Triple product|triple product]] entries.
;Related tasks:
* &nbsp; [[Dot product]]
* &nbsp; [[Quaternion type]]
<br><br>

View file

@ -0,0 +1,21 @@
defmodule Vector do
def dot_product({a1,a2,a3}, {b1,b2,b3}), do: a1*b1 + a2*b2 + a3*b3
def cross_product({a1,a2,a3}, {b1,b2,b3}), do: {a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1}
def scalar_triple_product(a, b, c), do: dot_product(a, cross_product(b, c))
def vector_triple_product(a, b, c), do: cross_product(a, cross_product(b, c))
end
a = {3, 4, 5}
b = {4, 3, 5}
c = {-5, -12, -13}
IO.puts "a = #{inspect a}"
IO.puts "b = #{inspect b}"
IO.puts "c = #{inspect c}"
IO.puts "a . b = #{inspect Vector.dot_product(a, b)}"
IO.puts "a x b = #{inspect Vector.cross_product(a, b)}"
IO.puts "a . (b x c) = #{inspect Vector.scalar_triple_product(a, b, c)}"
IO.puts "a x (b x c) = #{inspect Vector.vector_triple_product(a, b, c)}"

View file

@ -0,0 +1,62 @@
import java.util.Arrays;
import java.util.stream.IntStream;
public class VectorsOp {
// Vector dot product using Java SE 8 stream abilities
// the method first create an array of size values,
// and map the product of each vectors components in a new array (method map())
// and transform the array to a scalr by summing all elements (method reduce)
// the method parallel is there for optimization
private static int dotProduct(int[] v1, int[] v2,int length) {
int result = IntStream.range(0, length)
.parallel()
.map( id -> v1[id] * v2[id])
.reduce(0, Integer::sum);
return result;
}
// Vector Cross product using Java SE 8 stream abilities
// here we map in a new array where each element is equal to the cross product
// With Stream is is easier to handle N dimensions vectors
private static int[] crossProduct(int[] v1, int[] v2,int length) {
int result[] = new int[length] ;
//result[0] = v1[1] * v2[2] - v1[2]*v2[1] ;
//result[1] = v1[2] * v2[0] - v1[0]*v2[2] ;
// result[2] = v1[0] * v2[1] - v1[1]*v2[0] ;
result = IntStream.range(0, length)
.parallel()
.map( i -> v1[(i+1)%length] * v2[(i+2)%length] - v1[(i+2)%length]*v2[(i+1)%length])
.toArray();
return result;
}
public static void main (String[] args)
{
int[] vect1 = {3, 4, 5};
int[] vect2 = {4, 3, 5};
int[] vect3 = {-5, -12, -13};
System.out.println("dot product =:" + dotProduct(vect1,vect2,3));
int[] prodvect = new int[3];
prodvect = crossProduct(vect1,vect2,3);
System.out.println("cross product =:[" + prodvect[0] + ","
+ prodvect[1] + ","
+ prodvect[2] + "]");
prodvect = crossProduct(vect2,vect3,3);
System.out.println("scalar product =:" + dotProduct(vect1,prodvect,3));
prodvect = crossProduct(vect1,prodvect,3);
System.out.println("triple product =:[" + prodvect[0] + ","
+ prodvect[1] + ","
+ prodvect[2] + "]");
}
}

View file

@ -13,7 +13,7 @@ my @a = <3 4 5>;
my @b = <4 3 5>;
my @c = <-5 -12 -13>;
say (:@a, :@b, :@c).perl;
say (:@a, :@b, :@c);
say "a b = { @a@b }";
say "a b = <{ @a @b }>";
say "a (b c) = { scalar-triple-product(@a, @b, @c) }";

View file

@ -1,10 +1,57 @@
a <- c( 3.0, 4.0, 5.0)
b <- c( 4.0, 3.0, 5.0)
#===============================================================
# Vector products
# R implementation
#===============================================================
cross <- function(a, b)
c(a[2]*b[3] - a[3]*b[2],
a[3]*b[1] - a[1]*b[3],
a[1]*b[2] - a[2]*b[1])
a <- c(3, 4, 5)
b <- c(4, 3, 5)
c <- c(-5, -12, -13)
cross(a, b)
# [1] 5 5 -7
#---------------------------------------------------------------
# Dot product
#---------------------------------------------------------------
dotp <- function(x, y) {
if (length(x) == length(y)) {
sum(x*y)
}
}
#---------------------------------------------------------------
# Cross product
#---------------------------------------------------------------
crossp <- function(x, y) {
if (length(x) == 3 && length(y) == 3) {
c(x[2]*y[3] - x[3]*y[2], x[3]*y[1] - x[1]*y[3], x[1]*y[2] - x[2]*y[1])
}
}
#---------------------------------------------------------------
# Scalar triple product
#---------------------------------------------------------------
scalartriplep <- function(x, y, z) {
if (length(x) == 3 && length(y) == 3 && length(z) == 3) {
dotp(x, crossp(y, z))
}
}
#---------------------------------------------------------------
# Vector triple product
#---------------------------------------------------------------
vectortriplep <- function(x, y, z) {
if (length(x) == 3 && length(y) == 3 && length(z) == 3) {
crosssp(x, crossp(y, z))
}
}
#---------------------------------------------------------------
# Compute and print
#---------------------------------------------------------------
cat("a . b =", dotp(a, b))
cat("a x b =", crossp(a, b))
cat("a . (b x c) =", scalartriplep(a, b, c))
cat("a x (b x c) =", vectortriplep(a, b, c))

View file

@ -1,29 +1,24 @@
/*REXX program computes the products: the dot product, */
/* the cross product, */
/* the scalar triple product, and*/
/* the vector triple product. */
a = 3 4 5 /*positive numbers don't need " */
b = 4 3 5
c = "-5 -12 -13"
call tellV 'vector A =',a /*show the A vector, aligned #s*/
call tellV 'vector B =',b /*show the B vector, aligned #s*/
call tellV 'vector C =',c /*show the C vector, aligned #s*/
/*REXX program computes the products: dot, cross, scalar triple, and vector triple.*/
a= 3 4 5
b= 4 3 5 /*positive numbers don't need quotes. */
c= "-5 -12 -13"
call tellV 'vector A =', a /*show the A vector, aligned numbers.*/
call tellV 'vector B =', b /* " " B " " " */
call tellV 'vector C =', c /* " " C " " " */
say
call tellV ' dot product [AB] =',dot(a,b)
call tellV 'cross product [AxB] =',cross(a,b)
call tellV 'scalar triple product [A(BxC)] =',dot(a,cross(b,c))
call tellV 'vector triple product [Ax(BxC)] =',cross(a,cross(b,c))
exit /*stick a fork in it, we're done.*/
/*─────────────────────────────────────cross subroutine─────────────────*/
cross: procedure; parse arg x1 x2 x3,y1 y2 y3 /*the CROSS product.*/
return x2*y3-x3*y2 x3*y1-x1*y3 x1*y2-x2*y1 /*a vector quantity.*/
/*─────────────────────────────────────dot subroutine───────────────────*/
dot: procedure; parse arg x1 x2 x3,y1 y2 y3 /*the DOT product.*/
return x1*y1 + x2*y2 + x3*y3 /*a scaler quantity.*/
/*─────────────────────────────────────tellV subroutine─────────────────*/
tellV: procedure; parse arg name,x y z /*display the vector*/
w=max(4,length(x),length(y),length(z)) /*max width of nums.*/
say right(name,40) right(x,w) right(y,w) right(z,w)
call tellV ' dot product [AB] =', dot(a, b)
call tellV 'cross product [AxB] =', cross(a, b)
call tellV 'scalar triple product [A(BxC)] =', dot(a, cross(b, c) )
call tellV 'vector triple product [Ax(BxC)] =', cross(a, cross(b, c) )
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
cross: procedure; parse arg x1 x2 x3,y1 y2 y3 /*the CROSS product.*/
return x2*y3-x3*y2 x3*y1-x1*y3 x1*y2-x2*y1 /*a vector quantity. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
dot: procedure; parse arg x1 x2 x3,y1 y2 y3 /*the DOT product.*/
return x1*y1 + x2*y2 + x3*y3 /*a scalar quantity. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tellV: procedure; parse arg name,x y z /*display the vector. */
w=max(4, length(x), length(y), length(z)) /*max width of numbers*/
say right(name, 40) right(x,w) right(y,w) right(z,w) /*enforce alignment. */
return

View file

@ -1,15 +1,6 @@
require 'matrix'
class Vector
def cross_product(v)
unless size == 3 && v.size == 3
raise ArgumentError, "Vectors must have size 3"
end
Vector[self[1] * v[2] - self[2] * v[1],
self[2] * v[0] - self[0] * v[2],
self[0] * v[1] - self[1] * v[0]]
end
def scalar_triple_product(b, c)
self.inner_product(b.cross_product c)
end