ppittle / pMixins

pMixins - Mixin framework for C#
http://pMixins.com
Apache License 2.0
23 stars 5 forks source link

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

Open dzmitry-lahoda opened 9 years ago

dzmitry-lahoda commented 9 years ago

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.