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

Any interests in element wise operations on vector/matrix with F#? #450

Merged
merged 8 commits into from
Nov 20, 2016

Conversation

albertp007
Copy link
Contributor

…hose defined as operators in F#) in Matrix and Vector and also added implementations for all the subtypes
…ix and Vector generic types, so that these can be called from F# as if they are normal mathematical operators
…s it to return an integer and is different from the pointwise definition.
… Exp() respectively so that they work with F# mathematical operator syntax
Copy link
Member

@cdrnet cdrnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I've added some comments inline of some mostly cosmetic things we should address at latest before release.

@@ -459,6 +459,75 @@ protected override void DoPointwiseLog(Matrix<Complex> result)
Map(Complex.Log, result, Zeros.Include);
}

protected override void DoPointwiseAbs(Matrix<Complex> result)
{
Map(x => (Complex)Complex.Abs(x), result, Zeros.Include);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all operations where f(0) = 0 we can allow Map to skip zeros by specifying Zeros.AllowSkip. This is relevant on sparse matrices, where all the zero entries can be skipped this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it, thanks! Will fix it for abs, acos, asin, atan, ceiling, floor, round, sin, sinh, sqrt, tan and tanh

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, not acos...

f(result);
}

public Matrix<T> PointwiseBinary(Action<Matrix<T>, Matrix<T>> f, Matrix<T> other)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be public

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thanks! Will fix.

throw new ArgumentException(Resources.ArgumentVectorsSameLength, "y");
}

var result = Build.SameAs(this);
Copy link
Member

@cdrnet cdrnet Nov 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should consider y as well I think, i.e. Build.SameAs(this, y). Also it seems the other routines use other instead of y as parameter name - although that is not really consistent within the existing codebase either.

public void PointwiseExp(Matrix<T> result)
/// <param name="f"></param>
/// <returns></returns>
protected void PointwiseUnary(Action<Matrix<T>> f, Matrix<T> result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether we actually need these helpers, since we already do range checking within the Map function. Or we could use the unchecked Map implementations. Seems I need to spend some thoughts on the architecture here - but that should not block this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are really because I saw the checks done in all the other pointwise functions before this pull request. Since I am adding some 20 more of them, copy and pasting the checks is something I really want to avoid. True, I did not try to refactor the existing ones though. Yeah, please have a think and we can decide what to do here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. It's ok for now, but I'll think about it.

///
/// </summary>
/// <returns></returns>
public Matrix<T> PointwiseAbs()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to have proper documentation on all public members in the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being lazy here, will fix, thanks!

Copy link
Member

@cdrnet cdrnet Nov 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Yes, It's quite tedious; sorry for all the duplication (this would be less an issue with code gen, but we moved away from that a long time ago for good reasons).

@@ -305,5 +305,90 @@ public static Matrix<T> op_DotHat(Matrix<T> matrix, T exponent)
{
return matrix.PointwisePower(exponent);
}

public static Matrix<T> Log(Matrix<T> x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These will also need proper documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, will fix shortly, thanks!

public void PointwiseExp(Vector<T> result)
/// <param name="f"></param>
/// <param name="result"></param>
protected void PointwiseUnary(Action<Vector<T>> f, Vector<T> result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid adding empty documentation skeletons, that makes it hard to spot whether we're still lacking some proper documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, will fix and avoid going forward.

…he AllowSkip option in Map so that its more efficient for sparse matrices.
… consider both matrices when replicating so that a dense or sparse matrix is created accordingly
@cdrnet cdrnet merged commit 75c3b9f into mathnet:master Nov 20, 2016
@cdrnet
Copy link
Member

cdrnet commented Nov 20, 2016

Merged, thanks a lot!

I hope you do not mind that I let GitHub squash the commits together.

@albertp007
Copy link
Contributor Author

Thank you! Don't mind at all, would have been what I wanted to do anyways instead of having so many incremental commits. Glad to be able to contribute!

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.

2 participants