March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -1,9 +1,8 @@
void main() {
import std.stdio, std.range, std.algorithm;
import std.stdio, std.algorithm;
double[3] a = [1.0, 3.0, -5.0];
double[3] b = [4.0, -2.0, -1.0];
double[3] c;
c[] = a[] * b[];
c.reduce!q{a + b}.writeln;
double[3] c = a[] * b[];
c[].sum.writeln;
}

View file

@ -0,0 +1,16 @@
num dot(List<num> A, List<num> B){
if (A.length != B.length){
throw new Exception('Vectors must be of equal size');
}
num result = 0;
for (int i = 0; i < A.length; i++){
result += A[i] * B[i];
}
return result;
}
void main(){
var l = [1,3,-5];
var k = [4,-2,-1];
print(dot(l,k));
}

View file

@ -0,0 +1 @@
<1,2,3> . <4,5,6>

View file

@ -0,0 +1 @@
Array([1,2,3]) . Array([4,5,6])

View file

@ -0,0 +1 @@
LinearAlgebra( <1,2,3>, <4,5,6> )

View file

@ -0,0 +1 @@
LinearAlgebra( Array([1,2,3]), Array([4,5,6]) )

View file

@ -0,0 +1 @@
LinearAlgebra([1,2,3], [4,5,6] )

View file

@ -2,34 +2,34 @@
#import <stdint.h>
#import <stdlib.h>
#import <string.h>
#import <objc/Object.h>
#import <Foundation/Foundation.h>
// this class exists to return a result between two
// vectors: if vectors have different "size", valid
// must be NO
@interface VResult : Object
@interface VResult : NSObject
{
@private
double value;
BOOL valid;
}
+(id)new: (double)v isValid: (BOOL)y;
-(id)init: (double)v isValid: (BOOL)y;
+(instancetype)new: (double)v isValid: (BOOL)y;
-(instancetype)init: (double)v isValid: (BOOL)y;
-(BOOL)isValid;
-(double)value;
@end
@implementation VResult
+(id)new: (double)v isValid: (BOOL)y
+(instancetype)new: (double)v isValid: (BOOL)y
{
id s = [super new];
[s init: v isValid: y];
return s;
return [[self alloc] init: v isValid: y];
}
-(id)init: (double)v isValid: (BOOL)y
-(instancetype)init: (double)v isValid: (BOOL)y
{
value = v;
valid = y;
if ((self == [super init])) {
value = v;
valid = y;
}
return self;
}
-(BOOL)isValid { return valid; }
@ -37,14 +37,14 @@
@end
@interface RCVector : Object
@interface RCVector : NSObject
{
@private
double *vec;
uint32_t size;
}
+(id)newWithArray: (double *)v ofLength: (uint32_t)l;
-(id)initWithArray: (double *)v ofLength: (uint32_t)l;
+(instancetype)newWithArray: (double *)v ofLength: (uint32_t)l;
-(instancetype)initWithArray: (double *)v ofLength: (uint32_t)l;
-(VResult *)dotProductWith: (RCVector *)v;
-(uint32_t)size;
-(double *)array;
@ -52,27 +52,24 @@
@end
@implementation RCVector
+(id)newWithArray: (double *)v ofLength: (uint32_t)l
+(instancetype)newWithArray: (double *)v ofLength: (uint32_t)l
{
id s = [super new];
[s initWithArray: v ofLength: l];
return s;
return [[self alloc] initWithArray: v ofLength: l];
}
-(id)initWithArray: (double *)v ofLength: (uint32_t)l
-(instancetype)initWithArray: (double *)v ofLength: (uint32_t)l
{
size = l;
vec = malloc(sizeof(double) * l);
if ( vec != NULL ) {
if ((self = [super init])) {
size = l;
vec = malloc(sizeof(double) * l);
if ( vec == NULL )
return nil;
memcpy(vec, v, sizeof(double)*l);
return self;
}
[super free];
return nil;
return self;
}
-(void)free
-(void)dealloc
{
free(vec);
[super free];
}
-(uint32_t)size { return size; }
-(double *)array { return vec; }
@ -96,13 +93,15 @@ 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");
@autoreleasepool {
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

@ -1,12 +1,18 @@
import scala.language.postfixOps
import scala.language.implicitConversions
class Dot[T](v1: Seq[T])(implicit n: Numeric[T]) {
import n._
import n._ // import * operator
def dot(v2: Seq[T]) = {
require(v1.length == v2.length)
v1 zip v2 map Function.tupled(_*_) sum
require(v1.size == v2.size)
v1 zip v2 map Function.tupled(_ * _) sum
}
}
implicit def toDot[T : Numeric](v1: Seq[T]) = new Dot(v1)
val v1 = List(1, 3, -5)
val v2 = List(4, -2, -1)
println(v1 dot v2)
object Main extends App {
implicit def toDot[T: Numeric](v1: Seq[T]) = new Dot(v1)
val v1 = List(1, 3, -5)
val v2 = List(4, -2, -1)
println(v1 dot v2)
}