Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -1,4 +1,31 @@
|
|||
A vector is defined as having three dimensions as being represented by an ordered collection of three numbers: (X, Y, Z).
|
||||
;Related tasks:
|
||||
* [[Arrays]]
|
||||
* [[Vector]]
|
||||
** [[Dot product]]
|
||||
** [[Vector products]]
|
||||
*** A starting page on Wolfram MathWorld is {{Wolfram|Vector|Multiplication}}.
|
||||
*** Wikipedia [[wp:Dot product|dot product]].
|
||||
*** Wikipedia [[wp:Cross product|cross product]].
|
||||
*** Wikipedia [[wp:Triple product|triple product]].
|
||||
*** Wikipedia [[wp:Hodge star operator|hodge star operator]]
|
||||
*** Wikipedia [[wp:Inner product space|inner product space]]
|
||||
*** Wikipedia [[wp:Outer product|outer product]]
|
||||
*** Wikipedia [[wp:Interior product|interior product]]
|
||||
*** Wikipedia [[wp:Exterior product|exterior product]]
|
||||
*** Wikipedia [[wp:Wedge product|wedge product]]
|
||||
*** Wikipedia [[wp:Curry product|curry product]]
|
||||
*** Wikipedia [[wp:Pfaffian product|pfaffian product]]
|
||||
* [[Matrices]]
|
||||
* [[Bivector]]
|
||||
* [[Antivector]]
|
||||
* [[Tensor]]
|
||||
* [[Quaternion]]
|
||||
* [[Rotor]]
|
||||
* [[Motor]]
|
||||
* [[Sedenion]]
|
||||
* [[Octonion]]
|
||||
<br>
|
||||
A vector is defined as having three dimensions as being represented by an ordered collection of '''n''' numbers: i.e. for '''n'''='''3''' : (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.
|
||||
|
||||
|
|
@ -31,23 +58,3 @@ Given the three vectors:
|
|||
# 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:
|
||||
* A starting page on Wolfram MathWorld is {{Wolfram|Vector|Multiplication}}.
|
||||
* Wikipedia [[wp:Dot product|dot product]].
|
||||
* Wikipedia [[wp:Cross product|cross product]].
|
||||
* Wikipedia [[wp:Triple product|triple product]].
|
||||
* Wikipedia [[wp:Hodge star operator|hodge star operator]]
|
||||
* Wikipedia [[wp:Inner product space|inner product space]]
|
||||
* Wikipedia [[wp:Outer product|outer product]]
|
||||
* Wikipedia [[wp:Interior product|interior product]]
|
||||
* Wikipedia [[wp:Exterior product|exterior product]]
|
||||
* Wikipedia [[wp:Wedge product|wedge product]]
|
||||
* Wikipedia [[wp:Curry product|curry product]]
|
||||
* Wikipedia [[wp:Pfaffian product|pfaffian product]]
|
||||
|
||||
;Related tasks:
|
||||
* [[Dot product]]
|
||||
* [[Quaternion type]]
|
||||
<br><br>
|
||||
|
||||
|
|
|
|||
78
Task/Vector-products/Ada/vector-products.adb
Normal file
78
Task/Vector-products/Ada/vector-products.adb
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Vector is
|
||||
type Float_Vector is array (Positive range <>) of Float;
|
||||
package Float_IO is new Ada.Text_IO.Float_IO (Float);
|
||||
|
||||
procedure Vector_Put (X : Float_Vector) is
|
||||
begin
|
||||
Ada.Text_IO.Put ("(");
|
||||
for I in X'Range loop
|
||||
Float_IO.Put (X (I), Aft => 1, Exp => 0);
|
||||
if I /= X'Last then
|
||||
Ada.Text_IO.Put (", ");
|
||||
end if;
|
||||
end loop;
|
||||
Ada.Text_IO.Put (")");
|
||||
end Vector_Put;
|
||||
|
||||
-- cross product
|
||||
function "*" (Left, Right : Float_Vector) return Float_Vector is
|
||||
begin
|
||||
if Left'Length /= Right'Length then
|
||||
raise Constraint_Error with "vectors of different size in dot product";
|
||||
end if;
|
||||
if Left'Length /= 3 then
|
||||
raise Constraint_Error with "dot product only implemented for R**3";
|
||||
end if;
|
||||
return Float_Vector'(Left (Left'First + 1) * Right (Right'First + 2) -
|
||||
Left (Left'First + 2) * Right (Right'First + 1),
|
||||
Left (Left'First + 2) * Right (Right'First) -
|
||||
Left (Left'First) * Right (Right'First + 2),
|
||||
Left (Left'First) * Right (Right'First + 1) -
|
||||
Left (Left'First + 1) * Right (Right'First));
|
||||
end "*";
|
||||
|
||||
-- scalar product
|
||||
function "*" (Left, Right : Float_Vector) return Float is
|
||||
Result : Float := 0.0;
|
||||
I, J : Positive;
|
||||
begin
|
||||
if Left'Length /= Right'Length then
|
||||
raise Constraint_Error with "vectors of different size in scalar product";
|
||||
end if;
|
||||
I := Left'First; J := Right'First;
|
||||
while I <= Left'Last and then J <= Right'Last loop
|
||||
Result := Result + Left (I) * Right (J);
|
||||
I := I + 1; J := J + 1;
|
||||
end loop;
|
||||
return Result;
|
||||
end "*";
|
||||
|
||||
-- stretching
|
||||
function "*" (Left : Float_Vector; Right : Float) return Float_Vector is
|
||||
Result : Float_Vector (Left'Range);
|
||||
begin
|
||||
for I in Left'Range loop
|
||||
Result (I) := Left (I) * Right;
|
||||
end loop;
|
||||
return Result;
|
||||
end "*";
|
||||
|
||||
A : constant Float_Vector := (3.0, 4.0, 5.0);
|
||||
B : constant Float_Vector := (4.0, 3.0, 5.0);
|
||||
C : constant Float_Vector := (-5.0, -12.0, -13.0);
|
||||
begin
|
||||
Ada.Text_IO.Put ("A: "); Vector_Put (A); Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("B: "); Vector_Put (B); Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("C: "); Vector_Put (C); Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("A dot B = "); Float_IO.Put (A * B, Aft => 1, Exp => 0);
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("A x B = "); Vector_Put (A * B);
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("A dot (B x C) = "); Float_IO.Put (A * (B * C), Aft => 1, Exp => 0);
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("A x (B x C) = "); Vector_Put (A * Float_Vector'(B * C));
|
||||
Ada.Text_IO.New_Line;
|
||||
end Vector;
|
||||
23
Task/Vector-products/C++/vector-products-1.cpp
Normal file
23
Task/Vector-products/C++/vector-products-1.cpp
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include "vec.hpp"
|
||||
|
||||
using i32x3 = vec<int,3>;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
i32x3 a = { 3 , 4 , 5 };
|
||||
i32x3 b = { 4 , 3 , 5 };
|
||||
i32x3 c = { -5, -12, -13 };
|
||||
|
||||
i32x3 d = cross3(a,b);
|
||||
i32x3 e = triplevec3(a,b,c);
|
||||
|
||||
a.println("a = ");
|
||||
b.println("b = ");
|
||||
c.println("c = "); eol();
|
||||
|
||||
printf(" (a . b) = %d", dot(a,b)); eol();
|
||||
d.println(" (a x b) = ");
|
||||
printf("a . (b x c) = %d", triplesca3(a,b,c)); eol();
|
||||
e.println("a x (b x c) = ");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
135
Task/Vector-products/C++/vector-products-2.cpp
Normal file
135
Task/Vector-products/C++/vector-products-2.cpp
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
#pragma once
|
||||
/* -std=<c|gnu>++26
|
||||
* -march=native
|
||||
* -mfpmath=<your simd>
|
||||
* -O3
|
||||
* -ftree-vectorize -fopenmp-simd
|
||||
* -ffunction-sections -fdata-sections
|
||||
* -Wl,--gc-sections -Wl,--print-gc-sections -Wl,-s
|
||||
*
|
||||
* For MSVC see: https://learn.microsoft.com/en-us/cpp/parallel/openmp/openmp-simd?view=msvc-180
|
||||
*
|
||||
* Uses cstdio instead of iostream to avoid binary size increase.
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
#include <array>
|
||||
#include <bit>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define FORCE_INLINE __forceinline
|
||||
#define FLATTEN __flatten
|
||||
#else
|
||||
#define FORCE_INLINE __attribute__((always_inline))
|
||||
#define FLATTEN __attribute__((flatten))
|
||||
#endif
|
||||
|
||||
#ifdef __x86_64__
|
||||
#define REGPARM __attribute__((sseregparm))
|
||||
#elif __defined__(__i386__)
|
||||
#define REGPARM __attribute__((regparm(8)))
|
||||
#else
|
||||
#define REGPARM
|
||||
#endif
|
||||
|
||||
/*
|
||||
* end of line with operating system specific newline character
|
||||
* used as a replacement for std::cout << std::endl;
|
||||
*/
|
||||
inline constexpr FORCE_INLINE FLATTEN void eol(FILE* stream = stdout) { fputs("\n", stream); }
|
||||
|
||||
enum class alg
|
||||
{
|
||||
unk = 0,
|
||||
sca = 1,
|
||||
vec = 2,
|
||||
std = 3
|
||||
};
|
||||
|
||||
template< typename T, size_t N, enum alg A = alg::std, size_t N_POW2 = std::bit_ceil<size_t>(N)>
|
||||
struct alignas(((N == N_POW2) || (A == alg::vec) ? N_POW2 : 1) * alignof(T)) vec : std::array<T,N>
|
||||
{
|
||||
template<enum alg A_DST = alg::std>
|
||||
inline FORCE_INLINE FLATTEN operator vec<T,N,A_DST>() { return *reinterpret_cast<vec<T,N,A_DST>*>(this); }
|
||||
|
||||
/* permute vector according to input indices */
|
||||
template<typename... I>
|
||||
inline FORCE_INLINE FLATTEN constexpr vec<T, sizeof...(I),A> permute(const I... args) const { return vec<T,sizeof...(I),A>{ (*this)[args % N]... }; }
|
||||
|
||||
template<typename T_RHS, size_t N_RHS, enum alg A_RHS = alg::std>
|
||||
inline FORCE_INLINE FLATTEN vec<T,N>& operator-=(const vec<T_RHS,N_RHS>& rhs)
|
||||
{
|
||||
#pragma omp simd
|
||||
for(size_t i = 0; i < std::min<size_t>(N,N_RHS); i++)
|
||||
(*this)[i] -= rhs[i];
|
||||
return (*this);
|
||||
}
|
||||
|
||||
template<typename T_RHS, size_t N_RHS, enum alg A_RHS = alg::std>
|
||||
inline FORCE_INLINE FLATTEN vec<T,N>& operator*=(const vec<T_RHS,N_RHS>& rhs)
|
||||
{
|
||||
#pragma omp simd
|
||||
for(size_t i = 0; i < std::min<size_t>(N,N_RHS); i++)
|
||||
(*this)[i] *= rhs[i];
|
||||
return (*this);
|
||||
}
|
||||
inline FORCE_INLINE FLATTEN void print(const char* prefix = "", const char* suffix = "", size_t n = N, FILE* stream = stdout)
|
||||
{
|
||||
n = std::min(n,N);
|
||||
fprintf(stream, "%s[", prefix);
|
||||
for(size_t i = 0; i < n; i++)
|
||||
fprintf(stream, "%+e%s", (double)(*this)[i], i != (n - 1) ? " " : "]");
|
||||
fprintf(stream, "%s", suffix);
|
||||
}
|
||||
inline FORCE_INLINE FLATTEN void println(const char* prefix = "", const char* suffix = "", size_t n = N, FILE* stream = stdout)
|
||||
{
|
||||
print(prefix,suffix,n,stream);
|
||||
eol():
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<typename T_LHS, size_t N_LHS, enum alg A_LHS = alg::std, typename T_RHS, size_t N_RHS, enum alg A_RHS = A_LHS, typename T_DST = decltype((T_LHS)1 - (T_RHS)1), size_t N_DST = std::max(N_LHS,N_RHS)>
|
||||
constexpr inline FORCE_INLINE FLATTEN vec<T_DST,N_DST> operator-(const vec<T_LHS, N_LHS>& lhs, const vec<T_RHS,N_RHS>& rhs)
|
||||
{
|
||||
vec<T_DST, N_DST> dst{lhs};
|
||||
dst -= rhs;
|
||||
return dst;
|
||||
}
|
||||
template<typename T_LHS, size_t N_LHS, enum alg A_LHS = alg::std, typename T_RHS, size_t N_RHS, enum alg A_RHS = A_LHS, typename T_DST = decltype((T_LHS)1 * (T_RHS)1), size_t N_DST = std::max(N_LHS,N_RHS)>
|
||||
constexpr inline FORCE_INLINE FLATTEN vec<T_DST,N_DST> operator*(const vec<T_LHS, N_LHS>& lhs, const vec<T_RHS,N_RHS>& rhs)
|
||||
{
|
||||
vec<T_DST, N_DST> dst{lhs};
|
||||
dst *= rhs;
|
||||
return dst;
|
||||
}
|
||||
|
||||
template<typename T_LHS, size_t N_LHS, enum alg A_LHS, typename T_RHS, size_t N_RHS, enum alg A_RHS = A_LHS, typename T_DST = decltype((T_LHS)1 * (T_RHS)1 - (T_LHS)1 * (T_RHS)1)>
|
||||
constexpr inline FORCE_INLINE FLATTEN vec<T_DST, 3> cross3(const vec<T_LHS, N_LHS, A_LHS>& lhs, const vec<T_RHS,N_RHS,A_RHS>& rhs) requires((N_LHS > 2) && (N_RHS > 2))
|
||||
{
|
||||
return vec<T_DST,3>{ lhs.permute(1,2,0) * rhs.permute(2,0,1)
|
||||
- rhs.permute(1,2,0) * lhs.permute(2,0,1) };
|
||||
}
|
||||
template<typename T_LHS, size_t N_LHS, enum alg A_LHS, typename T_RHS, size_t N_RHS, enum alg A_RHS = A_LHS, typename T_DST = decltype((T_LHS)1 * (T_RHS)1 + (T_LHS)1 * (T_RHS)1)>
|
||||
inline FORCE_INLINE FLATTEN T_DST dot(const vec<T_LHS, N_LHS, A_LHS>& lhs, const vec<T_RHS,N_RHS,A_RHS>& rhs)
|
||||
{
|
||||
T_DST dst;
|
||||
#pragma omp simd reduction(+:dst)
|
||||
for(size_t i = 0; i < std::min(N_LHS, N_RHS); i++)
|
||||
dst += lhs[i] * rhs[i];
|
||||
return dst;
|
||||
}
|
||||
|
||||
template<typename T_LHS, size_t N_LHS, enum alg A_LHS, typename T_B, size_t N_B, enum alg A_B = alg::std, typename T_RHS, size_t N_RHS, enum alg A_RHS = A_LHS, typename T_DST = decltype((T_LHS)1 * (T_RHS)1 - (T_B)1 * (T_RHS)1)>
|
||||
constexpr inline FORCE_INLINE FLATTEN vec<T_DST, 3> triplevec3(const vec<T_LHS, N_LHS, A_LHS>& lhs, const vec<T_B,N_B,A_B>& b, const vec<T_RHS,N_RHS,A_RHS>& rhs) requires((N_LHS > 2) && (N_RHS > 2) && (N_B > 2))
|
||||
{
|
||||
return cross3(lhs,cross3(b,rhs));
|
||||
}
|
||||
|
||||
template<typename T_LHS, size_t N_LHS, enum alg A_LHS, typename T_B, size_t N_B, enum alg A_B = alg::std, typename T_RHS, size_t N_RHS, enum alg A_RHS = A_LHS, typename T_DST = decltype((T_LHS)1 * (T_B)1 - (T_RHS)1 * (T_LHS)1)>
|
||||
constexpr inline FORCE_INLINE FLATTEN T_DST triplesca3(const vec<T_LHS, N_LHS, A_LHS>& lhs, const vec<T_B,N_B,A_B>& b, const vec<T_RHS,N_RHS,A_RHS>& rhs) requires((N_LHS > 2) && (N_RHS > 2) && (N_B > 2))
|
||||
{
|
||||
return dot(lhs,cross3(b,rhs));
|
||||
}
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
#include <iostream>
|
||||
|
||||
template< class T >
|
||||
class D3Vector {
|
||||
|
||||
template< class U >
|
||||
friend std::ostream & operator<<( std::ostream & , const D3Vector<U> & ) ;
|
||||
|
||||
public :
|
||||
D3Vector( T a , T b , T c ) {
|
||||
x = a ;
|
||||
y = b ;
|
||||
z = c ;
|
||||
}
|
||||
|
||||
T dotproduct ( const D3Vector & rhs ) {
|
||||
T scalar = x * rhs.x + y * rhs.y + z * rhs.z ;
|
||||
return scalar ;
|
||||
}
|
||||
|
||||
D3Vector crossproduct ( const D3Vector & rhs ) {
|
||||
T a = y * rhs.z - z * rhs.y ;
|
||||
T b = z * rhs.x - x * rhs.z ;
|
||||
T c = x * rhs.y - y * rhs.x ;
|
||||
D3Vector product( a , b , c ) ;
|
||||
return product ;
|
||||
}
|
||||
|
||||
D3Vector triplevec( D3Vector & a , D3Vector & b ) {
|
||||
return crossproduct ( a.crossproduct( b ) ) ;
|
||||
}
|
||||
|
||||
T triplescal( D3Vector & a, D3Vector & b ) {
|
||||
return dotproduct( a.crossproduct( b ) ) ;
|
||||
}
|
||||
|
||||
private :
|
||||
T x , y , z ;
|
||||
} ;
|
||||
|
||||
template< class T >
|
||||
std::ostream & operator<< ( std::ostream & os , const D3Vector<T> & vec ) {
|
||||
os << "( " << vec.x << " , " << vec.y << " , " << vec.z << " )" ;
|
||||
return os ;
|
||||
}
|
||||
|
||||
int main( ) {
|
||||
D3Vector<int> a( 3 , 4 , 5 ) , b ( 4 , 3 , 5 ) , c( -5 , -12 , -13 ) ;
|
||||
std::cout << "a . b : " << a.dotproduct( b ) << "\n" ;
|
||||
std::cout << "a x b : " << a.crossproduct( b ) << "\n" ;
|
||||
std::cout << "a . b x c : " << a.triplescal( b , c ) << "\n" ;
|
||||
std::cout << "a x b x c : " << a.triplevec( b , c ) << "\n" ;
|
||||
return 0 ;
|
||||
}
|
||||
26
Task/Vector-products/C/vector-products-1.c
Normal file
26
Task/Vector-products/C/vector-products-1.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#include "vec.h"
|
||||
|
||||
#define out_tst(a,b,c) \
|
||||
({ \
|
||||
out_vec(" a =", (a) ,3); out_end(); \
|
||||
out_vec(" b =", (b) ,3); out_end(); \
|
||||
out_vec(" c =", (c) ,3); out_end(); \
|
||||
out_vec(" a . b =", broadcast(3,dot3((a),(b))) ,3); out_end(); \
|
||||
out_vec(" a x b =", cross3((a),(b)) ,3); out_end(); \
|
||||
out_vec("a . (b x c) =", broadcast(3,dot3((a),cross3((b),(c)))),3); out_end(); \
|
||||
out_vec("a x (b x c) =", cross3((a),cross3((b),(c))) ,3); out_end(); \
|
||||
out_vec(" (a x 1) =", cross2((a),0x901),2); out_end(); \
|
||||
out_vec(" (a x 0) =", cross2((a),0x900),2); out_end(); \
|
||||
out_vec(" (b x 1) =", cross2((b),0x901),2); out_end(); \
|
||||
out_vec(" (b x 0) =", cross2((b),0x900),2); out_end(); \
|
||||
})
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
vec(flt,3) a = { 3, 4, 5 };
|
||||
vec(flt,3) b = { 4, 3, 5 };
|
||||
vec(flt,3) c = { -5, -12, -13 };
|
||||
|
||||
out_tst(a,b,c);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
98
Task/Vector-products/C/vector-products-2.c
Normal file
98
Task/Vector-products/C/vector-products-2.c
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
#pragma once
|
||||
|
||||
#include<stdio.h>
|
||||
#include<stdint.h>
|
||||
#include<stdlib.h>
|
||||
#include<stddef.h>
|
||||
#include<stdbool.h>
|
||||
#include<stdalign.h>
|
||||
#include<stdarg.h>
|
||||
#include<sys/param.h>
|
||||
#include<math.h>
|
||||
|
||||
/* default floating point scalar */
|
||||
typedef double flt;
|
||||
#define pi M_PI
|
||||
|
||||
#define PARENS ()
|
||||
#define EXPAND(...) EXPAND4(EXPAND4(EXPAND4(EXPAND4(__VA_ARGS__))))
|
||||
#define EXPAND4(...) EXPAND3(EXPAND3(EXPAND3(EXPAND3(__VA_ARGS__))))
|
||||
#define EXPAND3(...) EXPAND2(EXPAND2(EXPAND2(EXPAND2(__VA_ARGS__))))
|
||||
#define EXPAND2(...) EXPAND1(EXPAND1(EXPAND1(EXPAND1(__VA_ARGS__))))
|
||||
#define EXPAND1(...) __VA_ARGS__
|
||||
|
||||
#undef countof
|
||||
#define countof(x) sizeof(typeof((x)))/sizeof(typeof((x)[0]))
|
||||
#define cnt(...) sizeof((typeof(__VA_ARGS__)[]){__VA_ARGS__})/sizeof(__VA_ARGS__)
|
||||
|
||||
#define BITOP_RUP01__(x) ( (x) | ( (x) >> 1))
|
||||
#define BITOP_RUP02__(x) (BITOP_RUP01__(x) | (BITOP_RUP01__(x) >> 2))
|
||||
#define BITOP_RUP04__(x) (BITOP_RUP02__(x) | (BITOP_RUP02__(x) >> 4))
|
||||
#define BITOP_RUP08__(x) (BITOP_RUP04__(x) | (BITOP_RUP04__(x) >> 8))
|
||||
#define BITOP_RUP16__(x) (BITOP_RUP08__(x) | (BITOP_RUP08__(x) >> 16))
|
||||
|
||||
|
||||
#define BIT_CEIL(x) (BITOP_RUP16__(((uint32_t)(x)) - 1) + 1)
|
||||
|
||||
#if defined(__clang__)
|
||||
#define vec(T,N) typeof(T __attribute__((ext_vector_type(N))))
|
||||
#elif defined(__GNUC__)
|
||||
#define vec(T,N) typeof(T __attribute__((vector_size(sizeof(T) * BIT_CEIL(N)))))
|
||||
#elif defined(_MSC_VER)
|
||||
#define vec(T,N) typeof(T __declspec((align(sizeof(T)*BIT_CEIL(N))))[BIT_CEIL(N)])
|
||||
#warn "Your compiler doens't support vector extensions."
|
||||
#warn "Using aligned arrays without operators instead."
|
||||
#else
|
||||
#define vec(T,N) typeof(T __attribute__((aligned(sizeof(T)*BIT_CEIL(N))))[BIT_CEIL(N)])
|
||||
#warn "Your compiler doens't support vector extensions."
|
||||
#warn "Using aligned arrays without operators instead."
|
||||
#endif
|
||||
|
||||
#define perm(a,...) { __VA_OPT__(EXPAND(perm_helper(a,__VA_ARGS__))) }
|
||||
#define perm_helper(a,i,...) (a)[i], __VA_OPT__(perm_again PARENS (a,__VA_ARGS__))
|
||||
#define perm_again() perm_helper
|
||||
#define perm2(a,...) ((vec(typeof((a)[0]),2))perm((a),__VA_ARGS__))
|
||||
#define perm3(a,...) ((vec(typeof((a)[0]),3))perm((a),__VA_ARGS__))
|
||||
#define perm4(a,...) ((vec(typeof((a)[0]),4))perm((a),__VA_ARGS__))
|
||||
#define broadcast(n,x) (vec(typeof((a)[0]),n))(x - (vec(typeof((a)[0]),n)){})
|
||||
|
||||
#define dot(a,b,t,n,i) \
|
||||
({ \
|
||||
t dst = (t)i; \
|
||||
_Pragma("omp simd reduction(+:dst)") \
|
||||
for(size_t j = 0; j < MIN(MIN(countof(a),countof(b)),n); j++) \
|
||||
dst += (t)((a)[j] * (b)[j]); \
|
||||
dst; \
|
||||
})
|
||||
|
||||
/* default dot products for length 3/4 */
|
||||
#define dot3(a,b) (dot((a),(b),flt,3,0))
|
||||
#define dot4(a,b) (dot((a),(b),flt,4,0))
|
||||
|
||||
#define negate(a,mod,val) \
|
||||
({ \
|
||||
for(size_t i = 0; i < countof((a)); i++) \
|
||||
(a)[i] = i % mod == val ? -(a)[i] : (a)[i]; \
|
||||
(a); \
|
||||
})
|
||||
|
||||
#define cross2(a,winding) \
|
||||
({ \
|
||||
vec(typeof((a)[0]),2) id = broadcast(2,1); \
|
||||
negate(id,2,0b1^(winding & 0b1)) * perm2(a,1,0); \
|
||||
})
|
||||
|
||||
#define cross3(a,b) \
|
||||
perm3(a,1,2,0) * perm3(b,2,0,1) \
|
||||
- perm3(a,2,0,1) * perm3(b,1,2,0)
|
||||
|
||||
#define out_vec(msg,a,n) \
|
||||
({ \
|
||||
printf("%s [ ", msg); \
|
||||
for(size_t i = 0; i < n; i++) \
|
||||
{ \
|
||||
printf("%+e ", (double)((a)[i])); \
|
||||
if(i == n-1) printf("]"); \
|
||||
} \
|
||||
})
|
||||
#define out_end() puts("");
|
||||
36
Task/Vector-products/Euphoria/vector-products.eu
Normal file
36
Task/Vector-products/Euphoria/vector-products.eu
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
constant X = 1, Y = 2, Z = 3
|
||||
|
||||
function dot_product(sequence a, sequence b)
|
||||
return a[X]*b[X] + a[Y]*b[Y] + a[Z]*b[Z]
|
||||
end function
|
||||
|
||||
function cross_product(sequence a, sequence b)
|
||||
return { a[Y]*b[Z] - a[Z]*b[Y],
|
||||
a[Z]*b[X] - a[X]*b[Z],
|
||||
a[X]*b[Y] - a[Y]*b[X] }
|
||||
end function
|
||||
|
||||
function scalar_triple(sequence a, sequence b, sequence c)
|
||||
return dot_product( a, cross_product( b, c ) )
|
||||
end function
|
||||
|
||||
function vector_triple( sequence a, sequence b, sequence c)
|
||||
return cross_product( a, cross_product( b, c ) )
|
||||
end function
|
||||
|
||||
constant a = { 3, 4, 5 }, b = { 4, 3, 5 }, c = { -5, -12, -13 }
|
||||
|
||||
puts(1,"a = ")
|
||||
? a
|
||||
puts(1,"b = ")
|
||||
? b
|
||||
puts(1,"c = ")
|
||||
? c
|
||||
puts(1,"a dot b = ")
|
||||
? dot_product( a, b )
|
||||
puts(1,"a x b = ")
|
||||
? cross_product( a, b )
|
||||
puts(1,"a dot (b x c) = ")
|
||||
? scalar_triple( a, b, c )
|
||||
puts(1,"a x (b x c) = ")
|
||||
? vector_triple( a, b, c )
|
||||
54
Task/Vector-products/Gleam/vector-products.gleam
Normal file
54
Task/Vector-products/Gleam/vector-products.gleam
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import gleam/float
|
||||
import gleam/io
|
||||
|
||||
pub type Vector3 {
|
||||
Vector3(Float, Float, Float)
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let a = Vector3(3.0, 4.0, 5.0)
|
||||
let b = Vector3(4.0, 3.0, 5.0)
|
||||
let c = Vector3(-5.0, -12.0, -13.0)
|
||||
|
||||
io.println("dot_product(a, b) = " <> dot_product(a, b) |> float.to_string)
|
||||
io.println("cross_product(a, b) = " <> cross_product(a, b) |> to_string)
|
||||
io.println(
|
||||
"scalar_triple_product(a, b) = "
|
||||
<> scalar_triple_product(a, b, c) |> float.to_string,
|
||||
)
|
||||
io.println(
|
||||
"vector_triple_product(a, b) = "
|
||||
<> vector_triple_product(a, b, c) |> to_string,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn dot_product(u: Vector3, v: Vector3) -> Float {
|
||||
let Vector3(a, b, c) = u
|
||||
let Vector3(x, y, z) = v
|
||||
a *. x +. b *. y +. c *. z
|
||||
}
|
||||
|
||||
pub fn cross_product(u: Vector3, v: Vector3) -> Vector3 {
|
||||
let Vector3(a, b, c) = u
|
||||
let Vector3(x, y, z) = v
|
||||
Vector3(b *. z -. c *. y, c *. x -. a *. z, a *. y -. b *. x)
|
||||
}
|
||||
|
||||
pub fn scalar_triple_product(u: Vector3, v: Vector3, w: Vector3) -> Float {
|
||||
dot_product(u, cross_product(v, w))
|
||||
}
|
||||
|
||||
pub fn vector_triple_product(u: Vector3, v: Vector3, w: Vector3) -> Vector3 {
|
||||
cross_product(u, cross_product(v, w))
|
||||
}
|
||||
|
||||
pub fn to_string(v: Vector3) -> String {
|
||||
let Vector3(a, b, c) = v
|
||||
"("
|
||||
<> float.to_string(a)
|
||||
<> ", "
|
||||
<> float.to_string(b)
|
||||
<> ", "
|
||||
<> float.to_string(c)
|
||||
<> ")"
|
||||
}
|
||||
21
Task/Vector-products/OoRexx/vector-products-2.rexx
Normal file
21
Task/Vector-products/OoRexx/vector-products-2.rexx
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
say 'VECTOR PRODUCTS'
|
||||
say .local~version
|
||||
say
|
||||
a = '3 4 5'; b = '4 3 5'; c = '-5 -12 -13'; d = '1 2 3'
|
||||
say 'VALUES'
|
||||
say 'A =' Vect2form(a)
|
||||
say 'B =' Vect2form(b)
|
||||
say 'C =' Vect2form(c)
|
||||
say 'D =' Vect2form(d)
|
||||
say
|
||||
say 'BASIC'
|
||||
say 'A . B =' DotV(a,b)/1 '= dot product'
|
||||
say 'A x B =' Vect2form(CrossV(a,b)) '= cross product'
|
||||
say
|
||||
say 'BONUS'
|
||||
say 'A . (BxC) =' ScalTripV(a,b,c)/1 '= scalar triple product'
|
||||
say 'A x (BxC) =' Vect2form(VectTripV(a,b,c)) '= vector triple product'
|
||||
say '(AxB) . (CxD) =' ScalQuadV(a,b,c,d)/1 '= scalar quadruple product'
|
||||
say '(AxB) x (CxD) =' Vect2form(VectQuadV(a,b,c,d)) '= vector quadruple product'
|
||||
|
||||
::requires math
|
||||
27
Task/Vector-products/PowerShell/vector-products.ps1
Normal file
27
Task/Vector-products/PowerShell/vector-products.ps1
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
function dot-product($a,$b) {
|
||||
$a[0]*$b[0] + $a[1]*$b[1] + $a[2]*$b[2]
|
||||
}
|
||||
|
||||
function cross-product($a,$b) {
|
||||
$v1 = $a[1]*$b[2] - $a[2]*$b[1]
|
||||
$v2 = $a[2]*$b[0] - $a[0]*$b[2]
|
||||
$v3 = $a[0]*$b[1] - $a[1]*$b[0]
|
||||
@($v1,$v2,$v3)
|
||||
}
|
||||
|
||||
function scalar-triple-product($a,$b,$c) {
|
||||
dot-product $a (cross-product $b $c)
|
||||
}
|
||||
|
||||
function vector-triple-product($a,$b) {
|
||||
cross-product $a (cross-product $b $c)
|
||||
}
|
||||
|
||||
$a = @(3, 4, 5)
|
||||
$b = @(4, 3, 5)
|
||||
$c = @(-5, -12, -13)
|
||||
|
||||
"a.b = $(dot-product $a $b)"
|
||||
"axb = $(cross-product $a $b)"
|
||||
"a.(bxc) = $(scalar-triple-product $a $b $c)"
|
||||
"ax(bxc) = $(vector-triple-product $a $b $c)"
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
-- 24 Aug 2025
|
||||
-- 4 Mar 2026
|
||||
include Setting
|
||||
|
||||
say 'VECTOR PRODUCTS'
|
||||
|
|
@ -6,20 +6,21 @@ say version
|
|||
say
|
||||
a = '3 4 5'; b = '4 3 5'; c = '-5 -12 -13'; d = '1 2 3'
|
||||
say 'VALUES'
|
||||
say 'A =' Lst2FormV(a)
|
||||
say 'B =' Lst2FormV(b)
|
||||
say 'C =' Lst2FormV(c)
|
||||
say 'D =' Lst2FormV(d)
|
||||
say 'A =' Vect2form(a)
|
||||
say 'B =' Vect2form(b)
|
||||
say 'C =' Vect2form(c)
|
||||
say 'D =' Vect2form(d)
|
||||
say
|
||||
say 'BASICS'
|
||||
say 'BASIC'
|
||||
say 'A . B =' DotV(a,b)/1 '= dot product'
|
||||
say 'A x B =' Lst2FormV(CrossV(a,b)) '= cross product'
|
||||
say 'A x B =' Vect2form(CrossV(a,b)) '= cross product'
|
||||
say
|
||||
say 'BONUS'
|
||||
say 'A . (BxC) =' ScalTripV(a,b,c)/1 '= scalar triple product'
|
||||
say 'A x (BxC) =' Lst2FormV(VectTripV(a,b,c)) '= vector triple product'
|
||||
say 'A x (BxC) =' Vect2form(VectTripV(a,b,c)) '= vector triple product'
|
||||
say '(AxB) . (CxD) =' ScalQuadV(a,b,c,d)/1 '= scalar quadruple product'
|
||||
say '(AxB) x (CxD) =' Lst2FormV(VectQuadV(a,b,c,d)) '= vector quadruple product'
|
||||
say '(AxB) x (CxD) =' Vect2form(VectQuadV(a,b,c,d)) '= vector quadruple product'
|
||||
exit
|
||||
|
||||
-- Vect2form; XxxV
|
||||
include Math
|
||||
|
|
|
|||
19
Task/Vector-products/Red/vector-products.red
Normal file
19
Task/Vector-products/Red/vector-products.red
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
Red [ "Vector products - hinjolicious" ]
|
||||
|
||||
#include %mylib/pipe-map.red
|
||||
#include %mylib/transpose-zip.Red
|
||||
|
||||
vec-dot: function [a b][(reduce [a b]) |> zip ==> [_m/1 * _m/2] |> sum]
|
||||
vec-x: function [a b][reduce [((a/2 * b/3) - (a/3 * b/2)) ((a/3 * b/1) - (a/1 * b/3)) ((a/1 * b/2) - (a/2 * b/1))]]
|
||||
vec-dot-x: function [a b c][vec-dot a vec-x b c]
|
||||
vec-x-x: function [a b c][vec-x a vec-x b c]
|
||||
|
||||
demo "Dot product" [
|
||||
a: [ 3 4 5]
|
||||
b: [ 4 3 5]
|
||||
c: [-5 -12 -13]
|
||||
]
|
||||
demo "A • B = a1b1 + a2b2 + a3b3" [probe vec-dot a b]
|
||||
demo "A x B = (a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1)" [probe vec-x a b]
|
||||
demo "A • (B x C)" [probe vec-dot-x a b c]
|
||||
demo "A x (B x C)" [probe vec-x-x a b c]
|
||||
Loading…
Add table
Add a link
Reference in a new issue