summaryrefslogtreecommitdiff
path: root/functions.c
diff options
context:
space:
mode:
authorPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-11 17:04:30 +0000
committerPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-11 17:04:30 +0000
commitfe701a61f4d057597dab3d46ba0fe31e550b41df (patch)
tree0a0624d789a59794c80e2bdb4f36fddec558918d /functions.c
parent06b5369113b33e535e09dc1391a98c912ca294b7 (diff)
Add monadic ≢ and ⍴
Diffstat (limited to 'functions.c')
-rw-r--r--functions.c66
1 files changed, 43 insertions, 23 deletions
diff --git a/functions.c b/functions.c
index ddd5532..8a5c959 100644
--- a/functions.c
+++ b/functions.c
@@ -31,7 +31,7 @@ fnmonad monadfunctiondefs[] = {
0, /* > */
0, /* ≥ */
0, /* ≡ */
- 0, /* ≢ */
+ fnTally, /* ≢ */
0, /* ∨ */
0, /* ∧ */
0, /* ⍲ */
@@ -53,7 +53,7 @@ fnmonad monadfunctiondefs[] = {
0, /* ~ */
fnRavel, /* , */
0, /* ⍪ */
- 0, /* ⍴ */
+ fnShape, /* ⍴ */
0, /* ⌽ */
0, /* ⊖ */
0, /* ⍉ */
@@ -61,30 +61,12 @@ fnmonad monadfunctiondefs[] = {
0, /* ⍕ */
};
-Array *
-fnCatenateFirst(Array *left, Array *right)
-{
- /* not even close to being right, but it works for stranding :) */
- if(left->rank == 0)
- left = fnRavel(left);
- if(right->rank == 0)
- right = fnRavel(right);
-
- /* assume two vectors of same type for now */
- Array *res = mkarray(left->type, 1, left->size+right->size);
- res->shape[0] = left->shape[0] + right->shape[0];
- memcpy(res->rawdata, left->rawdata, datasizes[res->type]*left->size);
- memcpy(res->rawdata+datasizes[res->type]*left->size, right->rawdata, datasizes[res->type]*right->size);
- return res;
-}
+/* Monadic functions */
Array *
-fnNest(Array *right)
+fnTally(Array *right)
{
- if(simplearray(right))
- return fnEnclose(right);
- else
- return right;
+ return mkscalarint(right->rank==0 ? 1 : right->shape[0]);
}
Array *
@@ -100,6 +82,15 @@ fnEnclose(Array *right)
}
Array *
+fnNest(Array *right)
+{
+ if(simplearray(right))
+ return fnEnclose(right);
+ else
+ return right;
+}
+
+Array *
fnRavel(Array *right)
{
Array *res = duparray(right);
@@ -107,4 +98,33 @@ fnRavel(Array *right)
res->shape = realloc(res->shape, sizeof(int) * 1);
res->shape[0] = res->size;
return res;
+}
+
+Array *
+fnShape(Array *right)
+{
+ Array *res = mkarray(AtypeInt, 1, right->rank);
+ res->shape[0] = right->rank;
+ for(int i = 0; i < right->rank; i++)
+ res->intdata[i] = right->shape[i];
+ return res;
+}
+
+/* Dyadic functions */
+
+Array *
+fnCatenateFirst(Array *left, Array *right)
+{
+ /* not even close to being right, but it works for stranding :) */
+ if(left->rank == 0)
+ left = fnRavel(left);
+ if(right->rank == 0)
+ right = fnRavel(right);
+
+ /* assume two vectors of same type for now */
+ Array *res = mkarray(left->type, 1, left->size+right->size);
+ res->shape[0] = left->shape[0] + right->shape[0];
+ memcpy(res->rawdata, left->rawdata, datasizes[res->type]*left->size);
+ memcpy(res->rawdata+datasizes[res->type]*left->size, right->rawdata, datasizes[res->type]*right->size);
+ return res;
} \ No newline at end of file