langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View 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));
}
}

View 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])

View 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.
*)

View 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.
*)

View 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;
}
}
}

View 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;
}

View 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

View 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]}}

View file

@ -0,0 +1,3 @@
dot(u,v)={
sum(i=1,#u,u[i]*v[i])
};

View 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;

View file

@ -0,0 +1 @@
say [+] (1, 3, -5) »*« (4, -2, -1);

View 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

View 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

View file

@ -0,0 +1,3 @@
x = rand(1,10);
y = rand(1,10);
s = sum( x .* y );

View 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";
}
}

View 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>
};

View 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

View 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

View 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;

View 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

View 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;

View 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))]
].

View 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
*)

View 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
*)

View file

@ -0,0 +1,7 @@
#import int
dot = sum:-0+ product*p
#cast %z
test = dot(<1,3,-5>,<4,-2,-1>)

View 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);
]