denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.67k stars 5.38k forks source link

Math Module missing in deno #6135

Closed darthcoder closed 4 years ago

darthcoder commented 4 years ago

Hi,

I am a python developer who is highly interested in deno ever since Ryan's first talk on deno. I looked at go's std and deno's std and there is no math module in deno.

Would it be okay if I were to try and port go's math library to deno? I mean I am not very knowledgeable in either go or ts, but I am willing to learn plus most of go looks like c anyways.

Can someone give me a brief overview of how to do PR and stuff that I should know or a link where I could read about the best practices in deno land.

With Regards,

basyt

lucacasonato commented 4 years ago

Hi,

JavaScript has a built-in called Math: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math. It contains most of the math functions in the Go standard library math module. I think the best plan of action is to check what functions are missing from the Math global, and then decide depending on that if it warrants a std/math module. :smiley:

darthcoder commented 4 years ago

Supposing I find missing functions, how do I add them to deno? What is the process flow in that case? Do I just fork deno and submit PR's? or is it more involved than that?

lucacasonato commented 4 years ago

The flow is usually:

  1. Open an issue and discuss
  2. Fork & implement
  3. PR

There might be some more info in the contributing section of the docs.

darthcoder commented 4 years ago

thanks a lot for the quick help. I'll see what I can do :)

ry commented 4 years ago

@darthcoder I’m very interested in improving the state of math in JavaScript. We haven’t done much work in this area yet but let’s discuss! What algorithms do you want to add?

zhmushan commented 4 years ago

I want matrix and complex number.

pedropaulosuzuki commented 4 years ago

Most problems in JS math could be solved by the usage of operator overloading, which is unfortunately currently unavailable.

Example for sum of complex numbers:

class ComplexNumber {
    constructor(private real: number, private imaginary: number) {    }

    [Symbol.add](x: ComplexNumber, y: ComplexNumber) {
        return new ComplexNumber(x.real + y.real, x.imaginary + y.imaginary);
    }

    toString() {
        return `${this.real} + ${this.imaginary}i`;
    }
}

const a = new ComplexNumber(3, 12);
const b = new ComplexNumber(5, -5);

const sum = a + b; // 8 + 7i

Of course it is just a flawed made-up example, but operator overloading in JS/TS would work really well through the usage of Symbols to override class behavior on operations such as sum, multiplication, subtraction, unary operations, among other things.

crowlKats commented 4 years ago

@pedropaulosuzuki https://github.com/tc39/proposal-operator-overloading#why-not-use-symbols-instead-of-a-whole-new-dispatch-mechanism

pedropaulosuzuki commented 4 years ago

@pedropaulosuzuki https://github.com/tc39/proposal-operator-overloading#why-not-use-symbols-instead-of-a-whole-new-dispatch-mechanism

I don't like the "+"() syntax because we already have [Symbol.iterator], for example. And how can you tell if you are implementing a - b or if you are using -a just by the "-" sign. On Symbol, you could have Symbol.subtract and Symbol.negation, Symbol.add and Symbol.toNumber, which makes more sense to me.

darthcoder commented 4 years ago

@darthcoder I’m very interested in improving the state of math in JavaScript. We haven’t done much work in this area yet but let’s discuss! What algorithms do you want to add?

basically I am use python for scientific computations. so i was thinking that a good linear algebra package would be a nice start, later we can probably incorporate stuff modeled after numpy and scipy libraries. I have never really designed a library for use by other people so I am in the dark honestly and I don't really know js, ts, rust or go as I have been solely focused on scientific computing in python for quite some time.

do you think it would be worthwhile? there must already be packages for this stuff in js. also i was thinking that the std library might become too bloated if we have all that stuff.

sorry for the wall of text.

tldr: Math is missing erf, expm1, floatsfrombits, gamma fn, bessel fn, hypot and other utility functions that are available in the go library and not in the Math lib of js(as documented on MDN).

i think it would be a good start and practice for me to just add implementations of these functions to deno's std, and then I can look at more complex ideas as I am not looking to bite off more than I can chew.

What do you think? comments? any advice?

pedropaulosuzuki commented 4 years ago

@darthcoder I’m very interested in improving the state of math in JavaScript. We haven’t done much work in this area yet but let’s discuss! What algorithms do you want to add?

basically I am use python for scientific computations. so i was thinking that a good linear algebra package would be a nice start, later we can probably incorporate stuff modeled after numpy and scipy libraries. I have never really designed a library for use by other people so I am in the dark honestly and I don't really know js, ts, rust or go as I have been solely focused on scientific computing in python for quite some time.

do you think it would be worthwhile? there must already be packages for this stuff in js. also i was thinking that the std library might become too bloated if we have all that stuff.

sorry for the wall of text.

tldr: Math is missing erf, expm1, floatsfrombits, gamma fn, bessel fn, hypot and other utility functions that are available in the go library and not in the Math lib of js(as documented on MDN).

i think it would be a good start and practice for me to just add implementations of these functions to deno's std, and then I can look at more complex ideas as I am not looking to bite off more than I can chew.

What do you think? comments? any advice?

I don't think that should be part of Deno's standard library. I'm actually writing a statistics library aimed for Deno right now, but it is going through a small refactor, so the repo is still private for now, but things like Hypotenuse (a 2 + b 2) ** 0.5 are too easy to put into Deno std lib in my opinion. The Error and Gamma functions, however, could be of interest.

darthcoder commented 4 years ago

@darthcoder I’m very interested in improving the state of math in JavaScript. We haven’t done much work in this area yet but let’s discuss! What algorithms do you want to add?

basically I am use python for scientific computations. so i was thinking that a good linear algebra package would be a nice start, later we can probably incorporate stuff modeled after numpy and scipy libraries. I have never really designed a library for use by other people so I am in the dark honestly and I don't really know js, ts, rust or go as I have been solely focused on scientific computing in python for quite some time. do you think it would be worthwhile? there must already be packages for this stuff in js. also i was thinking that the std library might become too bloated if we have all that stuff. sorry for the wall of text. tldr: Math is missing erf, expm1, floatsfrombits, gamma fn, bessel fn, hypot and other utility functions that are available in the go library and not in the Math lib of js(as documented on MDN). i think it would be a good start and practice for me to just add implementations of these functions to deno's std, and then I can look at more complex ideas as I am not looking to bite off more than I can chew. What do you think? comments? any advice?

I don't think that should be part of Deno's standard library. I'm actually writing a statistics library aimed for Deno right now, but it is going through a small refactor, so the repo is still private for now, but things like Hypotenuse (a 2 + b 2) ** 0.5 are too easy to put into Deno std lib in my opinion. The Error and Gamma functions, however, could be of interest.

well i was simplifying. i don't mean to imply that i want generic functions polluting the namespace, but i just meant to say that the go math library has about 70 functions while js has like 30 and js also has the hypot function that i missed.

other than this the go standard library provides big,bits, rand and cmplx submodules which i can see as being useful.

when i said that i wanted to implement the math library, i didn't mean i wanted to write return sqrt(a^2 + b^2) but more along these lines. I am not a huge expert in either ts or js or go (even though go code is easy to read) but i was interested in the idea of deno and as someone who has never contributed code to an open source project I was looking for ideas about something i can do that is more helpful than wasteful.

I didn't mean to waste anyone's time and for that I am sorry. Like many people I am self-taught and have a family to support and a full time job that actively discourages the existence of free time. I am just looking to contribute to open source and to understand the modern workflow of an opensource project that doesn't have a lot of technical debt.

again sorry for the wall of text but I hope you understand.