Skip to content

Commit

Permalink
large refactoring, notes, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriy-chumak committed Jun 6, 2024
1 parent 43011f9 commit a125f36
Show file tree
Hide file tree
Showing 29 changed files with 1,127 additions and 299 deletions.
27 changes: 23 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ define libol-algebra
endef

libol-algebra.so: $(wildcard src/*.h)
libol-algebra.so: vector.c matrix.c tensor.c $(wildcard src/*.c)
libol-algebra.so: vector.c matrix.c tensor.c $(wildcard src/*.c) \
$(wildcard src/**/*.c)
$(call libol-algebra,$^,$@)

libol-algebra.dll:CC := x86_64-w64-mingw32-gcc
Expand All @@ -51,21 +52,39 @@ libol-algebra.dll:CFLAGS += -Iwin32 \
-DOLVM_FCONV_PROTOTYPES -DOLVM_INEXACTS=1
libol-algebra.dll:$(wildcard src/*.h)
libol-algebra.dll:win32/DllMain.c
libol-algebra.dll:vector.c matrix.c tensor.c $(wildcard src/*.c)
libol-algebra.dll:vector.c matrix.c tensor.c $(wildcard src/*.c) \
$(wildcard src/**/*.c)
mkdir -p win32/ol
cp $(DESTDIR)$(PREFIX)/include/ol/vm.h win32/ol/vm.h
$(call libol-algebra,$^,$@)

# #####################################
# check
red=\033[1;31m
green=\033[1;32m
yellow=\033[1;33m
done=\033[0m

check: check-reference
$(MAKE) -B debug
# exact math check
LD_LIBRARY_PATH=`pwd` OTUS_ALGEBRA_DEFAULT_EXACTNESS=1 \
@printf "\n$(yellow)NO library, EXACTNESS = 1$(done)\n"
LD_LIBRARY_PATH=/ OTUS_ALGEBRA_DEFAULT_EXACTNESS=1 \
OTUS_ALGEBRA_NO_STARTUP_WARNINGS=1 \
$(MAKE) --always-make --quiet tests
# inexact math check
LD_LIBRARY_PATH=`pwd` OTUS_ALGEBRA_DEFAULT_EXACTNESS=0 \
@printf "\n$(yellow)NO library, EXACTNESS = 0$(done)\n"
LD_LIBRARY_PATH=/ OTUS_ALGEBRA_DEFAULT_EXACTNESS=0 \
OTUS_ALGEBRA_NO_STARTUP_WARNINGS=1 \
$(MAKE) --always-make --quiet tests
# exact math check
@printf "\n$(yellow)YES library, EXACTNESS = 1$(done)\n"
LD_LIBRARY_PATH=. OTUS_ALGEBRA_DEFAULT_EXACTNESS=1 \
OTUS_ALGEBRA_NO_STARTUP_WARNINGS=1 \
$(MAKE) --always-make --quiet tests
@printf "\n$(yellow)YES library, EXACTNESS = 0$(done)\n"
# inexact fast math check
LD_LIBRARY_PATH=. OTUS_ALGEBRA_DEFAULT_EXACTNESS=0 \
OTUS_ALGEBRA_NO_STARTUP_WARNINGS=1 \
$(MAKE) --always-make --quiet tests
@echo "Well done."
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ type ',help' to help, ',quit' to end session.

Infix notation inside Lisp (`\\` is a short for macro `infix-notation`):
```scheme
> (import (math infix-notation))
> (\\
[1 3 -5] ⨯ [4 -2 -1]
)
#(-13 -19 -14)
[-13 -19 -14]
```

Some unicode math symbols (don't forget spaces between regular and unicode math letters):
```scheme
> (import (math infix-notation))
> (import (otus algebra unicode))
> (define-values (a b c) (values 5 3 -26))
Expand Down
8 changes: 4 additions & 4 deletions matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// dot (matrix-multiplication)

__attribute__((used))
word* mdot(olvm_t* this, word arguments) // todo: change to word arguments
word mdot(olvm_t* this, word arguments) // todo: change to word arguments
{
word* fp;

Expand All @@ -22,7 +22,7 @@ word* mdot(olvm_t* this, word arguments) // todo: change to word arguments
size_t q = value(car(cdar(B)));

if (value(caar(B)) != n)
return RFALSE;
return IFALSE;

word* floats = new_floats(this, m * q, &A, &B);

Expand All @@ -45,7 +45,7 @@ word* mdot(olvm_t* this, word arguments) // todo: change to word arguments

// Matrix Transposition
__attribute__((used))
word* mtranspose(olvm_t* this, word arguments)
word mtranspose(olvm_t* this, word arguments)
{
word* fp;

Expand Down Expand Up @@ -81,5 +81,5 @@ word* mtranspose(olvm_t* this, word arguments)
RETURN_TENSOR(new_list(TPAIR, car(cdar(A)), caar(A)), floats);
}

return (word*) IFALSE; // invalid call
return IFALSE; // invalid call
}
50 changes: 25 additions & 25 deletions otus/algebra.scm
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,27 @@
;; реально обрабатывать не выйдет, но будет легко читаемый код с алгоритмами работы
;; (заодно и легче проверяемый)
;; точная, но медленная предваряется "e" (exact)
;; быстрая, неточная математика предваряется "f" (floating point, fast) или "i" (inexact)
;; быстрая, неточная математика предваряется "i" (inexact) или "f" (floating point, fast)

;; по умолчанию vector, matrix и tensor сделаны "exact"

Array?
Scalar?


; same as Tensor/Tensor~
Array
Array~
Array?

; create a Vector
Vector ; exact vector (lisp)
Vector~ ; inexact vector (c)
; Vector? ; helper function
Vector? ; is a vector?

; create a Matrix
Matrix ; exact matrix (lisp)
Matrix~ ; inexact matrix (c)
; Matrix?

;; ; create a Tensor
;; Tensor ; exact tensor (lisp)
; create a Tensor
Tensor ; exact tensor (lisp)
;; Tensor~ ; inexact tensor (c)
;; Tensor?

Expand All @@ -72,6 +74,7 @@
(exports (otus algebra operators))
(exports (otus algebra functions))

Shape Size
(exports (otus algebra shape))

rmap ; Recursive Map, * core, * internal
Expand All @@ -91,11 +94,13 @@

; REPL upgrade:
repl:write

; (math infix-notation) upgrade:
\\ infix-notation
\\operators
\\right-operators
\\postfix-functions

; equation overrides
= equal?
)
Expand All @@ -104,27 +109,22 @@
(otus algebra config))
(begin

; startup notification
(unless (config 'default-exactness algebra)
(print-to stderr "Warning: you'r requested inexact math usage by default,")
(if algebra
(print-to stderr " calculated results may be inaccurate.")
(print-to stderr " but the shared library is not loaded, so that kind of math won't be included.")))
(unless (config 'no-startup-warnings #f)
(print-to stderr "Warning: you'r requested inexact math usage by default,")
(if algebra
(print-to stderr " calculated results may be inaccurate.")
(print-to stderr " but the shared library is not loaded, so that kind of math won't be included."))))

; array? / scalar?
(define (Array? obj)
(or (vector? obj)
(tensor? obj)))
(define Scalar? scalar?)

; configurable exact/inexact constructors
(define Vector Vector)
(define Matrix Matrix)
(define Tensor Tensor)

; configurable exact/inexact constructors
(define Vector~ Vector~)
(define Matrix~ Matrix~)
(define Tensor~ Tensor~)
(define Scalar? number?)
(define (Vector? obj) ()) ; TBD.
;Matrix?
;Tensor?

; todo: make returning rows from matrices, etc
(define (Ref array . index)
Expand Down Expand Up @@ -192,8 +192,8 @@
then
(define number (apply Ref (cons obj index)))
(if first?
(format-number number tl 10)
(cons* #\space (format-number number tl 10)))
(format-any number tl)
(cons* #\space (format-any number tl)))

else
(define last (car indices))
Expand Down
8 changes: 4 additions & 4 deletions otus/algebra/config.scm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
(import (otus lisp))
(begin
(define (getenv str)
(let ((value (syscall 1016 str)))
(let*((str (c-string str))
(value (syscall 1016 str)))
(if (string? value) value "")))

(define string-ci= string-ci=?)
Expand All @@ -17,14 +18,13 @@
((string-ci= value "False") #F)
(else default))))

; algebra config
(define config {
; exactness of Vector, Matrix, Linspace, etc.
'default-exactness ; EXACT by default
(bool "OTUS_ALGEBRA_DEFAULT_EXACTNESS" #T)
(bool "OTUS_ALGEBRA_DEFAULT_EXACTNESS" #T)

; no warning for 'libol-algebra not found' and others
'no-startup-warnings ; show warnings by default
(bool "OTUS_ALGEBRA_NO_STARTUP_WARNINGS" #F)
})))

; todo: make Vector, Matrix, etc... config dependent
Loading

0 comments on commit a125f36

Please sign in to comment.