Java library for Fourier transform like in Mathcad - java

In mathcad there are two functions: cfft and icfft.
I need the same in my java code. It should work for 1d and 2d arrays.
Anyone know any java libraries for it? I tried to use Appache Common Math FastFourierTransformer class, but the length of the data set to be a power of 2.

Check whether JTransforms suits your needs. It is quite known, very fast implementation.

Related

Code will not compile, as it's "too large", even though the entire class is just data

I'm using NetBeans 8.1, trying to create a 2D array of impulse filters, each of which is an array of 128 doubles. I've generated text like this:
(Note that I've replaced most of the double[] values with ... to make it easier to read).
There are about 380 of these:
double[] left_H80e000a={0.1250000,-0.0390930,0.1328125...,0.7511292};
double[] left_H80e030a={-0.0390930,0.1406250,-0.0390930,...0.7511292};
double[][][] left_filters={
{left_H_40e000a,null,left_H_40e006a,left_H_40e013a,left_H_40e019a,null,left_H_40e026a,left_H_40e032a,left_H_40e039a,left_H_40e045a,null,left_H_40e051a,left_H_40e058a,left_H_40e064a,null,left_H_40e071a,left_H_40e077a,left_H_40e084a,left_H_40e090a,null,left_H_40e096a,left_H_40e103a,left_H_40e109a,null,left_H_40e116a,left_H_40e122a,left_H_40e129a,left_H_40e135a,null,left_H_40e141a,left_H_40e148a,left_H_40e154a,null,left_H_40e161a,left_H_40e167a,left_H_40e174a,left_H_40e180a}
,{left_H_30e000a,null,left_H_30e006a,left_H_30e012a,left_H_30e018a,left_H_30e024a,left_H_30e030a,null,left_H_30e036a,left_H_30e042a,left_H_30e048a,left_H_30e054a,left_H_30e060a,null,left_H_30e066a,left_H_30e072a,left_H_30e078a,left_H_30e084a,left_H_30e090a,null,left_H_30e096a,left_H_30e102a,left_H_30e108a,left_H_30e114a,left_H_30e120a,null,left_H_30e126a,left_H_30e132a,left_H_30e138a,left_H_30e144a,left_H_30e150a,null,left_H_30e156a,left_H_30e162a,left_H_30e168a,left_H_30e174a,left_H_30e180a}
,{left_H_20e000a,left_H_20e005a,left_H_20e010a,left_H_20e015a,left_H_20e020a,left_H_20e025a,left_H_20e030a,left_H_20e035a,left_H_20e040a,left_H_20e045a,left_H_20e050a,left_H_20e055a,left_H_20e060a,left_H_20e065a,left_H_20e070a,left_H_20e075a,left_H_20e080a,left_H_20e085a,left_H_20e090a,left_H_20e095a,left_H_20e100a,left_H_20e105a,left_H_20e110a,left_H_20e115a,left_H_20e120a,left_H_20e125a,left_H_20e130a,left_H_20e135a,left_H_20e140a,left_H_20e145a,left_H_20e150a,left_H_20e155a,left_H_20e160a,left_H_20e165a,left_H_20e170a,left_H_20e175a,left_H_20e180a}
,{left_H_10e000a,left_H_10e005a,left_H_10e010a,left_H_10e015a,left_H_10e020a,left_H_10e025a,left_H_10e030a,left_H_10e035a,left_H_10e040a,left_H_10e045a,left_H_10e050a,left_H_10e055a,left_H_10e060a,left_H_10e065a,left_H_10e070a,left_H_10e075a,left_H_10e080a,left_H_10e085a,left_H_10e090a,left_H_10e095a,left_H_10e100a,left_H_10e105a,left_H_10e110a,left_H_10e115a,left_H_10e120a,left_H_10e125a,left_H_10e130a,left_H_10e135a,left_H_10e140a,left_H_10e145a,left_H_10e150a,left_H_10e155a,left_H_10e160a,left_H_10e165a,left_H_10e170a,left_H_10e175a,left_H_10e180a}
,{left_H0e000a,left_H0e005a,left_H0e010a,left_H0e015a,left_H0e020a,left_H0e025a,left_H0e030a,left_H0e035a,left_H0e040a,left_H0e045a,left_H0e050a,left_H0e055a,left_H0e060a,left_H0e065a,left_H0e070a,left_H0e075a,left_H0e080a,left_H0e085a,left_H0e090a,left_H0e095a,left_H0e100a,left_H0e105a,left_H0e110a,left_H0e115a,left_H0e120a,left_H0e125a,left_H0e130a,left_H0e135a,left_H0e140a,left_H0e145a,left_H0e150a,left_H0e155a,left_H0e160a,left_H0e165a,left_H0e170a,left_H0e175a,left_H0e180a}
,{left_H10e000a,left_H10e005a,left_H10e010a,left_H10e015a,left_H10e020a,left_H10e025a,left_H10e030a,left_H10e035a,left_H10e040a,left_H10e045a,left_H10e050a,left_H10e055a,left_H10e060a,left_H10e065a,left_H10e070a,left_H10e075a,left_H10e080a,left_H10e085a,left_H10e090a,left_H10e095a,left_H10e100a,left_H10e105a,left_H10e110a,left_H10e115a,left_H10e120a,left_H10e125a,left_H10e130a,left_H10e135a,left_H10e140a,left_H10e145a,left_H10e150a,left_H10e155a,left_H10e160a,left_H10e165a,left_H10e170a,left_H10e175a,left_H10e180a}
,{left_H20e000a,left_H20e005a,left_H20e010a,left_H20e015a,left_H20e020a,left_H20e025a,left_H20e030a,left_H20e035a,left_H20e040a,left_H20e045a,left_H20e050a,left_H20e055a,left_H20e060a,left_H20e065a,left_H20e070a,left_H20e075a,left_H20e080a,left_H20e085a,left_H20e090a,left_H20e095a,left_H20e100a,left_H20e105a,left_H20e110a,left_H20e115a,left_H20e120a,left_H20e125a,left_H20e130a,left_H20e135a,left_H20e140a,left_H20e145a,left_H20e150a,left_H20e155a,left_H20e160a,left_H20e165a,left_H20e170a,left_H20e175a,left_H20e180a}
,{left_H30e000a,null,left_H30e006a,left_H30e012a,left_H30e018a,left_H30e024a,left_H30e030a,null,left_H30e036a,left_H30e042a,left_H30e048a,left_H30e054a,left_H30e060a,null,left_H30e066a,left_H30e072a,left_H30e078a,left_H30e084a,left_H30e090a,null,left_H30e096a,left_H30e102a,left_H30e108a,left_H30e114a,left_H30e120a,null,left_H30e126a,left_H30e132a,left_H30e138a,left_H30e144a,left_H30e150a,null,left_H30e156a,left_H30e162a,left_H30e168a,left_H30e174a,left_H30e180a}
,{left_H40e000a,null,left_H40e006a,left_H40e013a,left_H40e019a,null,left_H40e026a,left_H40e032a,left_H40e039a,left_H40e045a,null,left_H40e051a,left_H40e058a,left_H40e064a,null,left_H40e071a,left_H40e077a,left_H40e084a,left_H40e090a,null,left_H40e096a,left_H40e103a,left_H40e109a,null,left_H40e116a,left_H40e122a,left_H40e129a,left_H40e135a,null,left_H40e141a,left_H40e148a,left_H40e154a,null,left_H40e161a,left_H40e167a,left_H40e174a,left_H40e180a}
,{left_H50e000a,null,left_H50e008a,null,left_H50e016a,left_H50e024a,null,left_H50e032a,left_H50e040a,null,left_H50e048a,null,left_H50e056a,left_H50e064a,null,left_H50e072a,left_H50e080a,null,left_H50e088a,null,left_H50e096a,left_H50e104a,null,left_H50e112a,left_H50e120a,null,left_H50e128a,null,left_H50e136a,left_H50e144a,null,left_H50e152a,left_H50e160a,null,left_H50e168a,null,left_H50e176a}
,{left_H60e000a,null,left_H60e010a,null,left_H60e020a,null,left_H60e030a,null,left_H60e040a,null,left_H60e050a,null,left_H60e060a,null,left_H60e070a,null,left_H60e080a,null,left_H60e090a,null,left_H60e100a,null,left_H60e110a,null,left_H60e120a,null,left_H60e130a,null,left_H60e140a,null,left_H60e150a,null,left_H60e160a,null,left_H60e170a,null,left_H60e180a}
,{left_H70e000a,null,null,left_H70e015a,null,null,left_H70e030a,null,null,left_H70e045a,null,null,left_H70e060a,null,null,left_H70e075a,null,null,left_H70e090a,null,null,left_H70e105a,null,null,left_H70e120a,null,null,left_H70e135a,null,null,left_H70e150a,null,null,left_H70e165a,null,null,left_H70e180a}
,{left_H80e000a,null,null,null,null,null,left_H80e030a,null,null,null,null,null,left_H80e060a,null,null,null,null,null,left_H80e090a,null,null,null,null,null,left_H80e120a,null,null,null,null,null,left_H80e150a,null,null,null,null,null,left_H80e180a}
,{left_H90e000a,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null}
};
I've had much larger arrays in past code, one of which is 26 arrays containing 60,000 strings, so I'm baffled as to why this isn't possible.
Firstly, there's no code, secondly, does anyone have a better way to embed large data structures in Java?
Well there is code. This is converted to bytecode, somewhere the JVM will have to load these number in memory, you can have a guess as what the code will look like here: How does Arrays work in the ByteCode of Java
The worst solution I could think of would creating one class per array (like a Left_H80e000a class and statically calling them for your 3D arrays.
But honestly, create a CSV file and read from it. Like, you make aleft_H80e000a.csv" with the same data, read from it then put it in your 3D array. (isn't double[] left_H80e000a= readFromCSV("left_H80e000a.csv"); cleaner ?..) I guess a class file with only the 3D array should be fine.
Some comments stated to use a database. That's an idea, but there is no point building a database if that's your only use.

How to adjust two arrays of points to a sine function?

I'm working with ImageJ. I have two arrays of points (i.e it[ ], cmx[ ]) and what I want is to adjust this to a sine function. I've been working with CurveFitting but I don't understand it very well. I also am having issues with UserFunction.
Is there an easier approach to this? If you have examples I would appreciate it.
The following Groovy script is an example of running curve fitting on three data points:
import ij.measure.CurveFitter;
xData = [0,1,2];
yData = [3.1, 5.1, 6.9];
cv = new CurveFitter((double[]) xData.toArray(), (double[]) yData.toArray());
cv.doFit(CurveFitter.STRAIGHT_LINE);
println (cv.getResultString());
I'm not sure if CurveFitter allows fitting to trigonometric functions, there doesn't seem to be this option in the available fitting types. You might try a high-degree polynomial fitting instead.
You can also ask on the ImageJ forum or mailing list regarding the implementation details of the CurveFitter class.

Convex optimization, java

I'm looking for a Java library to solve this problem:
We know X is sparse(most of it's entries are zero), so X can be recovered by solving this:
variable X;
minimize(norm(X,1)+norm(A*X - Y,2));
It's a MATLAB code, matrix A and vector Y are known and I want the best X.
I saw JOptimizer, but I couldn't use it. (Doesn't have good documentation or examples).
What you need is a reasonably good LP Solver.
Possible Java LP Solver Options
Apache Commons (Math) Simplex Solver.
See this blog post.
If you have access to CPLEX (not-free), its Java API would work great.
Also, you can look into SuanShu, a Java numerical and statistical library
lpSolve has a Java wrapper which can do the job.
Finally, JOptimizer is indeed a good option. Not sure if you looked at this example.
Hope at least one of those help.
As far as I can tell, you're trying to solve a binary integer program for feasibility
Ax = b, x in {0,1}.
I'm not completely sure, but it seems that you might be interested in the optimization problem
min 1'*x
s.t. Ax = b, x in {0,1}
where 1 is a vector of 1's of the same dimension as x.
The feasibility problem may be in practice much easier than the optimization problem - it all depends on a particular A and b.
If you can get a license of either CPLEX or Gurobi (if you're an academic), these are excellent integer programming solvers with good Java API's. If you don't have access to these, lpsolve may be a good option.
As far as I can tell, JOptimizer will not solve your problem since your variables are integers (although I have never used JOptimizer).
To solve convex optimization problems in java you can use the following library https://github.com/erikerlandson/gibbous

Memory-efficient sparse array in Java

(There are some questions about time-efficient sparse arrays but I am looking for memory efficiency.)
I need the equivalent of a List<T> or Map<Integer,T> which
Can grow on demand just by setting a key larger than any encountered before. (Can assume keys are nonnegative.)
Is about as memory-efficient as an ArrayList<T> in the case that most of the indices are not null, i.e. when the actual data is not very sparse.
When the indices are sparse, consumes space proportional to the number of non-null indices.
Uses less memory than HashMap<Integer,T> (as this autoboxes the keys and probably does not take advantage of the scalar key type).
Can get or set an element in amortized log(N) time where N is the number of entries: need not be linear time, binary search would be acceptable.
Implemented in a nonviral open-source pure Java library (preferably in Maven Central).
Does anyone know of such a utility class?
I would have expected Commons Collections to have one but it did not seem to.
I came across org.apache.commons.math.util.OpenIntToFieldHashMap which looks almost right except the value type is a FieldElement which seems gratuitous; I just want T extends Object. It looks like it would be easy to edit its source code to be more generic, though I would rather use a binary dependency if one is available.
I would try with trove collections, there is TIntObjectMap which can work for your intents.
I would look at Android's SparseArray implementation for inspiration. You can view the source by downloading AOSP's source code here http://source.android.com/source/downloading.html
I will suggest you to use OpenIntObjectHashMap from Colt library. Link
I have saved my test case as jglick/inthashmap. The results:
HashMap size: 1017504
TIntObjectMap size: 853216
IntHashMap size: 846984
OpenIntObjectHashMap size: 760472
Late to this question, but there is IntMap in libgdx which uses cuckoo hashing. If anything it would be interesting to compare with the others.

How to store table or matrix in Java?

I used to use matrix in octave to store data from data set, in Java how can I do that? Assume I have 10-20 columns and large data, I don't think
int [][]data;
would be the best option. Is nested map the only solution?
You could create a class Coordinate that takes an X and Y values and properly implement hashCode and equals.
Then create a HashMap<Coordinate, Data> and work with it.
Depends on what you need to do. If you know the size of the lists, then an array is definitely ideal since it means you will have instant access (read/write time) to any position in the array, this is very useful for speed.
Maps are better if you dont know the size and it needs to be able to adapt.
And finally, as I discovered in a previous question, if you have a TON of data, and a lot of it will be "0" you might want to also consider using a Sparse Martrix
This answer merges some of gnomed's answer and SJuan76's answer contents.
At a quick glance, I'd suggest you to use bidimentional arrays such as int[][].
It's not a very huge amount of data (we're speaking of ≈500 ints) so it's not a bad idea.
Advantages: It's the simpler, ideal (from the data-structuring side) way to go,
especially if every “slot” of the matrix contains data.
The inconvenient: You have to know the size of the matrix before constructing it.
Anyway, you can resize it later using the Arrays utilities.
If you want more effective handling of the data, you can use a single point-map.
That is, the key of every entry is a java.awt.Point that defines where is the value located.
Advantages: It's more effective than having a 2D array,
especially if part of your matrix doesn't contain data.
And it's adaptative; you don't need to know any sizes to construct/resize it.
The inconvenient: If every “slot” of your matrix contains data,
you'll loose (a lot of) space and performance. A 2D-array is more effective then.
Want more? If your data is really huge you can use a sparse matrix.
See this question for more details.
I would not discard multidimensional arrays so far: have you tried them? Are you finding specific limitations? IMHO as long as your data fits in memory, arrays can be good.
If your data is very sparse though, you may want to look at maps indeed.
Related question btw: Making a very large Java array
You can use multidimensional arrays or you can try any pairs like HashMap
I think multi-dimentional arrays are the best choice! They should serve your purpose. If your data set is only integers, int [] [] is an ideal choice.
Well, if your indices are small integers, you can certainly use nested arrays.
In a matrix class, you may want to use a plain array, like so: (assuming n is the number of columns)
double get(int i, int j) { return data[i*n + j]; }
For a general table (sparse matrix), you can use nested maps, but consider using com.google.common.collect.Table implementations from the Google Guava library.

Categories