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

Matrix: remove limitation to numerical entries #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

telephon
Copy link
Contributor

@telephon telephon commented Aug 26, 2022

This is experimental and will need careful checking.

For prior discussion see: #38

There are also improvements to error handling in the Matrix class.

I am not the expert user for matrices, so it would be good if others could write up tests.
We can put them into unit tests once we have collected them.

Here is some code that makes testing easy:

(
~runOnServer = { |func, callback, value = 1.2|
	var shape;
	{ 
		var result = func.(DC.ar(value));
		shape = result.shape;
		if(shape.isNil) { result } { result.flat }
	}.loadToFloatArray(0.001, action: { |array|
		if(shape.isNil) {
			callback.value(array.first)
		} {
			callback.value(array.keep(shape.asArray.product).reshape(*shape))
		}
	});
};
~compareResults = { |func, callback, value|
	var sclangResult = func.value(value);
	~runOnServer.(func, { |scsynthResult| callback.value(scsynthResult, sclangResult) }, value)
	
};
~sameValue = { |func, callback, value|
	~compareResults.(func, { |scsynthResult, sclangResult|
		var same;
		scsynthResult = scsynthResult.round(0.001);
		sclangResult = sclangResult.round(0.001);
		scsynthResult.postln;
		sclangResult.postln;
		same = scsynthResult == sclangResult;
		"The results are %the same.".format(if(same.not) { "not " } { "" }).postln;
		callback.(same)
	}, value);
	
};
~sameMatrixValue = { |func, callback, value|
	~compareResults.(func, { |scsynthResult, sclangResult|
		var same;
		scsynthResult = Matrix.with(scsynthResult).round(0.001);
		sclangResult = sclangResult.round(0.001);
		scsynthResult.postln;
		sclangResult.postln;
		same = scsynthResult == sclangResult;
		"The results are %the same.".format(if(same.not) { "not " } { "" }).postln;
		callback.(same)
	}, value);
	
};
)

(
~sameMatrixValue.({ |value|
	var a, b;
	a = Matrix.fill(4, 5, { value });
	b = Matrix.fill(5, 4, { value });
	a.mulMatrix(b)
}, value: 1.2)
)


(
~sameMatrixValue.({ |value|
	var a, b;
	a = Matrix.fill(4, 4, { value });
	b = 10;
	a * b
}, value: 1.2)
)



(
~sameMatrixValue.({ |value|
	var a, b;
	a = Matrix.fill(4, 4, { value });
	b = Matrix.fill(4, 4, { value });
	a + b
}, value: 1.2)
)

(
~sameMatrixValue.({ |value|
	var a, b;
	a = Matrix.fill(4, 4, { value });
	b = Matrix.fill(4, 4, { value });
	a - b
}, value: 1.2)
)


(
~sameMatrixValue.({ |value|
	var a, b;
	a = Matrix.fill(4, 4, { value });
	b = Matrix.fill(4, 4, { value });
	a mul: b
}, value: 1.2)
)

(
~sameMatrixValue.({ |value|
	var a;
	a = Matrix.fill(4, 4, { value });
	a.adjoint
}, value: 1.2)
)

(
~sameValue.({ |value|
	var a;
	a = Matrix.fill(4, 4, { value });
	a.det
}, value: 1.2)
)


(
~sameValue.({ |value|
	var a;
	a = Matrix.fill(4, 4, { value });
	a.cofactor(1, 1)
}, value: 1.2)
)


- removed checks
- simplified syntax
- standardized error handling
@telephon telephon changed the title remove limitation to number Matrix: remove limitation to numerical entries Aug 26, 2022
@telephon
Copy link
Contributor Author

As far as I can see, the main trouble occurs with the overridden standard methods like collect and put, which are used in UGen math.

@telephon
Copy link
Contributor Author

Here are examples of what doesn't work:

a = Matrix.with([[3.0, 2], [1.2, 3]] * DC.ar(1.2));
b = 8;
a + b // OK
b + a // OK
b = DC.ar(8);
a + b // fails
b + a // fails
b = DC.kr(8);
a + b // fails
b + a // fails

There is nothing inherent about this, it could work, but there are so many overridden non-standard methods in Matrix that I think it might be better to start with a new class, or even add the matrix methods to Array itself.

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.

1 participant