langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
17
Task/Dot-product/Nemerle/dot-product.nemerle
Normal file
17
Task/Dot-product/Nemerle/dot-product.nemerle
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
using Nemerle.Collections.NCollectionsExtensions;
|
||||
|
||||
module DotProduct
|
||||
{
|
||||
DotProduct(x : array[int], y : array[int]) : int
|
||||
{
|
||||
$[(a * b)|(a, b) in ZipLazy(x, y)].FoldLeft(0, _+_);
|
||||
}
|
||||
|
||||
Main() : void
|
||||
{
|
||||
def arr1 = array[1, 3, -5]; def arr2 = array[4, -2, -1];
|
||||
WriteLine(DotProduct(arr1, arr2));
|
||||
}
|
||||
}
|
||||
21
Task/Dot-product/NetRexx/dot-product.netrexx
Normal file
21
Task/Dot-product/NetRexx/dot-product.netrexx
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref savelog symbols binary
|
||||
|
||||
whatsTheVectorVictor = [[double 1.0, 3.0, -5.0], [double 4.0, -2.0, -1.0]]
|
||||
dotProduct = Rexx dotProduct(whatsTheVectorVictor)
|
||||
say dotProduct.format(null, 2)
|
||||
|
||||
return
|
||||
|
||||
method dotProduct(vec1 = double[], vec2 = double[]) public constant returns double signals IllegalArgumentException
|
||||
if vec1.length \= vec2.length then signal IllegalArgumentException('Vectors must be the same length')
|
||||
|
||||
scalarProduct = double 0.0
|
||||
loop e_ = 0 to vec1.length - 1
|
||||
scalarProduct = vec1[e_] * vec2[e_] + scalarProduct
|
||||
end e_
|
||||
|
||||
return scalarProduct
|
||||
|
||||
method dotProduct(vecs = double[,]) public constant returns double signals IllegalArgumentException
|
||||
return dotProduct(vecs[0], vecs[1])
|
||||
6
Task/Dot-product/OCaml/dot-product-1.ocaml
Normal file
6
Task/Dot-product/OCaml/dot-product-1.ocaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
let dot = List.fold_left2 (fun z x y -> z +. x *. y) 0.
|
||||
|
||||
(*
|
||||
# dot [1.0; 3.0; -5.0] [4.0; -2.0; -1.0];;
|
||||
- : float = 3.
|
||||
*)
|
||||
11
Task/Dot-product/OCaml/dot-product-2.ocaml
Normal file
11
Task/Dot-product/OCaml/dot-product-2.ocaml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
let dot v u =
|
||||
if Array.length v <> Array.length u
|
||||
then invalid_arg "Different array lengths";
|
||||
let times v u =
|
||||
Array.mapi (fun i v_i -> v_i *. u.(i)) v
|
||||
in Array.fold_left (+.) 0. (times v u)
|
||||
|
||||
(*
|
||||
# dot [| 1.0; 3.0; -5.0 |] [| 4.0; -2.0; -1.0 |];;
|
||||
- : float = 3.
|
||||
*)
|
||||
28
Task/Dot-product/Objeck/dot-product.objeck
Normal file
28
Task/Dot-product/Objeck/dot-product.objeck
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
bundle Default {
|
||||
class DotProduct {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
DotProduct([1, 3, -5], [4, -2, -1])->PrintLine();
|
||||
}
|
||||
|
||||
function : DotProduct(array_a : Int[], array_b : Int[]) ~ Int {
|
||||
if(array_a = Nil) {
|
||||
return 0;
|
||||
};
|
||||
|
||||
if(array_b = Nil) {
|
||||
return 0;
|
||||
};
|
||||
|
||||
if(array_a->Size() <> array_b->Size()) {
|
||||
return 0;
|
||||
};
|
||||
|
||||
val := 0;
|
||||
for(x := 0; x < array_a->Size(); x += 1;) {
|
||||
val += (array_a[x] * array_b[x]);
|
||||
};
|
||||
|
||||
return val;
|
||||
}
|
||||
}
|
||||
}
|
||||
108
Task/Dot-product/Objective-C/dot-product.m
Normal file
108
Task/Dot-product/Objective-C/dot-product.m
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
#import <stdio.h>
|
||||
#import <stdint.h>
|
||||
#import <stdlib.h>
|
||||
#import <string.h>
|
||||
#import <objc/Object.h>
|
||||
|
||||
// this class exists to return a result between two
|
||||
// vectors: if vectors have different "size", valid
|
||||
// must be NO
|
||||
@interface VResult : Object
|
||||
{
|
||||
@private
|
||||
double value;
|
||||
BOOL valid;
|
||||
}
|
||||
+(id)new: (double)v isValid: (BOOL)y;
|
||||
-(id)init: (double)v isValid: (BOOL)y;
|
||||
-(BOOL)isValid;
|
||||
-(double)value;
|
||||
@end
|
||||
|
||||
@implementation VResult
|
||||
+(id)new: (double)v isValid: (BOOL)y
|
||||
{
|
||||
id s = [super new];
|
||||
[s init: v isValid: y];
|
||||
return s;
|
||||
}
|
||||
-(id)init: (double)v isValid: (BOOL)y
|
||||
{
|
||||
value = v;
|
||||
valid = y;
|
||||
return self;
|
||||
}
|
||||
-(BOOL)isValid { return valid; }
|
||||
-(double)value { return value; }
|
||||
@end
|
||||
|
||||
|
||||
@interface RCVector : Object
|
||||
{
|
||||
@private
|
||||
double *vec;
|
||||
uint32_t size;
|
||||
}
|
||||
+(id)newWithArray: (double *)v ofLength: (uint32_t)l;
|
||||
-(id)initWithArray: (double *)v ofLength: (uint32_t)l;
|
||||
-(VResult *)dotProductWith: (RCVector *)v;
|
||||
-(uint32_t)size;
|
||||
-(double *)array;
|
||||
-(void)free;
|
||||
@end
|
||||
|
||||
@implementation RCVector
|
||||
+(id)newWithArray: (double *)v ofLength: (uint32_t)l
|
||||
{
|
||||
id s = [super new];
|
||||
[s initWithArray: v ofLength: l];
|
||||
return s;
|
||||
}
|
||||
-(id)initWithArray: (double *)v ofLength: (uint32_t)l
|
||||
{
|
||||
size = l;
|
||||
vec = malloc(sizeof(double) * l);
|
||||
if ( vec != NULL ) {
|
||||
memcpy(vec, v, sizeof(double)*l);
|
||||
return self;
|
||||
}
|
||||
[super free];
|
||||
return nil;
|
||||
}
|
||||
-(void)free
|
||||
{
|
||||
free(vec);
|
||||
[super free];
|
||||
}
|
||||
-(uint32_t)size { return size; }
|
||||
-(double *)array { return vec; }
|
||||
-(VResult *)dotProductWith: (RCVector *)v
|
||||
{
|
||||
double r = 0.0;
|
||||
uint32_t i, s;
|
||||
double *v1;
|
||||
if ( [self size] != [v size] ) return [VResult new: r isValid: NO];
|
||||
s = [self size];
|
||||
v1 = [v array];
|
||||
for(i = 0; i < s; i++) {
|
||||
r += vec[i] * v1[i];
|
||||
}
|
||||
return [VResult new: r isValid: YES];
|
||||
}
|
||||
@end
|
||||
|
||||
double val1[] = { 1, 3, -5 };
|
||||
double val2[] = { 4,-2, -1 };
|
||||
|
||||
int main()
|
||||
{
|
||||
RCVector *v1 = [RCVector newWithArray: val1 ofLength: sizeof(val1)/sizeof(double)];
|
||||
RCVector *v2 = [RCVector newWithArray: val2 ofLength: sizeof(val1)/sizeof(double)];
|
||||
VResult *r = [v1 dotProductWith: v2];
|
||||
if ( [r isValid] ) {
|
||||
printf("%lf\n", [r value]);
|
||||
} else {
|
||||
fprintf(stderr, "length of vectors differ\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
3
Task/Dot-product/Octave/dot-product.octave
Normal file
3
Task/Dot-product/Octave/dot-product.octave
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
a = [1, 3, -5]
|
||||
b = [4, -2, -1] % or [4; -2; -1] and avoid transposition with '
|
||||
disp( a * b' ) % ' means transpose
|
||||
7
Task/Dot-product/Oz/dot-product.oz
Normal file
7
Task/Dot-product/Oz/dot-product.oz
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
declare
|
||||
fun {DotProduct Xs Ys}
|
||||
{Length Xs} = {Length Ys} %% assert
|
||||
{List.foldL {List.zip Xs Ys Number.'*'} Number.'+' 0}
|
||||
end
|
||||
in
|
||||
{Show {DotProduct [1 3 ~5] [4 ~2 ~1]}}
|
||||
3
Task/Dot-product/PARI-GP/dot-product.pari
Normal file
3
Task/Dot-product/PARI-GP/dot-product.pari
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
dot(u,v)={
|
||||
sum(i=1,#u,u[i]*v[i])
|
||||
};
|
||||
10
Task/Dot-product/PL-I/dot-product.pli
Normal file
10
Task/Dot-product/PL-I/dot-product.pli
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
get (n);
|
||||
begin;
|
||||
declare (A(n), B(n)) float;
|
||||
declare dot_product float;
|
||||
|
||||
get list (A);
|
||||
get list (B);
|
||||
dot_product = sum(a*b);
|
||||
put (dot_product);
|
||||
end;
|
||||
1
Task/Dot-product/Perl-6/dot-product.pl6
Normal file
1
Task/Dot-product/Perl-6/dot-product.pl6
Normal file
|
|
@ -0,0 +1 @@
|
|||
say [+] (1, 3, -5) »*« (4, -2, -1);
|
||||
17
Task/Dot-product/PostScript/dot-product.ps
Normal file
17
Task/Dot-product/PostScript/dot-product.ps
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/dotproduct{
|
||||
/x exch def
|
||||
/y exch def
|
||||
/sum 0 def
|
||||
/i 0 def
|
||||
x length y length eq %Check if both arrays have the same length
|
||||
{
|
||||
x length{
|
||||
/sum x i get y i get mul sum add def
|
||||
/i i 1 add def
|
||||
}repeat
|
||||
sum ==
|
||||
}
|
||||
{
|
||||
-1 ==
|
||||
}ifelse
|
||||
}def
|
||||
24
Task/Dot-product/PureBasic/dot-product.purebasic
Normal file
24
Task/Dot-product/PureBasic/dot-product.purebasic
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
Procedure dotProduct(Array a(1),Array b(1))
|
||||
Protected i, sum, length = ArraySize(a())
|
||||
|
||||
If ArraySize(a()) = ArraySize(b())
|
||||
For i = 0 To length
|
||||
sum + a(i) * b(i)
|
||||
Next
|
||||
EndIf
|
||||
|
||||
ProcedureReturn sum
|
||||
EndProcedure
|
||||
|
||||
If OpenConsole()
|
||||
Dim a(2)
|
||||
Dim b(2)
|
||||
|
||||
a(0) = 1 : a(1) = 3 : a(2) = -5
|
||||
b(0) = 4 : b(1) = -2 : b(2) = -1
|
||||
|
||||
PrintN(Str(dotProduct(a(),b())))
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
3
Task/Dot-product/RLaB/dot-product.rlab
Normal file
3
Task/Dot-product/RLaB/dot-product.rlab
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
x = rand(1,10);
|
||||
y = rand(1,10);
|
||||
s = sum( x .* y );
|
||||
16
Task/Dot-product/Rascal/dot-product-1.rascal
Normal file
16
Task/Dot-product/Rascal/dot-product-1.rascal
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import List;
|
||||
|
||||
public int dotProduct(list[int] L, list[int] M){
|
||||
result = 0;
|
||||
if(size(L) == size(M)) {
|
||||
while(size(L) >= 1) {
|
||||
result += (head(L) * head(M));
|
||||
L = tail(L);
|
||||
M = tail(M);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
throw "vector sizes must match";
|
||||
}
|
||||
}
|
||||
12
Task/Dot-product/Rascal/dot-product-2.rascal
Normal file
12
Task/Dot-product/Rascal/dot-product-2.rascal
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import Prelude;
|
||||
|
||||
public real matrixDotproduct(rel[real x, real y, real v] column1, rel[real x, real y, real v] column2){
|
||||
return (0.0 | it + v1*v2 | <x1,y1,v1> <- column1, <x2,y2,v2> <- column2, y1==y2);
|
||||
}
|
||||
|
||||
//a matrix, given by a relation of x-coordinate, y-coordinate, value.
|
||||
public rel[real x, real y, real v] matrixA = {
|
||||
<0.0,0.0,12.0>, <0.0,1.0, 6.0>, <0.0,2.0,-4.0>,
|
||||
<1.0,0.0,-51.0>, <1.0,1.0,167.0>, <1.0,2.0,24.0>,
|
||||
<2.0,0.0,4.0>, <2.0,1.0,-68.0>, <2.0,2.0,-41.0>
|
||||
};
|
||||
14
Task/Dot-product/Run-BASIC/dot-product.run
Normal file
14
Task/Dot-product/Run-BASIC/dot-product.run
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
v1$ = "1, 3, -5"
|
||||
v2$ = "4, -2, -1"
|
||||
|
||||
print "DotProduct of ";v1$;" and "; v2$;" is ";dotProduct(v1$,v2$)
|
||||
end
|
||||
|
||||
function dotProduct(a$, b$)
|
||||
while word$(a$,i + 1,",") <> ""
|
||||
i = i + 1
|
||||
v1$=word$(a$,i,",")
|
||||
v2$=word$(b$,i,",")
|
||||
dotProduct = dotProduct + val(v1$) * val(v2$)
|
||||
wend
|
||||
end function
|
||||
11
Task/Dot-product/SNOBOL4/dot-product.sno
Normal file
11
Task/Dot-product/SNOBOL4/dot-product.sno
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
define("dotp(a,b)sum,i") :(dotp_end)
|
||||
dotp i = 1; sum = 0
|
||||
loop sum = sum + (a<i> * b<i>)
|
||||
i = i + 1 ?a<i> :s(loop)
|
||||
dotp = sum :(return)
|
||||
dotp_end
|
||||
|
||||
a = array(3); a<1> = 1; a<2> = 3; a<3> = -5;
|
||||
b = array(3); b<1> = 4; b<2> = -2; b<3> = -1;
|
||||
output = dotp(a,b)
|
||||
end
|
||||
41
Task/Dot-product/SPARK/dot-product.spark
Normal file
41
Task/Dot-product/SPARK/dot-product.spark
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
with Spark_IO;
|
||||
--# inherit Spark_IO;
|
||||
--# main_program;
|
||||
procedure Dot_Product_Main
|
||||
--# global in out Spark_IO.Outputs;
|
||||
--# derives Spark_IO.Outputs from *;
|
||||
is
|
||||
Limit : constant := 1000;
|
||||
type V_Elem is range -Limit .. Limit;
|
||||
V_Size : constant := 100;
|
||||
type V_Index is range 1 .. V_Size;
|
||||
type Vector is array(V_Index range <>) of V_Elem;
|
||||
|
||||
type V_Prod is range -(Limit**2)*V_Size .. (Limit**2)*V_Size;
|
||||
--# assert V_Prod'Base is Integer;
|
||||
|
||||
subtype Index3 is V_Index range 1 .. 3;
|
||||
subtype Vector3 is Vector(Index3);
|
||||
Vect1 : constant Vector3 := Vector3'(1, 3, -5);
|
||||
Vect2 : constant Vector3 := Vector3'(4, -2, -1);
|
||||
|
||||
function Dot_Product(V1, V2 : Vector) return V_Prod
|
||||
--# pre V1'First = V2'First
|
||||
--# and V1'Last = V2'Last;
|
||||
is
|
||||
Sum : V_Prod := 0;
|
||||
begin
|
||||
for I in V_Index range V1'Range
|
||||
--# assert Sum in -(Limit**2)*V_Prod(I-1) .. (Limit**2)*V_Prod(I-1);
|
||||
loop
|
||||
Sum := Sum + V_Prod(V1(I)) * V_Prod(V2(I));
|
||||
end loop;
|
||||
return Sum;
|
||||
end Dot_Product;
|
||||
|
||||
begin
|
||||
Spark_IO.Put_Integer(File => Spark_IO.Standard_Output,
|
||||
Item => Integer(Dot_Product(Vect1, Vect2)),
|
||||
Width => 6,
|
||||
Base => 10);
|
||||
end Dot_Product_Main;
|
||||
3
Task/Dot-product/SQL/dot-product.sql
Normal file
3
Task/Dot-product/SQL/dot-product.sql
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
select i, k, sum(A.N*B.N) as N
|
||||
from A inner join B on A.j=B.j
|
||||
group by i, k
|
||||
23
Task/Dot-product/Seed7/dot-product.seed7
Normal file
23
Task/Dot-product/Seed7/dot-product.seed7
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
$ syntax expr: .().dot.() is -> 6; # priority of dot operator
|
||||
|
||||
const func integer: (in array integer: a) dot (in array integer: b) is func
|
||||
result
|
||||
var integer: sum is 0;
|
||||
local
|
||||
var integer: index is 0;
|
||||
begin
|
||||
if length(a) <> length(b) then
|
||||
raise RANGE_ERROR;
|
||||
else
|
||||
for index range 1 to length(a) do
|
||||
sum +:= a[index] * b[index];
|
||||
end for;
|
||||
end if;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
begin
|
||||
writeln([](1, 3, -5) dot [](4, -2, -1));
|
||||
end func;
|
||||
6
Task/Dot-product/Slate/dot-product.slate
Normal file
6
Task/Dot-product/Slate/dot-product.slate
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
v@(Vector traits) <dot> w@(Vector traits)
|
||||
"Dot-product."
|
||||
[
|
||||
(0 below: (v size min: w size)) inject: 0 into:
|
||||
[| :sum :index | sum + ((v at: index) * (w at: index))]
|
||||
].
|
||||
6
Task/Dot-product/Standard-ML/dot-product-1.ml
Normal file
6
Task/Dot-product/Standard-ML/dot-product-1.ml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
val dot = ListPair.foldlEq Real.*+ 0.0
|
||||
|
||||
(*
|
||||
- dot ([1.0, 3.0, ~5.0], [4.0, ~2.0, ~1.0]);
|
||||
val it = 3.0 : real
|
||||
*)
|
||||
11
Task/Dot-product/Standard-ML/dot-product-2.ml
Normal file
11
Task/Dot-product/Standard-ML/dot-product-2.ml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fun dot (v, u) = (
|
||||
if Vector.length v <> Vector.length u then
|
||||
raise ListPair.UnequalLengths
|
||||
else ();
|
||||
Vector.foldli (fn (i, v_i, z) => v_i * Vector.sub (u, i) + z) 0.0 v
|
||||
)
|
||||
|
||||
(*
|
||||
- dot (#[1.0, 3.0, ~5.0], #[4.0, ~2.0, ~1.0]);
|
||||
val it = 3.0 : real
|
||||
*)
|
||||
7
Task/Dot-product/Ursala/dot-product.ursala
Normal file
7
Task/Dot-product/Ursala/dot-product.ursala
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#import int
|
||||
|
||||
dot = sum:-0+ product*p
|
||||
|
||||
#cast %z
|
||||
|
||||
test = dot(<1,3,-5>,<4,-2,-1>)
|
||||
13
Task/Dot-product/XPL0/dot-product.xpl0
Normal file
13
Task/Dot-product/XPL0/dot-product.xpl0
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
include c:\cxpl\codes;
|
||||
|
||||
func DotProd(U, V, L);
|
||||
int U, V, L;
|
||||
int S, I;
|
||||
[S:= 0;
|
||||
for I:= 0 to L-1 do S:= S + U(I)*V(I);
|
||||
return S;
|
||||
];
|
||||
|
||||
[IntOut(0, DotProd([1, 3, -5], [4, -2, -1], 3));
|
||||
CrLf(0);
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue