Skip to content

Commit

Permalink
Add Dot method
Browse files Browse the repository at this point in the history
  • Loading branch information
WinPooh32 committed Jan 20, 2023
1 parent ac5acfd commit 4bf6bf2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
23 changes: 23 additions & 0 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,29 @@ func (d Data) Min(r Data) Data {
return d
}

// Dot returns scalar of vectors production.
func (d Data) Dot(r Data) DType {
valuesL := d.values
valuesR := r.values

if EnabledAVX2 {
return vek.Dot(valuesL, valuesR)
}

if len(valuesL) != len(valuesR) {
panic("sizes of values at series must be equal")
}

var dot DType

for i := range valuesL {
vL, vR := valuesL[i], valuesR[i]
dot += vL * vR
}

return dot
}

func (d Data) AddScalar(s DType) Data {
values := d.values

Expand Down
50 changes: 50 additions & 0 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1652,3 +1652,53 @@ func TestData_Cumsum(t *testing.T) {
})
}
}

func TestData_Dot(t *testing.T) {
type fields struct {
freq int64
index []int64
values []DType
}
type args struct {
r Data
}
tests := []struct {
name string
fields fields
args args
want DType
}{
{
name: "simple",
fields: fields{
values: []DType{0, 1, 2, 3},
},
args: args{
r: MakeValues([]DType{-1, 2, -3, 4}),
},
want: 8,
},
{
name: "with n/a",
fields: fields{
values: []DType{0, NaN, 2, 3},
},
args: args{
r: MakeValues([]DType{-1, 2, -3, 4}),
},
want: NaN,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := Data{
freq: tt.fields.freq,
index: tt.fields.index,
values: tt.fields.values,
}
if got := d.Dot(tt.args.r); !fpEq(got, tt.want, EpsFp32) && !(IsNA(got) && IsNA(tt.want)) {
t.Errorf("Data.Dot() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 4bf6bf2

Please sign in to comment.