September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -28,8 +28,8 @@ Given the 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 scalar triple product.
# Compute and display: <code>a x b x c</code>, the vector 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:

View file

@ -0,0 +1,21 @@
-module(vector).
-export([main/0]).
vector_product(X,Y)->
[X1,X2,X3]=X,
[Y1,Y2,Y3]=Y,
Ans=[X2*Y3-X3*Y2,X3*Y1-X1*Y3,X1*Y2-X2*Y1],
Ans.
dot_product(X,Y)->
[X1,X2,X3]=X,
[Y1,Y2,Y3]=Y,
Ans=X1*Y1+X2*Y2+X3*Y3,
io:fwrite("~p~n",[Ans]).
main()->
{ok, A} = io:fread("Enter vector A : ", "~d ~d ~d"),
{ok, B} = io:fread("Enter vector B : ", "~d ~d ~d"),
{ok, C} = io:fread("Enter vector C : ", "~d ~d ~d"),
dot_product(A,B),
Ans=vector_product(A,B),
io:fwrite("~p,~p,~p~n",Ans),
dot_product(C,vector_product(A,B)),
io:fwrite("~p,~p,~p~n",vector_product(C,vector_product(A,B))).

View file

@ -0,0 +1,13 @@
S" fsl-util.fs" REQUIRED
: 3f! 3 SWAP }fput ;
: vector
CREATE
HERE 3 DUP FLOAT DUP , * ALLOT SWAP CELL+ }fput
DOES>
CELL+ ;
: >fx@ 0 } F@ ;
: >fy@ 1 } F@ ;
: >fz@ 2 } F@ ;
: .Vector 3 SWAP }fprint ;
0e 0e 0e vector pad \ NB: your system will be non-standard after this line
\ From here on is identical to the above example

View file

@ -1,32 +1,53 @@
import Data.Monoid ((<>))
type Vector a = [a]
type Scalar a = a
a,b,c,d :: Vector Int
a = [ 3, 4, 5 ]
b = [ 4, 3, 5 ]
c = [-5,-12,-13 ]
d = [ 3, 4, 5, 6 ]
a, b, c, d :: Vector Int
a = [3, 4, 5]
dot :: (Num t) => Vector t -> Vector t -> Scalar t
dot u v | length u == length v = sum $ zipWith (*) u v
| otherwise = error "Dotted Vectors must be of equal dimension."
b = [4, 3, 5]
cross :: (Num t) => Vector t -> Vector t -> Vector t
cross u v | length u == 3 && length v == 3 =
[u !! 1 * v !! 2 - u !! 2 * v !! 1,
u !! 2 * v !! 0 - u !! 0 * v !! 2,
u !! 0 * v !! 1 - u !! 1 * v !! 0]
| otherwise = error "Crossed Vectors must both be three dimensional."
c = [-5, -12, -13]
scalarTriple :: (Num t) => Vector t -> Vector t -> Vector t -> Scalar t
d = [3, 4, 5, 6]
dot
:: (Num t)
=> Vector t -> Vector t -> Scalar t
dot u v
| length u == length v = sum $ zipWith (*) u v
| otherwise = error "Dotted Vectors must be of equal dimension."
cross
:: (Num t)
=> Vector t -> Vector t -> Vector t
cross u v
| length u == 3 && length v == 3 =
[ u !! 1 * v !! 2 - u !! 2 * v !! 1
, u !! 2 * head v - head u * v !! 2
, head u * v !! 1 - u !! 1 * head v
]
| otherwise = error "Crossed Vectors must both be three dimensional."
scalarTriple
:: (Num t)
=> Vector t -> Vector t -> Vector t -> Scalar t
scalarTriple q r s = dot q $ cross r s
vectorTriple :: (Num t) => Vector t -> Vector t -> Vector t -> Vector t
vectorTriple
:: (Num t)
=> Vector t -> Vector t -> Vector t -> Vector t
vectorTriple q r s = cross q $ cross r s
main = do
mapM_ putStrLn [ "a . b = " ++ (show $ dot a b)
, "a x b = " ++ (show $ cross a b)
, "a . b x c = " ++ (show $ scalarTriple a b c)
, "a x b x c = " ++ (show $ vectorTriple a b c)
, "a . d = " ++ (show $ dot a d) ]
main :: IO ()
main =
mapM_
putStrLn
[ "a . b = " <> show (dot a b)
, "a x b = " <> show (cross a b)
, "a . b x c = " <> show (scalarTriple a b c)
, "a x b x c = " <> show (vectorTriple a b c)
, "a . d = " <> show (dot a d)
]

View file

@ -0,0 +1,28 @@
// version 1.1.2
class Vector3D(val x: Double, val y: Double, val z: Double) {
infix fun dot(v: Vector3D) = x * v.x + y * v.y + z * v.z
infix fun cross(v: Vector3D) =
Vector3D(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x)
fun scalarTriple(v: Vector3D, w: Vector3D) = this dot (v cross w)
fun vectorTriple(v: Vector3D, w: Vector3D) = this cross (v cross w)
override fun toString() = "($x, $y, $z)"
}
fun main(args: Array<String>) {
val a = Vector3D(3.0, 4.0, 5.0)
val b = Vector3D(4.0, 3.0, 5.0)
val c = Vector3D(-5.0, -12.0, -13.0)
println("a = $a")
println("b = $b")
println("c = $c")
println()
println("a . b = ${a dot b}")
println("a x b = ${a cross b}")
println("a . b x c = ${a.scalarTriple(b, c)}")
println("a x b x c = ${a.vectorTriple(b, c)}")
}

View file

@ -0,0 +1,49 @@
a = .vector~new(3, 4, 5);
b = .vector~new(4, 3, 5);
c = .vector~new(-5, -12, -13);
say a~dot(b)
say a~cross(b)
say a~scalarTriple(b, c)
say a~vectorTriple(b, c)
::class vector
::method init
expose x y z
use arg x, y, z
::attribute x get
::attribute y get
::attribute z get
-- dot product operation
::method dot
expose x y z
use strict arg other
return x * other~x + y * other~y + z * other~z
-- cross product operation
::method cross
expose x y z
use strict arg other
newX = y * other~z - z * other~y
newY = z * other~x - x * other~z
newZ = x * other~y - y * other~x
return self~class~new(newX, newY, newZ)
-- scalar triple product
::method scalarTriple
use strict arg vectorB, vectorC
return self~dot(vectorB~cross(vectorC))
-- vector triple product
::method vectorTriple
use strict arg vectorB, vectorC
return self~cross(vectorB~cross(vectorC))
::method string
expose x y z
return "<"||x", "y", "z">"

View file

@ -1,7 +1,7 @@
/*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"
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 " " " */
@ -12,13 +12,10 @@ 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. */
cross: procedure; arg $1 $2 $3,@1 @2 @3; return $2*@3 -$3*@2 $3*@1 -$1*@3 $1*@2 -$2*@1
dot: procedure; arg $1 $2 $3,@1 @2 @3; return $1*@1 + $2*@2 + $3*@3
/*──────────────────────────────────────────────────────────────────────────────────────*/
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
tellV: procedure; parse arg name,x y z /*obtain name, values.*/
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 /* [↑] display vector*/

View file

@ -0,0 +1,35 @@
# Project : Vector products
# Date : 2017/09/21
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
d = list(3)
e = list(3)
a = [3, 4, 5]
b = [4, 3, 5]
c = [-5, -12, -13]
see "a . b = " + dot(a,b) + nl
cross(a,b,d)
see "a x b = (" + d[1] + ", " + d[2] + ", " + d[3] + ")" + nl
see "a . (b x c) = " + scalartriple(a,b,c) + nl
vectortriple(a,b,c,d)
def dot(a,b)
sum = 0
for n=1 to len(a)
sum = sum + a[n]*b[n]
next
return sum
func cross(a,b,d)
d = [a[2]*b[3]-a[3]*b[2], a[3]*b[1]-a[1]*b[3], a[1]*b[2]-a[2]*b[1]]
func scalartriple(a,b,c)
cross(b,c,d)
return dot(a,d)
func vectortriple(a,b,c,d)
cross(b,c,d)
cross(a,d,e)
see "a x (b x c) = (" + e[1] + ", " +e[2] + ", " + e[3] + ")"

View file

@ -0,0 +1,45 @@
#[derive(Debug)]
struct Vector {
x: f64,
y: f64,
z: f64,
}
impl Vector {
fn new(x: f64, y: f64, z: f64) -> Self {
Vector {
x: x,
y: y,
z: z,
}
}
fn dot_product(&self, other: &Vector) -> f64 {
(self.x * other.x) + (self.y * other.y) + (self.z * other.z)
}
fn cross_product(&self, other: &Vector) -> Vector {
Vector::new(self.y * other.z - self.z * other.y,
self.z * other.x - self.x * other.z,
self.x * other.y - self.y * other.x)
}
fn scalar_triple_product(&self, b: &Vector, c: &Vector) -> f64 {
self.dot_product(&b.cross_product(&c))
}
fn vector_triple_product(&self, b: &Vector, c: &Vector) -> Vector {
self.cross_product(&b.cross_product(&c))
}
}
fn main(){
let a = Vector::new(3.0, 4.0, 5.0);
let b = Vector::new(4.0, 3.0, 5.0);
let c = Vector::new(-5.0, -12.0, -13.0);
println!("a . b = {}", a.dot_product(&b));
println!("a x b = {:?}", a.cross_product(&b));
println!("a . (b x c) = {}", a.scalar_triple_product(&b, &c));
println!("a x (b x c) = {:?}", a.vector_triple_product(&b, &c));
}

View file

@ -0,0 +1,43 @@
mata
real scalar sprod(real colvector u, real colvector v) {
return(u[1]*v[1] + u[2]*v[2] + u[3]*v[3])
}
real colvector vprod(real colvector u, real colvector v) {
return(u[2]*v[3]-u[3]*v[2]\u[3]*v[1]-u[1]*v[3]\u[1]*v[2]-u[2]*v[1])
}
real scalar striple(real colvector u, real colvector v, real colvector w) {
return(sprod(u, vprod(v, w)))
}
real colvector vtriple(real colvector u, real colvector v, real colvector w) {
return(vprod(u, vprod(v, w)))
}
a = 3\4\5
b = 4\3\5
c = -5\-12\-13
sprod(a, b)
49
vprod(a, b)
1
+------+
1 | 5 |
2 | 5 |
3 | -7 |
+------+
striple(a, b, c)
6
vtriple(a, b, c)
1
+--------+
1 | -267 |
2 | 204 |
3 | -3 |
+--------+
end

View file

@ -0,0 +1,3 @@
fcn dotp(a,b){ a.zipWith('*,b).sum() } //1 slow but concise
fcn crossp([(a1,a2,a3)],[(b1,b2,b3)]) //2
{ return(a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1) }

View file

@ -0,0 +1,5 @@
a,b,c := T(3,4,5), T(4,3,5), T(-5,-12,-13);
dotp(a,b).println(); //5 --> 49
crossp(a,b).println(); //6 --> (5,5,-7)
dotp(a, crossp(b,c)).println(); //7 --> 6
crossp(a, crossp(b,c)).println(); //8 --> (-267,204,-3)

View file

@ -0,0 +1,11 @@
var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
a:=GSL.VectorFromData( 3, 4, 5);
b:=GSL.VectorFromData( 4, 3, 5);
c:=GSL.VectorFromData(-5,-12,-13);
(a*b).println(); // 49, dot product
a.copy().crossProduct(b) // (5,5,-7) cross product, in place
.format().println();
(a*(b.copy().crossProduct(c))).println(); // 6 scalar triple product
(a.crossProduct(b.crossProduct(c))) // (-267,204,-3) vector triple product, in place
.format().println();