0

I have written mathematica file that does manipulations on a certain set of points. I would like to access code that someone else has written for a least square fit in C++. How do I access this C++ in mathematica?

Some specifics about the code: The least square fit code is a function that accesses a .cpp file and two different .h files. It is a void function that takes in a vector of points, the point itself, and an integer, each called by reference.

I make this specific because I am a novice in coding in c++. I have seen in another post someone say that they have :

I think we have these:

MathLink communication through pipes/files LibraryLink

I do not have any of those things, nor do I know what they are. Can someone explain how a novice like myself can handle this situation.

Also, from what I saw from other posts, the function I have to access is much more complicated. It looks something like:

void function1(vectorFoo &thing, Foo &variable1, int64_t &variable2) {

    stuff happens here
}

(the the first argument of the function is a vector that takes in Foo between "<" and ">" but that formatting won't let me show that. that's why I wrote "vectorFoo")

this function accesses other functions,call them "function2", "function3", and other named variables (if that is the right technical term...like I said, I don't know much c++) from three files. so even if I call this file, will I have to worry about calling the other three files from which it draws? Please help!

nycguy92
  • 181
  • 3
  • 1
    Regardless of the accepted answer there, today, generally the best way is LibraryLink. – Szabolcs Jul 06 '15 at 20:32
  • 2
    You're aware that Mathematica knows all about least squares fits (and other types of fits)? – Sjoerd C. de Vries Jul 06 '15 at 20:34
  • Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory Tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Jul 06 '15 at 20:36
  • I am aware about Mathematica's least square capabilities. They are not suitable for my purposes. I saw the other post about implementing C++ files on Mathematica, but the person who posted the question seems to have more of a setup than I do. I am starting from ground zero. – nycguy92 Jul 07 '15 at 12:51
  • 2
    "They are not suitable for my purposes." - can you explain why? – J. M.'s missing motivation Jul 07 '15 at 12:54
  • i am not fitting to a line but a circle – nycguy92 Jul 07 '15 at 13:07
  • "I do not have any of those things, nor do I know what they are." <-- then you need to start by looking them up. The linked question has detailed answers for explaining some of these. Calling C from Mathematica is not trivial, and it is going to take some time to learn it. The linked question has the basics, plus links to the documentation. That's where you need to start. First learn the basics, using simple examples. When you're comfortable with that, only then move on to solving your more complicated problem. – Szabolcs Jul 07 '15 at 14:05
  • If you have problems understanding the answers in the linked question, you need to ask specific questions about the things that you had trouble with instead of a general one about how to call C++ from Mathematica. The general question is already answered in the other thread, in as much detail as possible on this site. – Szabolcs Jul 07 '15 at 14:09
  • "but a circle" - this is doable in Mathematica. Search around here; there have been at least two questions about circle fitting on this site. – J. M.'s missing motivation Jul 07 '15 at 14:37
  • 2
    The following one-liner with a list of your coordinate pairs as input in the variable pts finds the circle parameters for you: NMinimize[{((#1 - mx)^2 + (#2 - my)^2 - r^2)^2 & @@@ pts // Total, mx > 0 && my > 0 && r > 0}, {mx, my, r}] – Sjoerd C. de Vries Jul 07 '15 at 14:38
  • so if my list of points is

    points = {{x1,y1},{x2,y2},{x3,y3},...}

    then I should write

    NMinimize[{((#1 - points[[i]][[1]])^2 + (#2 - points[[i]][[2]])^2 - r^2)^2 & @@@ pts // Total, points[[i]][[1]] > 0 && points[[i]][[2]] > 0 && r > 0}, {points[[i]][[1]], points[[i]][[2]], r}]

    What do I put for #1, #2, and r, given that I only have a list of points?

    – nycguy92 Jul 07 '15 at 20:16
  • All you have to do is set pts to your list of points before running @Sjoerd's snippet. – J. M.'s missing motivation Jul 08 '15 at 02:47
  • Indeed, don't change anything in the code I posted. Just start with pts ={{1,0},{0.7,0.71}, ... ,{-1,0}} or whatever your data may be. Then execute the above code as is. The result will be the circle's center given by mx and my and its radius r. – Sjoerd C. de Vries Jul 08 '15 at 05:55
  • Alternatively, if you have your points in a differently named variable change the variable pts in my code to the one you're using (apparently, points). That's the only change needed in that case. – Sjoerd C. de Vries Jul 08 '15 at 06:05

0 Answers0