Skip to content
This repository has been archived by the owner on Apr 13, 2020. It is now read-only.

Define recipe to do template/type class metaprogramming if possible #39

Open
dzmitry-lahoda opened this issue Jan 13, 2015 · 0 comments

Comments

@dzmitry-lahoda
Copy link

Given code

  interface INum 
    {
      INum op_Multiply(INum a,INum b);
      INum op_Addition(INum a,INum b);
    }
}

and

partial class MyMath{
    INum SumOfSquares(INum  a,INum  b)
{
   return a.op_Multiply(a).op_Addition(b.op_Multiply(b));
}
}

generate

partial class MyMath
{
      int SumOfSquares(int  a,int  b)
{
   return a*a + b*b;
}

      float SumOfSquares(float  a,float  b)
{
   return a*a + b*b;
}

// also this subject to what numeric `casts`
 int SumOfSquares(int  a,float  b)
{
   return a*a + b*b;
}

}

Some items to consider:

op_* names in .NET:

http://msdn.microsoft.com/en-us/library/dd233204.aspx

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.matrix.op_addition.aspx

Interfaces implemented by numeric values:

public struct Int32 : IComparable, IFormattable, 
    IConvertible, IComparable<int>, IEquatable<int>

Approach with defining 2 pseudo numeric of 2 size with implicit/explicit operators:

https://github.com/asd-and-Rizzo/matrixextensions/blob/master/MatrixExtensionsCodeBase/NumericBroad.cs

https://github.com/asd-and-Rizzo/matrixextensions/blob/master/MatrixExtensionsCodeBase/NumericNarrow.cs

some matrix operations

https://github.com/asd-and-Rizzo/matrixextensions/blob/master/MatrixExtensionsCodeBase/NumericArray2DManipulationExtensions.cs

and generate code for each defined type (int,float,complex) etc.

https://github.com/asd-and-Rizzo/matrixextensions/blob/master/MatrixExtensionsCodeTransformator/Program.cs

Runtime code generation http://www.yoda.arachsys.com/csharp/genericoperators.html

Kind of allowing to write down generic numeric algorithms.

I know that there are not so much of really generic algorithms in the end cause numeric sized influence algorithms, performance, precisions, but this may be useful for some implementations.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant