goplus / gop

The Go+ programming language is designed for engineering, STEM education, and data science. Our vision is to enable everyone to become a builder of the digital world.
https://goplus.org
Apache License 2.0
8.91k stars 546 forks source link

Simplified form of 2d vector #385

Open xushiwei opened 4 years ago

xushiwei commented 4 years ago
a := [[a11, a12, ..., a1N], [a21, a22, ..., a2N], ..., [aM1, aM2, ..., aMN]]

can be:

a := [a11, a12, ..., a1N; a21, a22, ..., a2N; ...; aM1, aM2, ..., aMN]

or:

a := [a11, a12, ..., a1N
a21, a22, ..., a2N
...
aM1, aM2, ..., aMN]
damonchen commented 4 years ago

I think

a := [a11, a12, ..., a1N; a21, a22, ..., a2N; ...; aM1, aM2, ..., aMN]

is better.

we could format the code to multiline when there is ';' by the format tool. so the code will look like below:

a := [a11, a12, ..., a1N;
a21, a22, ..., a2N;
...;
aM1, aM2, ..., aMN;
]

put the ']' in new line, so it will be easy for user to add new row in vector.

xushiwei commented 4 years ago

@damonchen No. In the Go language, token ; can be omitted at line end (\n). So

a := [a11, a12, ..., a1N; a21, a22, ..., a2N; ...; aM1, aM2, ..., aMN]

is equal to

a := [a11, a12, ..., a1N
a21, a22, ..., a2N
...
aM1, aM2, ..., aMN]

Of course, if we allow empty line in 2d vector, it can be

a := [
    a11, a12, ..., a1N
    a21, a22, ..., a2N
    ...
    aM1, aM2, ..., aMN
]
xushiwei commented 5 months ago
X := [A..., B..., c1, c2, ..., cN]
Y := [
    A...
    B...
    c1, c2, ..., cN
]
Z := [
    A1..., A2..., a1, a2, ..., aN
    B...
    C1..., C2...
    d1, d2, ..., dM
]

is equivalent to:

X := concat(A, B, [c1, c2, ..., cN])
Y := vconcat(A, B, [c1, c2, ..., cN])
Z := vconcat(
    concat(A1, A2, [a1, a2, ..., aN]),
    B,
    concat(C1, C2),
    [d1, d2, ..., dM])

prototype:

type matrix[T number] struct {
    Data []T
    Row, Col int
}

func matrix[T].icast(v []T) matrix[T] {  // T.icast means `implicit cast of type T`
    return matrix[T]{v, 1, len(v)}
}

func concat[T string](args ...T) T
func concat[T number](args ...[]T) []T
func concat(T number](args ...matrix[T]) matrix[T]

func vconcat[T number](args ...matrix[T]) matrix[T]

For more details about T.icast, see https://github.com/goplus/gop/issues/1847.