SERVING THE QUANTITATIVE FINANCE COMMUNITY

 
User avatar
Cuchulainn
Topic Author
Posts: 62586
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C# Quiz

November 7th, 2013, 9:50 am

C# generics are different from C++ templates. Ideally I would prefer not to have to use dynamic (plan B is code emit...)Any good solutions?
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
BramJ
Posts: 372
Joined: January 10th, 2006, 2:01 pm

C# Quiz

November 7th, 2013, 2:01 pm

Links to some alternatives can be found here.Note that the lack of a clean solution is not a .NET thing, as you can define in F#:let inline add (x : ^a) (y : ^a) : ^a = x + yWithout all the "^a" type annotations F# will still accept this, but then the function signature becomes^a -> ^b -> ^c when ( ^a or ^b) : (static member ( + ) : ^a * ^b -> ^c)instead of ^a -> ^a -> ^a when ^a : (static member ( + ) : ^a * ^a -> ^a)
Last edited by BramJ on November 6th, 2013, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Topic Author
Posts: 62586
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C# Quiz

November 7th, 2013, 2:07 pm

C++ does not have this problem. F# and C# are from the same stable (IL code ..)I used both dynamic and opcodes for NumericMatrix<T>. It's just ugly and that's that.Plan B: 0. Have to do + etc. for each micky mouse class ........................................1. No generics for classes that need +, ..2. Just use doublesBah, It's almost a show-stopper IMO. edit: Bram, I wonder what the performance penalty of dynamic is?
Last edited by Cuchulainn on November 6th, 2013, 11:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Topic Author
Posts: 62586
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C# Quiz

November 8th, 2013, 6:27 am

In C# Nutshell they call it "Numerical Type Unification".
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Govert
Posts: 103
Joined: January 28th, 2006, 10:02 am

C# Quiz

November 8th, 2013, 9:27 am

Generic arithmetic is indeed problematic with .NET. F# has a compile-time approach, but that won't help you in C#.Google will find quite a few approaches using interface constraints. Here's one write-up: http://www.codeproject.com/Articles/853 ... lationsOne nice solution uses the Expression features in .NET 3.5: http://www.yoda.arachsys.com/csharp/gen ... rs.htmlThe advantage is that you don't need to write out IL opcodes, but the code is still JIT-compiled so the resulting performance is similar.I doubt the dynamic approach has acceptable performance.-GovertExcel-DNA - Free and easy .NET for Excel
Last edited by Govert on November 7th, 2013, 11:00 pm, edited 1 time in total.
 
User avatar
Cuchulainn
Topic Author
Posts: 62586
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C# Quiz

November 8th, 2013, 10:32 am

Yeah, done all thatIn the beginning I thought Matrix<T> was the only place but then I needed LU<T>, Cholesky<T> ga maar door.In .NET 4.0 they have complex numbers with yes, double and imaginary parts. Now I know why.Good old std::complex<T>
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Topic Author
Posts: 62586
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C# Quiz

November 8th, 2013, 11:55 am

1. Try default generic args foobar(T t = 0)... 2. Not possible T*T, but T *(dynamic) T is OK
Last edited by Cuchulainn on November 7th, 2013, 11:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
chocolatemoney
Posts: 322
Joined: October 8th, 2008, 6:50 am

C# Quiz

November 22nd, 2013, 12:20 pm

I am new to .net, recently started working with F# coming from Scala. I have a question on @BramJ post above: Statically resolved type parameters are powerful but require inlining.They work great but if used in abundance, wouldn't they lead to a significant expansion of the size of the compiled code?Is there a further workaround for that?
 
User avatar
Polter
Posts: 2526
Joined: April 29th, 2008, 4:55 pm

C# Quiz

November 22nd, 2013, 6:16 pm

In C++ the well-known and widely used solution is template hoisting; see Section 5.3 of the following (which also introduces generalized hoisting):http://www.stroustrup.com/SCARY.pdfSection 8, "Generalizing to Other Languages", comments on C#.Related:http://en.wikibooks.org/wiki/More_C++_I ... ateNotably, some of the bloat can be detected and avoided at the compiler (or, more precisely, optimizer) stage:"At least one compiler, Visual Studio 8, has advanced heuristics that can optimize for code bloat by reusing the body of assembly-level identical functions."http://lubomir.org/academic/MinimizingCodeBloat.pdfAt the same time, Section 7 of the aforementioned "SCARY" paper (incidentally, what an awesome name ;]), mentions the limitations to the compiler-level detection (and thus motivates its proposed generalized hoisting technique).
 
User avatar
Cuchulainn
Topic Author
Posts: 62586
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C# Quiz

November 22nd, 2013, 8:36 pm

Code bloat is least worrisome(buy more h/w). My brain's neurons are C++ template hard-wired and C# 'numerics' are just meshuggah.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
chocolatemoney
Posts: 322
Joined: October 8th, 2008, 6:50 am

C# Quiz

November 23rd, 2013, 11:57 am

QuoteOriginally posted by: CuchulainnCode bloat is least worrisome(buy more h/w). My brain's neurons are C++ template hard-wired and C# 'numerics' are just meshuggah.I am on a very tight budget: I'll soon celebrate the 5th b.day of my laptop. BTW: congratulations for the 25th anniversary of Datasim.
 
User avatar
Cuchulainn
Topic Author
Posts: 62586
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C# Quiz

November 23rd, 2013, 12:07 pm

Thank you CM, BTW it is 26thMaybe there is a hyrid solution: create the solution in C++/CLI (Managed C++) and it talks to both native C++ and C#!// At the end of the day, people work with double in 95% of case, so why bother with C# generics?
Last edited by Cuchulainn on November 22nd, 2013, 11:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
Cuchulainn
Topic Author
Posts: 62586
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C# Quiz

November 23rd, 2013, 7:25 pm

QuoteOriginally posted by: PolterIn C++ the well-known and widely used solution is template hoisting; see Section 5.3 of the following (which also introduces generalized hoisting):http://www.stroustrup.com/SCARY.pdfSection 8, "Generalizing to Other Languages", comments on C#.Related:http://en.wikibooks.org/wiki/More_C++_I ... ateNotably, some of the bloat can be detected and avoided at the compiler (or, more precisely, optimizer) stage:"At least one compiler, Visual Studio 8, has advanced heuristics that can optimize for code bloat by reusing the body of assembly-level identical functions."http://lubomir.org/academic/MinimizingCodeBloat.pdfAt the same time, Section 7 of the aforementioned "SCARY" paper (incidentally, what an awesome name ;]), mentions the limitations to the compiler-level detection (and thus motivates its proposed generalized hoisting technique).C++ != C#
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
 
User avatar
chocolatemoney
Posts: 322
Joined: October 8th, 2008, 6:50 am

C# Quiz

November 25th, 2013, 1:33 pm

I think on my side the most annoying missing feature of .net (or F# in the specific) is the lack of higher kinded-types: http://cs.hubfs.net/topic/None/59392
 
User avatar
Cuchulainn
Topic Author
Posts: 62586
Joined: July 16th, 2004, 7:38 am
Location: Amsterdam
Contact:

C# Quiz

November 25th, 2013, 2:09 pm

QuoteOriginally posted by: chocolatemoneyI think on my side the most annoying missing feature of .net (or F# in the specific) is the lack of higher kinded-types: http://cs.hubfs.net/topic/None/59392Can you give some examples? What are you trying to do exactly?Is Tuple such an example? // That F# code looks like Greek to me
Last edited by Cuchulainn on November 24th, 2013, 11:00 pm, edited 1 time in total.
Step over the gap, not into it. Watch the space between platform and train.
http://www.datasimfinancial.com
http://www.datasim.nl
ABOUT WILMOTT

PW by JB

Wilmott.com has been "Serving the Quantitative Finance Community" since 2001. Continued...


Twitter LinkedIn Instagram

JOBS BOARD

JOBS BOARD

Looking for a quant job, risk, algo trading,...? Browse jobs here...


GZIP: On