janet-lang / spork

Various Janet utility modules - the official "Contrib" library.
MIT License
122 stars 35 forks source link

Add math module with statistics and linear algebra code #88

Closed pepe closed 2 years ago

pepe commented 2 years ago

I still need to create documentation from docstrings and tests, so I am just thinking about Janet's script for that.

But I will be happy if anyone checks the codes and tests already there.

sogaiu commented 2 years ago

My jpm test output for eb9508a9759d818a63cb6e73afa0dc6588da8440 ended in an error.

Full output follows.

$ jpm test
compiling src/tarray.c to build/src___tarray.o...
compiling src/json.c to build/src___json.o...
generating meta file build/spork/json.meta.janet...
compiling src/rawterm.c to build/src___rawterm.o...
generating meta file build/spork/rawterm.meta.janet...
compiling src/utf8.c to build/src___utf8.o...
generating meta file build/spork/utf8.meta.janet...
generating meta file build/spork/tarray.meta.janet...
compiling src/tarray.c to build/src___tarray.static.o...
creating native module build/spork/tarray.so...
compiling src/json.c to build/src___json.static.o...
creating native module build/spork/json.so...
creating native module build/spork/rawterm.so...
compiling src/rawterm.c to build/src___rawterm.static.o...
creating native module build/spork/utf8.so...
compiling src/utf8.c to build/src___utf8.static.o...
creating static library build/spork/tarray.a...
creating static library build/spork/json.a...
creating static library build/spork/rawterm.a...
creating static library build/spork/utf8.a...
running test/suite0000.janet ...
test suite 0 finished in 0.000 seconds - 7 of 7 tests passed.
running test/suite0001.janet ...
test suite 1 finished in 0.000 seconds - 7 of 7 tests passed.
running test/suite0002.janet ...
test suite 2 finished in 0.001 seconds - 5 of 5 tests passed.
running test/suite0003.janet ...
test suite 3 finished in 0.001 seconds - 2 of 2 tests passed.
running test/suite0004.janet ...
test suite 4 finished in 0.000 seconds - 16 of 16 tests passed.
running test/suite0005.janet ...
test suite 5 finished in 0.000 seconds - 16 of 16 tests passed.
running test/suite0006.janet ...
test suite 6 finished in 0.000 seconds - 29 of 29 tests passed.
running test/suite0007.janet ...
Hello world!
test suite 666 finished in 0.000 seconds - 16 of 16 tests passed.
running test/suite0008.janet ...
test suite 8 finished in 0.001 seconds - 8 of 8 tests passed.
running test/suite0009.janet ...
Starting networked repl server on 127.0.0.1, port 8000...
client test connected
test suite 9 finished in 0.001 seconds - 14 of 14 tests passed.
closing client test
running test/suite0010.janet ...
test suite 10 finished in 0.000 seconds - 2 of 2 tests passed.
running test/suite0011.janet ...
test suite 11 finished in 0.000 seconds - 44 of 44 tests passed.
running test/suite0012.janet ...
test suite 12 finished in 0.047 seconds - 56 of 56 tests passed.
running test/suite0013.janet ...
test suite 13 finished in 0.000 seconds - 17 of 17 tests passed.
running test/suite0014.janet ...
test suite 14 finished in 0.002 seconds - 23 of 23 tests passed.
running test/suite0015.janet ...
test suite 15 finished in 0.000 seconds - 19 of 19 tests passed.
running test/suite0016.janet ...
test suite 16 finished in 0.000 seconds - 7 of 7 tests passed.
running test/suite0017.janet ...
test suite 17 finished in 0.000 seconds - 6 of 6 tests passed.
running test/suite0018.janet ...
test suite 18 finished in 0.000 seconds - 33 of 33 tests passed.
running test/suite0019.janet ...
test suite 19 finished in 0.124 seconds - 173152 of 173152 tests passed.
running test/suite0020.janet ...
test suite 20 finished in 0.015 seconds - 299 of 299 tests passed.
running test/suite0021.janet ...
error: spork/math.janet:960:5: compile error: unknown symbol do-var
  in dofile [boot.janet] (tailcall) on line 2879, column 7
  in source-loader [boot.janet] on line 2889, column 15
  in require-1 [boot.janet] on line 2909, column 18
  in import* [boot.janet] (tailcall) on line 2940, column 15
non-zero exit code in test/suite0021.janet: 1
Failing test scripts: 1
pepe commented 2 years ago

Thank you, @sogaiu, for checking! I can see now that even here, the check is failing. For me, everything seems to be ok on my machine. I will investigate what is going on.

Techcable commented 2 years ago

I would like to note this is by far the largest Janet module in spork.

It looks useful, but it is over 1000 lines of code. The next biggest modules in size are spork/misc.janet (666 lines) and spork/http.janet (447 lines).

It also appears to have a large API surface. Perhaps it would be a good idea to separate into multiple files for better organization/readability. You could move the matrix and statistics code into separate modules, allowing for things to be found easier.

pepe commented 2 years ago

If you go one commit back it is like that 😉. Thank you for the point of view, that's why it is still a draft.

bakpakin commented 2 years ago

Good work here! I've actually wanted something like this for a while and would redo things poorly often.

No problem with the size of the library, a stats library looks pretty useful to me. I think a more succinct description of this module though would be just "stats", I don't really see any linear algebra here.

EDIT: nvm, I see a few things at the bottom for linear algebra. Sort of like the kinds of things you would find in R or python's numpy

pepe commented 2 years ago

Thank you. I am teaching this week so no time. But I will just add the document with API and I undraft it.

pepe commented 2 years ago

I added essential documentation. The failing check here, I quite do not understand what is going on.

bakpakin commented 2 years ago

Probably don't use (use /spork/math), use a different import pattern

sogaiu commented 2 years ago

When I make the following changes, things work ok here:

diff --git a/spork/math.janet b/spork/math.janet
index 988b3ba..c480372 100644
--- a/spork/math.janet
+++ b/spork/math.janet
@@ -1,4 +1,4 @@
-(use spork/misc)
+(use ./misc)

 # Statistics

diff --git a/test/suite-math.janet b/test/suite-math.janet
index 5382fe2..229c8c2 100644
--- a/test/suite-math.janet
+++ b/test/suite-math.janet
@@ -1,6 +1,6 @@
-(use spork/misc)
-(use spork/test)
-(import /spork/math :prefix "")
+(use ../spork/misc)
+(use ../spork/test)
+(use ../spork/math)

 (start-suite 0)
pepe commented 2 years ago

@sogaiu thank you very much! Finally, it is ready I guess.

pepe commented 2 years ago

Another thing that I need to address is the doc's order. I guess I should order them alphabetically, @bakpakin?

bakpakin commented 2 years ago

Yeah, I generally try to sort them so I can more easily find a specific import, but it doesn't matter

Techcable commented 2 years ago

I still think it'd be nice to split the linear algebra/statistics code into submodules to make them more organized and easier to find.

There are already 56 functions in this module, and I fear this may grow in the future.

For comparison, Python has 28 functions in the Python stdlib statistics module, which they've deemed worthwhile to split up.

Maybe you could also add some functions for combinatorics like count-combinations, count-permutations, and factorial. These are all in the Python math module and I've found them very useful.

pepe commented 2 years ago

@Techcable I guess the consensus is to leave it all in the math module. PRs for new fns welcome as always.

@bakpakin I ordered the docs. I guess it is ready to go

pepe commented 2 years ago

@bakpakin, can you please retake a look? I guess we can merge it now.