Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for set/array/vector/matrix proposal #79

Open
adsick opened this issue Nov 8, 2021 · 5 comments
Open

support for set/array/vector/matrix proposal #79

adsick opened this issue Nov 8, 2021 · 5 comments

Comments

@adsick
Copy link

adsick commented Nov 8, 2021

Hi, sometimes it is useful to operate not only on numbers, but on sets of numbers: vectors, matrices, ordered and unordered sets.
We can define operations for vectors as so:

[1, 2, 3, 4] * 7 = [7, 14, 21, 28]
[2, 4, 8] / 2 = [1, 2, 4]

[5, 8, 11] + 4 = [9, 12, 15]
[10, 7, 4] - 4 = [6, 3, 0]

for f(x) where x is number:
f([a1, a2, ..., an]) = [f(a1), f(a2), ..., f(an)]

and also some aggregate functions would be extremely useful e.g.
a = [1/2, 1/4, 1/8]
sum(a) = 7/8
avg, product, variance, min, max e.t.c.

[a1, a2, ..., an] * [b1, b2, ..., bn] = [a1 * b1, a2 * b2, ..., an * bn]
and more vector/matrix specific functions such as vector product...
what do you think of it?

@PaddiM8
Copy link
Owner

PaddiM8 commented Nov 8, 2021

I agree, this would be very useful. I should be able to implement this when I have time.

@adsick
Copy link
Author

adsick commented Nov 8, 2021

Maybe I should take a look on kalker's codebase and try to help with some stuff.
Is it complicated?

@PaddiM8
Copy link
Owner

PaddiM8 commented Nov 8, 2021

That would be cool. Basic vector support should be fairly simple, although the KalkNum struct probably needs some better way to deal with values of different types, so some architectural changes may be ideal, although probably not required initially to make this work. The implementation would involve:

  • Updating the lexer to support square brackets
  • Adding a vector type to the ast
  • Making the parser support vector expressions... in `parse_primary
  • Adding support for the vector types in the interpreter
  • Adding support for storing vectors in KalkNum (in the future KalkNum needs to be updated to support values of different types better, but initially it would probably work to deal with it the same way booleans are dealt with)

@PaddiM8
Copy link
Owner

PaddiM8 commented Jan 5, 2022

I'm trying to decide how to deal with the different kinds of multiplication operations with vectors. The Wolfram language seems to do the following:

  • * for element-wise multiplication, eg. [1, 2, 3] * [1, 2, 3] = [1, 4, 9]
  • . for dot product
  • × for cross product

The problem with this in kalker is that * and × are the same operator. *is automatically turned into × in the web version. I guess it would then be confusing if it did something else, as people could assume it would do the cross product instead. I feel like the "default" shouldn't be cross product though... Maybe it would be possible to replace * with a dot symbol instead of a cross when it's between two vectors, but that would require semantic information.

Edit: Solving this by using for multiplication instead of × (the web version changes * to , but × still works)

@PaddiM8
Copy link
Owner

PaddiM8 commented Jan 7, 2022

I have now implemented basic support for vectors/matrices.

Vectors are defined like (1, 2, 3) or [1, 2, 3]. You can do addition, subtraction, division, multiplication and power with another vector or a single number. The dot product is calculated for multiplication. I also added an average function that takes a vector: average([1, 2, 3]) or average(1, 2, 3), and modified min and max to take vectors. If a function is not made to handle vectors, the it applies the function on all the items like suggested in the issue. Vectors are primarily indexed like this: v⟦n⟧ which is equivalent of v[[n]], but can also be indexed by a lowered number v₁ which is equivalent of v_1.

Matrices are defined only with square brackets. New rows are defined either after a new line or a semicolon. Most operations are also possible here, but with some limitations to power operations. If a function is not made to handle matrices, the it applies the function on all the items. Matrices are indexed like this: m⟦a, b⟧.

Now it would be good to have some way of dynamically creating vectors. Maybe something like list comprehension in Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants