summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authorPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-13 20:33:51 +0000
committerPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-13 20:33:51 +0000
commit2d498de6c105e57c32c9048e5901955556ab38bf (patch)
tree47f1f2b2d0946345a21ea8bad7383282d4102e86 /array.c
parent50d6dd8b50958271bf1ff13f99dc21d4cd8431f7 (diff)
Implement dyadic (integer only) version of + - × ÷ * ⍟
Diffstat (limited to 'array.c')
-rw-r--r--array.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/array.c b/array.c
index 4913112..a2afc44 100644
--- a/array.c
+++ b/array.c
@@ -41,3 +41,41 @@ simplescalar(Array *a)
{
return simplearray(a) && a->rank == 0;
}
+
+Array *
+extend(Array *a, Array *b)
+{
+ /* extend the singleton a to the shape of b */
+ Array *shape = fnShape(b);
+ Array *res = fnReshape(shape, a);
+ freearray(shape);
+ return res;
+}
+
+int
+scalarextend(Array *a, Array *b, Array **aa, Array **bb)
+{
+ /* Extend the arrays a and b to have the same shape.
+ The resulting arrays are stored in aa and bb,
+ except when the ranks don't match or extension can't
+ happen, in which case the function returns 0 and
+ aa and bb are unchanged.
+ */
+
+ if(a->size == 1 && b->size != 1){
+ *aa = extend(a, b);
+ *bb = fnSame(b);
+ }else if(b->size == 1 && a->size != 1){
+ *aa = fnSame(a);
+ *bb = extend(b, a);
+ }else if(a->size == b->size && a->rank == b->rank){
+ /* Check that each dimension matches */
+ for(int i = 0; i < a->rank; i++)
+ if(a->shape[i] != b->shape[i])
+ return 0;
+ *aa = fnSame(a);
+ *bb = fnSame(b);
+ }else
+ return 0;
+ return 1;
+} \ No newline at end of file