kunde21 / numgo Goto Github PK
View Code? Open in Web Editor NEWn-dimensional array implementation in Go.
License: BSD 3-Clause "New" or "Revised" License
n-dimensional array implementation in Go.
License: BSD 3-Clause "New" or "Revised" License
After issue #8 I though this might be a point to bring up.
How would you go about the naming of the files and distributing the code in them? I suppose the "general" stuff that applies to any array type would go into numgo.go. For the Arrayb-specific methods we have bool.go. Where would you like the Array64-specific code?
It might be clearer to just have
Just an idea, I'm open for any other suggestions
For various mathematical applications it would be useful to have a reflection method that mirrors the array around a given axis.
For a 2D example:
1 2 3
4 5 6
7 8 9
reflected around the Y-axis, this should result in:
3 2 1
6 5 4
9 8 7
I'm trying to implement this for n-dimensional objects. The operation is simple: leave all indices as they are, except the ones of the given axis which equal (size at that axis)-(original position)
For a 2D matrix that could be implemented as:
func (a *Array64) ReflectX() {
tmp := a.C()
for x := 0; x < int(a.shape[0]; x++ {
for y := 0; y < int(a.shape[1]; y++ {
val := a.Get( x,y)
tmp.Set( a.shape[0] - x ,y )
}
}
a = tmp
}
func (a *Array64) ReflectY() {
tmp := a.C()
for x := 0; x < int(a.shape[0]; x++ {
for y := 0; y < int(a.shape[1]; y++ {
val := a.Get( x,y)
tmp.Set(x, a.shape[1] - y )
}
}
a = tmp
}
Since I can't make a method for every axis (unknown number of axis), I need a general method that takes the axis to flip around as parameter. Also I can't add a nested for loop for every axis, since I don't know how many there are.
// Reflect mirrors the array at around a given axis
func (a *Array64) Reflect(axis int) {
if len(a.shape) < axis {
/*handle error, invalid axis */
}
tmp := a.C()
for i := 0; i < int(a.shape[axis]); i++ {
val := a.Get(/*other axis before*/, i, /*other axis following*/)
tmp.Set(val, /*other axis before*/, a.shape[axis] - i /*other axis following*/)
}
a = tmp
}
I have trouble filling in the blanks (see comments in code above). How do I go about implementing this?
More generally: How do I loop through all axis with indexes?
Hi,
All other methods work with ints, only Shape() returns a []uint64 which makes it unnessesary complicated to use.
Consider this code:
rows := myArr.Shape()[0]
collums :=myArr.Shape()[1]
depth :=myArr.Shape()[2]
newArr := numgo.NewArray64(nil,rows,collums,depth) //ERROR
It throws an error because NewArray64 expects ints, and Shape returns uint64.
To avoid using casts to int all the time, wouldn't it be easier if Shape just returns int so that it is directly compatible with the other functions?
If interested I would submit a pull request.
Let me know what you think!
Pablo
Hi,
In numgo, fmt.Println(numgo.Arange(0, 10)) => [0 1 2 3 4 5 6 7 8 9 10].
In numpy, print(np.arange(0, 10)) => [0 1 2 3 4 5 6 7 8 9].
Both are ok, but I prefer to drop 'stop', like numpy.
Would you think about it?
Tks!
There is a lot of redundancy in the Array64 and Arrayb methods. Quite a few of them are exactly the same, except for the input/return type.
Consider e.g. the Reshape() method of Array64 and Arrayb: The are identical, the only difference beeing in the declaration:
func (a *Arrayb) Reshape(shape ...int) *Arrayb {
...
}
vs.
func (a *Array64) Reshape(shape ...int) *Array64 {
...
}
This is not specific to this example and applies for a lot of code.
To simplify the code it would be possible to add a general n-Dimensional structure independent to the underlying type. The type of data (bool for Arrayb and float64 for Array64) would be encapsulated in a interface.
This would be a possible implementation:
type nDimObject struct {
shape []int
strides []int
err error
debug, stack string
data []nDimElement
}
type nDimElement interface {
}
// Array64 is an n-dimensional array of float64 data
type Array64 struct {
nDimObject
}
// Arrayb is an n-dimensional array of boolean values
type Arrayb struct {
nDimObject
}
Methods that are identical for both Array types could be implemented on nDimObject that way.
I will create a pull request on a separate branch. It sure removes a lot of code and simplifies it (in my opinion) a lot.
I understand this is a major change to the structure of the project and you me want to go in a different direction. Let me know what you think of the idea!
P.S. this approach would make it a lot easier to implement new types of array (maybe for ints?) in the future if needed.
hi,
perusing the code, I just saw:
https://github.com/Kunde21/numgo/blob/master/accessors.go#L25
b = create(a.shape...)
for i, v := range a.data {
b.data[i] = v
}
If I am not mistaken, it should probably just read:
b = create(a.shape...)
copy(b.data, a.data)
this should compile to a straightforward memcopy, instead of the many bound checks of the first version.
Hi,
I have been working with your package a while and noticed a few inconsistencies in the signatures of the functions used to create different types of arrays.
Consider these three functions:
func NewArray64(data []float64, shape ...int) (a *Array64)
func Full(val float64, shape ...int) (a *Array64)
func RandArray64(base, scale float64, shape []int) (a *Array64)
Arguments
All Functions accept variadic inputs. Why is RandArray asking for a slice instead?
Naming:
Why is the function for filled arrays not called FullArray64, like the others?
If you are iinterested in changing this, I would submit a pull request.
My proposal would be:
func NewArray64(data []float64, shape ...int) (a *Array64)
func NewFullArray64(val float64, shape ...int) (a *Array64)
func NewRandArray64(base, scale float64, shape ...int) (a *Array64)
Let me know what you think!
Hi,
this is just for discussion, not a real bug or issue.
Is the use of a "internal" package needed/useful? I find it unnecessarily complicated to contribute, for every change I have to fork, pull fork, change package import path to my user, make changes, change package import back, push. Otherwise I get an error when trying to run the tests:
binaryplease➜github.com/binaryplease/numgo(master)» go test .
package github.com/binaryplease/numgo
imports github.com/Kunde21/numgo/internal: use of internal package not allowed
This might be a stupid mistake as I'm pretty new to go. Is there a common workaround for this rather than changing the import path in the code forth and back all the time? I must admit I don't really understand the code in that folder very well though.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.