Is there any library or the code of a function in C++ that I can use for comparing numeric vectors in C++?
Just googled a little and found smth like that here I think you can use that to write your own which will work with numbers and vectors. Anyways, I dont know that topic but they say it is about strings(in other words letters). Also, I don't think for a project of this scale, speed will be an issue.
Related
As the title says, I'm trying to find a method to shuffle an array. C++Builder doesn't seem to have random_shuffle() and I don't know how to do it.
I can't find much about this program on google. To be more specific, I'm trying to do a memory matching game that has 16 pairs. I tried implementing something to give each card a random position (position 0, 1, 2, .. etc) but I don't know how to do it without having a value duplicated.
So the easiest method I see is creating an array a[]={0,1,2,...31} and shuffling the values somehow. Is it possible to do anything like this? If not, do I have any alternatives?
I hope my question is understandable.
The C++ Builder doesn't seem to have random_shuffle()
Yes, it does. Make sure you have #include <algorithm> in your code.
The standard algorithm to shuffle an array is the Fisher-Yates algorithm. That is easy to implement and there are a number of examples on the web. However, if you are learning then it would be a good idea to program it from scratch rather than blindly copy someone else's code.
I have a set of data z(0), z(1), z(2)...,z(n) that I am currently fitting with a 2 variables polynomial of the kind p(x,y) = a(1)*x^2+a(2)*y^2+a(3)*x*y+a(4). I have i=1,...,n (x(i),y(i)) coordinates that I impose to be p(x(i),y(i))=z(i). In this way I have a Overdetermined System that I can solve using Eigen SVD . I am looking for a more sophisticated method that can take care of outliers, like a Least Median of Squares robust regression (as described here) but I haven't found a C++ implementation for 2 variables. I looked in GSL but it seems there is nothing for 2 variable functions. The only other solution I can think of is using a TGraph2D in ROOT. Do you know any other solution? Numerical recipes maybe? Since I am writing C++ code I would prefer C or C++ implementations.
Since non answer has been given yet, but I am still working on this problem, I will share my progresses here.
The class TLinearFitter has a fit method that allows you to select Robust fitting - Least Trimmed Squares regression (LTS):
https://root.cern.ch/root/html532/TLinearFitter.html
Another possible solution, more time consuming maybe, but maybe more efficient on the long run is to write my own function to be minimized, and the use:
https://projects.coin-or.org/Ipopt to minimize it. Although in this approach there is a bigger "step". I don't know how to use the library and I haven't (yet?) found a nice tutorial to understand it.
here: https://wis.kuleuven.be/stat/robust/software there is a Fortran implementation of the LMedS algorithm called PROGRESS. So another possible solution could be to port this software to C/C++ and make a library out of it.
Is there any best approach or template to follow, while doing this?
I mean two things in particular, because for me it is problematic to imagine how it would go in c++:
expanding arrays on go, where they are expanded during program and i dont know whethere the final size will be e.g. 10 or 100000.
plots. I have never done any plot in c++ as I always have been doing it in matlab when necessary.
So what templates or rules should I follow, and how could I cope with those two things?
I found that eigen library would be useful for matrices (dynamically expanding also?), but as I am not sure, want to ask first to be sure of a right approach. Nothing i know about plots.
Please add some link for me to learn from, if useful.
Thanks!
expanding arrays on go, where they are expanded during program and i dont know whethere the final size will be e.g. 10 or 100000.
The solution to this is simple: look up std::vector (or std::deque) both provide this behaviour. (With "subtle" differences between a deque and a vector).
plots. I have never done any plot in c++ as I always have been doing it in matlab when necessary.
For this you'll have to search for a library that can do this, first you'll have to look into a graphical window library such as Qt. And then you'll have to look up some library that can plot data in a graph form.
Though for this matlab will probably always be the "easier/better" choice; C++ has nothing to help you with this.
Also remember: first learn the language, then learn libraries!
For plotting using QT, QWT is basically all you need as it provides all the non trivial kind of charts one may need.
I'm looking for a (space) efficient implementation of an LCS algorithm for use in a C++ program. Inputs are two random access sequences of integers.
I'm currently using the dynamic programming approach from the wikipedia page about LCS. However, that has O(mn) behaviour in memory and time and dies on me with out of memory errors for larger inputs.
I have read about Hirschberg's algorithm, which improves memory usage considerably, Hunt-Szymanski and Masek and Paterson. Since it isn't trivial to implement these I'd prefer to try them on my data with an existing implementation. Does anyone know of such a library? I'd imagine since text diff tools are pretty common, there ought to be some open source libraries around?
When searching for things like that, try scholar.google.com. It is much better for finding scholarly works. It turned up
http://www.biotec.icb.ufmg.br/cabi/artigos/seminarios2/subsequence_algorithm.pdf
this document, a "survey of longest common subsequences algorithms".
Not C++ but Python but I think usable.
http://wordaligned.org/articles/longest-common-subsequence
Hirschberg's Algorithm embeds a javascript implementation : almost C.
I created a program using dev-cpp and wxwidgets which solves a puzzle.
The user must fill the operations blocks and the results blocks, and the program will solve it. I'm solving it using brute force, I generate all non-repeated 9 length number combinations using a recursive algorithm. It does it pretty fast.
Up to here all is great!
But the problem is when my program operates depending the character on the blocks. Its extremely slow (it never gets the answer), because of the chars comparation against +, -, *, etc. I'm doing a CASE.
Is there some way or some programming language which allows dynamic creation of operators? So I can define the operator ROW1COL2 to be a +, and the same way to all other operations.
I leave a screenshot of the app, so its easier to understand how the puzzle works.
http://www.imageshare.web.id/images/9gg5cev8vyokp8rhlot9.png
PD: The algorithm works, I tried it with a trivial puzzle, and solved it in a second.
Not sure that this is really what you're looking for but..
Any Object Oriented language such as C++ or C# will allow you to create an "Operator" base class and then to derive from this base class a "PlusOperator" or "MinusOperator" etc'. this is the standard way to avoid such case statements.
However I am not sure this will solve your performance problem.
Using plain brute force for such a problem will result you in an exponential solution. this will seem to work fast for small input - say completing all the numbers. But if you want to complete the operations its a much larger problem with alot more possibilities.
So its likely that even without the CASE your program is not going to be able to solve it.
The right way to try to solve this kind of problems is using some advanced search methods which use some Heuristic function. See the A* (A-star) algorithm for example.
Good luck!
You can represent the numbers and operators as objects, so the parsing is done only once in the beginning of the solving.