I am using a R/C wrapper to call a set of subroutines in a Fortran module. I want to get the outputs as arrays of 2D and 3D in R. However, I am having troubles defining the 3D output in the wrapper. So far I did not manage to define the output for a 3D array, since allocMatrix takes only three arguments (for 2D arrays/matrices):
SEXP output = PROTECT( allocMatrix(REALSXP, nt, 5) );
// 2nd argument to allocMatrix is number of rows, 3rd is number of columns
Does anyone know if it is possible or how to define the output as a 3D array in the wrapper?
I'm working on a Mac running Fortran 90, GNU Fortran, and the C compiler that comes with Xcode.
Any help/suggestion would be much appreciated.
Related
I have train my LSTM network and deploy it on the Arduino but my problem is that as you know the input of the LSTM network is as follows (window length of the time series data),(features) in my case 256 window length and 6 axis IMU data which means 2D input however all of the examples that I have seen are only 1D input
here is the Arduino code that used to assign the model's input
input->data.f[0] =aX
and when trying to modify the code to fit my input dimension
(256,6) like this
input->data.f[0][0] =aX
I got this error
fall_detection:142:44: error: invalid types 'float[int]' for array subscript
tflInputTensor->data.f[samplesRead][5] = (gZ + 2000.0) / 4000.0;
It looks like you are setting the tensor input data "manually" by digging in to the internal structure of the TF Lite tensor type. I would suggest using the API here:
https://www.tensorflow.org/lite/guide/inference#load_and_run_a_model_in_c
In particular the code snippet here:
float* input = interpreter->typed_input_tensor<float>(0);
// Fill `input`.
gives you the proper pointer. Then you can just cast the pointer for your actual data structure. Something like this:
input = reinterpret_cast<float *>(aX);
I see the potential for confusion for the 2D vs. 1D issue, but it is OK as long as the input tensor is properly shaped. If it has shape <256, 6> what this means is that the 1D sequence of float values will be "a0,a1,a2,a3,a4,a5,b0,b1,..." where the "a" values are all six values of the first row, the "b" values are all six values of the next row and so on. The standard C convention for multi-dimension memory layout works this way. Unless there is something unusual about your aX object, it should be fine. If aX is not laid out in this fashion, you should copy the data so that it is in this layout.
With FFTW and MPI, given a two-dimensional array that is the transform of a real function, represented in complex space, is it possible to output the real-space array transposed?
For example, suppose there is a 2x4 array in real space. If the code calls fftw_mpi_plan_dft_r2c_2d, then it will output a 2x3 complex array. If the flag FFTW_MPI_TRANSPOSED_OUT is added, then the output is a 3x2 complex array, the transpose of the former array. I can easily produce this behavior.
My question: is it possible to go the other way? Starting with a 2x3 complex array that is the complex-space transform of a 2x4 real-space array, is it possible to use fftw_mpi_plan_dft_c2r_2d with suitable arguments to produce the transposed, 4x2 real-space array?
Note, this is in 2D. In 3D, everything works fine, indicating that in 2D it may have to do with the last dimension representing only half of the complex plane conflicting with FTTW's expectation of the layout of the complex transpose.
I'm using Julia's FFT implementation to perform a 2D real FFT on a couple of arrays but I can't be sure of the order of the frequencies in the output. Consider the MWE
N=64
U = rand(Float64, N, N);
FFTW.set_num_threads(2)
prfor = plan_rfft(U, (1,2), flags=FFTW.MEASURE);
size(prfor*U)
The output is an array of size (33, 64).
Julia doesn't have a rfftfreq function like Numpy does, and the fact that Julia's output is different from Numpy's fft.rfftn default output makes me not want to use Numpy's default here. I read the documentation but it's not clear how the frequencies are organized just by reading that.
Is there anywhere that tells us the order of the frequencies?
I'm not sure what you are seeking exactly, but if you use DSP.jl, its util.jl file probably has what you may need:
https://github.com/JuliaDSP/DSP.jl/blob/master/src/util.jl
"""
rfftfreq(n, fs=1)
Return discrete fourier transform sample frequencies for use with
`rfft`. The returned Frequencies object is an AbstractVector
containing the frequency bin centers at every sample point. `fs`
is the sample rate of the input signal.
"""
I'm trying to pass a three-dimensional data structure to Stan (in RStan) where the entries must be integers, because a function down-stream requires that. However I'm having trouble declaring it.
I tried the straight-forward approach:
int x[n,n,k];
But that gave me the error
mismatch in number dimensions declared and found in context; ... dims declared=(n,n,k); dims found=(n*n*k)
meaning, clearly, the input array is getting flattened, for some reason (that I don't understand). I'm giving it a simple 3d array, no NAs, the dimensions look right before I pass it. And in fact, the same things is happening for 2d arrays, as well, meaning I can't even declare a set of 2d matrices, as a workaround.
Then I tried
row_vector[K] x[N,N];
but that gives back real, not int. And when I do something like
int row_vector[K] x[N,N];
that's just not proper syntax.
I also tried passing logical values, hoping they'd be re-cast as ints, but no. I passed arrays, I passed them cast with as.matrix, I checked their dimension both before and after being put into the data list.
This is with R version 3.4.1 on OSX 10.11.6, using the most recent version of stan, that was just compiled from source, today.
What am I missing? OR, how might I cast a single real to an integer, so that the integer-requiring function doesn't break?
(And, WHERE is the documentation? The best I can find is long-dead comment threads.)
I'm trying to write a subroutine for matrix sparse matrix multiplication in R using Sparsekit. It is a simple Fortran subroutine that calls two subroutines in the Sparsekit
subroutine mprod(nrowa,ncola,ncolb,job,ra,fa,ca,rb,fb,cb,rc,fc, cc, nnzc, iwi,err)
call amubdg(nrowa,ncola,ncolb,fa,ca,fb,cb,ndegr,nnzc,iwi)
call amub (nrowa,ncolb,job,ra,fa,ca,rb,fb,cb, rc,fc, cc, nnzc,iwi,err)
return
end
The subroutine amubdg gets the number of nonzero elements in each row of the product, i.e., it returns nnzc that I need to specify in amub to compute the product. Here comes my question, in R after compiling with no problem the function in the package I'm working on.
z <- .Fortran("mprod",
nrowa=as.integer(nrowa),
ncola=as.integer(ncola),
ncolb=as.integer(ncolb),
job= as.integer(1),
ra=as.double(A.csr#ra),
fa=as.integer(A.csr#ja),
ca=as.integer(A.csr#ia),
rb=as.double(B.csr#ra),
fb=as.integer(B.csr#ja),
cb=as.integer(B.csr#ia),
rc=double(nnzc),
fb=integer(nnzc),
cb=integer(A.csr#ia+1),
ndegr=integer(nrowa),
iwi=integer(ncola),
err=integer(1),
PACKAGE = "naus")
The question is the following then, is there a way to call the subroutine from R without having to specify the size of the arrays that I want out? This is, a priori I don't know what nnzc is going to be, this is calculated by amubdg, but to call it from R I need it to specify it. Any guidance will be greatly appreciated.