Having a matrix like this in C#
double[,] M
I would like to get the same fast manipulation of its content as Matlab does. In particular, having this code in Matlab:
for i = 1:N
M(i, 1:i) = 1;
I would like to have its equivalent in C# without a second loop. I'm not sure about this, but as far as I know, Matlab uses a process called Vectorization for this line M(i, 1:i) = 1, which is faster than me implementing a for loop from 1 to i setting each cell to 1. Maybe I'm wrong, please correct me.
So how can achieve a fast manipulation of matrices in C# like Matlab.
A common solution is to use a matrix library like math.net numerics for matrix operations.
Related
I started using the MathNet Numerics Library and I need it to calculate the largest Eigenvalues corresponding to their Eigenvectors of my adjacency matrix.
When using large amount of points my adjacency Matrix gets quite big (i.e. 5782x5782 entries)
Most of the entries are '0' so I thought I could use the 'SparseMatrix'. But when I use it, it still takes ages for computation. In fact I never really waited that long until its finished.
I tried the whole thing in matlab and there wasn't any problem at all. Matlab solved it within a few seconds.
Do you have any suggestions for me?
Here is what I'm doing:
// initialize matrix and fill it with zeros
Matrix<double> A = SparseMatrix.Create(count, count, 0);
... fill matrix with values ...
// get eigenvalues and eigenvectors / this part takes centuries =)
Evd<double> eigen = A.Evd(Symmetricity.Symmetric);
Vector<Complex> eigenvector = eigen.EigenValues;
Math.Net Numerics's implementation is purely C# based. Therefore, performance may not be on-par with tools such as MATLAB since they mostly rely on native and highly optimized BLAS libraries for performing numerical computations.
You may want to use the native wrappers that come with Math.Net that leverage highly optimized linear algebra libraries (such as Intel's MKL or AMD's ACML). There is a guide on this MSDN page that explains how to build Math.NET with ACML support (look under Compiling and Using AMD ACML in Math.NET Numerics).
I learned basic algorithms on visual C# in highschool, and I made a simple code that numerically integrates a math function within given limits.
I want to be able to change the function the code integrates without actually editing the code, so I googled it for a while and found a lot of articles about how to do it. I tired to understand it but the problem is I can't understand any of what's written there because it's too much above my level.
I need a code that can add code on the run from a string containing a math function, that can accept a variable, log, ln, powers, sin, cos, tan and maybe pi and e, that is ready in a friendly "copy-paste" format, followed by instructions on where to paste it, and how to connect it to my code. To clarify:
I want to take something like this:
string s = "Sqrt(ln(1 + x ^ 2))";
and make it like this:
double x = 0;
double y = Math.Sqrt(Math.Log(1 + Math.Pow(x,2)));
I know it's a pretty annoying request and if it's not the right place to ask such a thing I apologize in advance.
This is actually fairly difficult to do in a language like C#, as it's statically compiled.
A good alternative would be to use an expression parsing library, such as NCalc. This library would allow you to create the expression (your string), parse it, and extract the result.
I have a matrix double[800][800] and I need to find largest eigenvalue of this matrix and corresponding eigenvector. As I managed to find, power iteration method is the best one for me, as my matrix is close to singular and standard functions can't help me.
Does anyone know working code for this aplication of power iteration method?
did you check the next :
http://www.bluebit.gr/net/
did you test C# to see if it can handle this size ? ( here show how )
Take a look at ARPACK. The original code was Fortran, which you should be able to find on the Interwebs to download. Googling a bit seems to show that there are interfaces or maybe translations in other languages. I see something about C# and ARPACK [1].
[1] http://sweb.cityu.edu.hk/kincau/blog/files/0e9a2b646554191e9ddc831b697cf04d-1.html
My function always looks like this: y = beta1 / (1 + exp(beta2 + beta3 * x)). With the data it can get, it always looks like a mirror of that (it starts with high values, then decreases)
I have the values beta1, beta2 and beta3. Now I need to calculate the point of inflection. I guess I could do it with an algorithm, but are there any functions provided by a library I could use? (Currently I cannot use Accord.NET because I still have C# 3.5 and it does only support 4.+)
update I'd be fine with a method to take the derivative ;)
Best regards
What you need is second derivation, and if the function only differs in constants, you can pre-compute it somewhere, and then only fill in constants.
Here you are: wolfram
There is no standard library function that derivates for you.
No need to write an algorithm: the second derivative is null for x = - beta2 / beta3. There is a close solution to this problem.
I've written a simple graphing implementation in C#, and I can graph things by comparing each pixel to the position on the graph it represents and plugging that position into the function I have to see if it is on the curve. That's all well and good.
The problem I'm having is USING a generated taylor polynomial. For example, I am able to create the nth taylor polynomial of a transcendent function f centered at c by doing
summation of this from 0 to to n with the counter variable being k = ((kth derivative of f(c)) * (x-c)^k)/k!
I am not sure how to do math markup on stackoverflow nor am I too competent with doing that on the web, but I hope that is understandable. The left side could be written as sigma _k=0 ^n or something like that with _ representing the section under sigma and the ^ representing the part above...
So I end up generating a Taylor polynomial of the 6th degree for cos(x) centered at 0(maclaurin, I know) that looks something like
"1 - x^2/2! + x^4/4! - x^6/6!"
This can be done through simple string manipulation in C#. I can just loop through and add the next term to the string.
I really can't comprehend how I would actually be able to use the string as a function to compare to graph positions to see if that graph position is actually on this graph to therefore graph it. So essentially: How would I use a string as an actual mathematical function in C#, or is there a better way of doing this.
Really sorry if it's confusing...really trying my best to explain it in a way that people can help.
You need a parser of string -> function. See MathParser for an example, that probably does everything you mentioned you need.
From a general perspective, anytime you want to convert a string into something that does work, you have to implement a parser, which will interpret the string and perform the actions dictated by it. For mathematical formulas, an expression tree may be of use to maintain order of operations and grouping. There are probably some math expression libraries available that will do this, or you can roll your own. This is not a trivial task, but it's certainly possible.
Once you have the expression tree, to figure out if a value f(x) for a given x is graphable, just evaluate it. For an f(x) graph, you can test x first to see if it falls in the visible domain of the graphing area. If it does, evaluate f(x) and if the point (x,f(x)) is graphable then draw the point.