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

Fix scalar multiplication #51

Closed
wants to merge 1 commit into from
Closed

Conversation

bezineb5
Copy link
Contributor

Hello,
I found an issue in the Mul operator, when the 1st term is a scalar, it always returned the 1st term unchanged.
The actual problem seems to be in defaultengine_arith.go, in MulScalar, where the leftTensor parameter is not properly handled (seems that on line 669, it calls Mul in a way that doesn't change the output). However, this is generated code, so I'm not sure how to fix it. Plus, it's actually simpler to commute the 2 terms.
What was the rationale behind the use of leftTensor?

Thanks!

@coveralls
Copy link

coveralls commented Oct 20, 2019

Coverage Status

Coverage increased (+0.02%) to 72.809% when pulling f719761 on bezineb5:scalar-mul into d78b17f on gorgonia:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.003%) to 72.794% when pulling f719761 on bezineb5:scalar-mul into d78b17f on gorgonia:master.

@chewxy
Copy link
Member

chewxy commented Oct 20, 2019

Two things

1. You Found A Bug

You have found a bug! Thank you! The bug is highly likely related to the issues you found, but not what you described.

The test cases you wrote highlights the bug:

	a2 := New(WithBacking([]float64{2}))
	b2 := New(WithBacking([]float64{3}))
	var correct interface{} = 6.0

	res, err := Mul(a2, b2)
	if err != nil {
		t.Fatalf("Error: %v", err)
	}
	assert.Equal(t, correct, res.Data())
	t.Logf("a2 %v b2 %v, res %v", a2, b2, res)

and

	a := New(WithBacking([]float64{3, 2}))
	b := New(WithBacking([]float64{2}))
	correct := []float64{6, 4}

	res, err := Mul(a, b)
	if err != nil {
		t.Fatalf("Error: %v", err)
	}
	assert.Equal(t, correct, res.Data())
	t.Logf("a %v b %v, res %v", a, b, res)

	// Test commutativity
	res, err = Mul(b, a)
	if err != nil {
		t.Fatalf("Error: %v", err)
	}
	assert.Equal(t, correct, res.Data())
	t.Logf("a %v b %v, res %v", a, b, res)

The solution however is not to fix the API, but to fix the defaultEngine 's arith functions, which are as you say, generated.

Specifically, one line short of 666: https://github.com/gorgonia/tensor/blob/master/defaultengine_arith.go#L665

This problem is easily fixed. But for now I'd like to close this PR, write up an issue, then attribute the fix to you

2. Email Me

Please email me - chewxy [at] gmail.com

@chewxy chewxy closed this Oct 20, 2019
@chewxy chewxy mentioned this pull request Oct 20, 2019
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

Successfully merging this pull request may close these issues.

None yet

3 participants