I am dealing with the case of pattern recognition and for input, I need to read coordinates of points, up to 10000 points from a text file. I need to perform certain calculations on the points read. So, my question is whether I should always read them from the text file when I need to do some calculation or I should store them in some data structure, eg, a 2D array and hence access the values. What difference would there be in terms of storage and time?
Edit:
The language I am using is Java.
The data structure is user defined that has
a constructor that instantiates the object with x and y coordinates.
a method to draw the point on standard output.
a method to draw a line between two given points.
a method to compare the position of two points based on coordinates.
a method to compute the slope between two points.
an inner class extending Comparable interface that can be used to compare two points.
The comparison is based on slope made by each point w.r.t a reference point.
PS - I am sorry if the question is a silly one but just wanted to be clear about things than shying back. Thanks in advance!
Well storing them in the memory will be the recommended option here, assuming that its a static text file(meaning that the points don't change when you are running the recognition ) as it will really speed up the whole process and 10000 points is not too large to be cached in memory.
Not to mention this point also depends on the fact which language and data structure you are using.
Related
I'm developing a small app for Android with OpenGL ES 1.x. There is no glBegin-glEnd-functionality so one has to define vertex (and color and texcoord) arrays for the objects to be drawn, and then use matrix operations to move, scale, and rotate them. This works nicely for large objects, nothing to complain here...
However, if one want's to draw small, "temporary" objects (e.g. just a line from point A to point B), things get a bit annoying. I have thus created some small utility functions such as:
DrawHelper.drawLine(starting point, ending point)
I have noticed two possible ways to do this. My question is which of these versions is preferred? Since we are dealing with such simple helper functions, and both methods are easy and understandable, one might as well write them as good as possible from the start, even if the potential speed gain would be very low. So please no "benchmark and identify bottlenecks first".. =)
Method 1:
The draw helper has FloatBuffer containing the points (0,0,0) and (1,0,0). I draw this line every time with the appropriate modelview matrix in place transforming the two points to the desired locations.
Method 2:
The draw helper has a dummy FloatBuffer and I use FloatBuffer.put to feed in the new points every time.
Method 1 is clearly (?) better for larger objects such as circles or other geometric shapes. How about a simple line or a simple triangle?
You always choose the method that involves fewer work. Applying a matrix multiplication is takes a lot more computations than two vector assignments. Also the matrix transformation approach sends ~2.5 times as much data to the GPU (a whole 4×4-matrix) than sending two 3-vectors.
OTOH Java adds the penality of going through a FloatBuffer.
I'm currently writing a piece of code that handles the User Interface of my app.
The code has the objective of setting the location of text in orientation to two other points (centering the text). For example:
PointX 1 = 50;
PointX 2 = 150;
This input would yield the X value of the text to be 100.
My question now is that each time when the UI is refreshed this simple calculation is performed. Is this permissible or should I create a variable that stores the x value?
NB: The reason why I'm not going with the variable method, is that this would up the amount of variables in my code, which I'm not a big fan of, since I'm trying to keep my code as clean, simply and maintainable as possible.
If you are trying to keep it simple and keep memory down I would keep it the way you have it now. I'd assume that you are finding the average between the two values and this is not a very expensive operation.
I have an image that I want to transform to the frequency domain using FFT, there seems to be a lack of libraries for this for Java but I have found two. One is JTransforms and the other was less well known and doesn't have a name.
With the less well known one the 2D could only have length vales of powers of two but had simple to use methods like FastFourierTransform.fastFT(real, imaginary, true); with the real being the 2D array of doubles full of every pixel values and the imaginary part being a 2D array the same size full of zeroes. The Boolean value would depend on a forward or reverse transform. This made sense to me and it worked except for the power of two requirement which ruined any transform I did (I initially added black space around the image to fit it to the closest power of two), what I am struggling with is working out how to use the equivalent methods for JTransforms and would appreciate any guidance in doing so. I will state what I am currently doing.
I believe the relevant class would be DoubleFFT_2D, its constructor takes a number of rows and columns which I would assume to be the width and height of my image. Because my image has no imaginary parts I think I can use doubleFFT.realForwardFull(real); which treats imaginary parts as zero and pass the real 2D array full of pixels. Unfortunately this doesn't work at all. The JavaDoc states the input array must be of size rows*2*columns, with only the first rows*columns elements filled with real data But I don't see how this related to my image and what I would have to do to meet this requirement.
Sorry about the lengthy and poor explanation, if any additional information is needed I would be happy to provide it.
JTransforms Library and Docs can be found here: https://sites.google.com/site/piotrwendykier/software/jtransforms
It's too bad the documentation for JTransforms isn't available online other than a zipped download. It's very complete and helpful, you should check it out!
To answer your question: DoubleFFT_2D.realForwardFull(double[][] a) takes an array of real numbers (your pixels). However, the result of the FFT will have two output values for each input value - a the real and the imaginary part of each frequency bin. This is why your input array needs to be twice as big as the actual image array, with half of it empty / filled with zeroes.
Note that all the FFT functions use a not only for input, but also for output - this means any image data in there will be lost, so it might be desirable to copy to a different / larger array anyway!
The easy and obvious fix for your scenario would be to use DoubleFFT_2D.realForward(double[][] a) instead. This one will only calculate the positive spectrum, because the negative side will be symmetrical to it. This is because your input values are real.
Also, check out the RealFFTUtils_2D class in JTransforms, which will make it a lot easier for you to retrieve your results from the array afterwards :)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Interpolation over an array (or two)
I have a set of CSV files that contain points of a 2D function... in other words I have four CSV files, each is the result of evaluating a function f(x, y) at different y values. I need to interpolate between these data such that I can calculate an arbitrary f for a certain x and y. The CSV files have varying lengths and x-values. Does anyone know of a library or algorithm in Java for this task? Linear interpolation is OK, as is spline interpolation.
Thanks,
taktoa
Ok, first of all I assume the "CSV" bit is irrelevant, let's assume you have read those into memory and merged them together (they're the values of the same function, right?). Now you have a single set of f(x,y) values for different (x,y) pairs and would like to interpolate between those. Fine so far?
If you stick to linear interpolation, there's still the question of how many points to take into account, which will depend on the level of noise in the measurements. In the simplest case one would use just the three nearest points to identify the plane they lie in and use that to find the value for the point in question. This option requires neither libraries nor algorithms, apart from vector addition, subtraction, cross product and dot product.
More sophisticated solutions would generally require some sort of fitting, e.g. (weighted) least squares.
The simplest function is to find the closest points and use linear interpolation. e.g. chose two of three closest points and interpolate them.
Or you can take a weighted average based on distance. Or you can pick a close point and then find points on the "other side" of the closest point to improve the interpolation.
Lagrange interpolation would be simple and accurate.
I don't know if the title is appropriate but this is a design question.
I am designing a Java class which has a method which does heavy calculation and I am wondering there is a clean way to avoid this calculation every time the method is called. I know that the calling code can handle this but should it always be the responsibility of the calling code?.
To elaborate - I was writing a class for thousand dimensional vectors with a method to calculate the magnitude.So every time this method will be called it will calculate the magnitude over all the dimensions.
The concept you are looking for is called Memoization
Just cache the results in some structure internal to your class. Once the method is called, it looks if it has the previously calculated result in cache and returns it. In the other case it does the calculation and stores the result in cache. Be careful with the memory though.
Use flag to indicate whether there is a change to your vectors or not. If there is a change, then the method should do a full calculation or apply the calculation to only the changes but you will need to becareful with all the implementations of the rest of your class and make sure that the flag is properly set every time the value is modified.
The second method is to use cache. This is done by storing the previously calculated result and look it up before doing the calculation. However, this method is only work well if you don't have many variety in the key values of your objects oterwise you will end up using a lot of memory. Especially, if your key value has type of double, it is possible that the key value will never be found if they aren't exactly equal.
If the "thousand dimensional vectors" are passed in c'tor you can calculate the magnitude in c'tor and store in some private member variable.
Few things to take care of are:
If there are methods to add / delete vectors or contents of vectors then you need to update the magnitude in those methods.
If your class is supposed to be thread-safe then ensure appropriate write functions are atomic.
How often are the magnitudes changed? Is this immutable? How much of the interface for the vector do you control? Specifically, do you have any way to identify rotations or other magnitude-preserving transformations in your 1000 dimensional space? You could just store state for the magnitude, flag when the value changes, and recalculate only when necessary. If your transformations have nice internals, you might be able to skip the calculation based on that knowledge.