PVector_ptr Vector_mul(PVector_ptr a, PVector_ptr b) { REF((heap_object *)a.vector); REF((heap_object *)b.vector); int i; if ( a.vector==NULL || b.vector==NULL || a.vector->length!=b.vector->length ) vector_operation_error(); size_t n = a.vector->length; PVector_ptr c = PVector_init(0, n); for (i=0; i<n; i++) c.vector->nodes[i].data = ith(a, i) * ith(b, i); DEREF((heap_object *)a.vector); DEREF((heap_object *)b.vector); return c; }
PVector_ptr Vector_add(PVector_ptr a, PVector_ptr b) { REF((heap_object *)a.vector); REF((heap_object *)b.vector); int i; if ( a.vector==NULL || b.vector==NULL || a.vector->length!=b.vector->length ) vector_operation_error(); size_t n = a.vector->length; PVector_ptr c = PVector_init(0, n); for (i=0; i<n; i++) c.vector->nodes[i].data = ith(a, i) + ith(b, i); // safe because we have sole ptr to c for now DEREF((heap_object *)a.vector); DEREF((heap_object *)b.vector); return c; }
PVector_ptr Vector_div(PVector_ptr a, PVector_ptr b) { REF((heap_object *)a.vector); REF((heap_object *)b.vector); int i; if ( a.vector==NULL || b.vector==NULL || a.vector->length!=b.vector->length ) vector_operation_error(); size_t n = a.vector->length; PVector_ptr c = PVector_init(0, n); for (i=0; i<n; i++) { if (ith(b,i) == 0) { fprintf(stderr, "ZeroDivisionError: Divisor cann't be 0\n"); exit(1); } c.vector->nodes[i].data = ith(a, i) / ith(b, i); } DEREF((heap_object *)a.vector); DEREF((heap_object *)b.vector); return c; }
PVector_ptr Vector_add(PVector_ptr a, PVector_ptr b) { REF((heap_object *)a.vector); REF((heap_object *)b.vector); if ( a.vector==NULL || b.vector==NULL ) { null_pointer_error("Addition operator cannot be applied to NULL Vectors\n"); return NIL_VECTOR; } if ( a.vector->length!=b.vector->length ) { vector_operation_error(); return NIL_VECTOR; } int i; size_t n = a.vector->length; PVector_ptr c = PVector_init(0, n); for (i=0; i<n; i++) c.vector->nodes[i].data = ith(a, i) + ith(b, i); // safe because we have sole ptr to c for now DEREF((heap_object *)a.vector); DEREF((heap_object *)b.vector); return c; }
PVector_ptr Vector_mul(PVector_ptr a, PVector_ptr b) { REF((heap_object *)a.vector); REF((heap_object *)b.vector); if ( a.vector==NULL || b.vector==NULL ) { null_pointer_error("Multiplication operator cannot be applied to NULL Vectors\n"); return NIL_VECTOR; } if ( a.vector->length!=b.vector->length ) { vector_operation_error(); return NIL_VECTOR; } int i; size_t n = a.vector->length; PVector_ptr c = PVector_init(0, n); for (i=0; i<n; i++) c.vector->nodes[i].data = ith(a, i) * ith(b, i); DEREF((heap_object *)a.vector); DEREF((heap_object *)b.vector); return c; }
PVector_ptr Vector_div(PVector_ptr a, PVector_ptr b) { REF((heap_object *)a.vector); REF((heap_object *)b.vector); if ( a.vector==NULL || b.vector==NULL ) { null_pointer_error("Division operator cannot be applied to NULL Vectors\n"); return NIL_VECTOR; } if ( a.vector->length!=b.vector->length ) { vector_operation_error(); return NIL_VECTOR; } int i; size_t n = a.vector->length; PVector_ptr c = PVector_init(0, n); for (i=0; i<n; i++) { if (ith(b,i) == 0) { fprintf(stderr, "ZeroDivisionError: Divisor cann't be 0\n"); return NIL_VECTOR; } c.vector->nodes[i].data = ith(a, i) / ith(b, i); } DEREF((heap_object *)a.vector); DEREF((heap_object *)b.vector); return c; }