I am trying to convert an .m file to an .oct file in Octave and part of the .m file code is:-
for hh = 1 : nt
bi_star = bi_star + A( : , : , hh ) * data( : , : , hh + 1 )' ;
end
where both "A" and "data" are of NDArray type. I have tried to extract the values from the NDArrays by using something like
A.extract( 0 , 0 , num_dims-1 , num_dims-1 , hh ) ;
but get the error message
error: ‘class NDArray’ has no member named ‘extract’
when compiling. The only other way I can think of doing this at the moment is to put nested loops within the hh loop to loop over both "A" and "data" to fill in intermediate calculation matrices and do the matrix multiplications and additions using these intermediate matrices. However, this seems to be a very long winded way of doing things. Is there a more efficient way of accomplishing this?
Thanks to Andy's answer here and its included link to the Octave sources page, I have been able to figure out that I simply need to use:
A.page ( hh )
to accomplish what I what.
Thanks Andy!
Related
I am new to Fortran but I am trying to adapt a Fortran code and I am having trouble doing something which I think is probably quite simple.
I want to adapt a Fortran file called original.f so that it makes an input file called input.inp and populates it with 4 integers calculated earlier in original.f so that input.inp looks like, for example:
&input
A = 1
B = 2
C = 3
D = 4
&end
I know how to write this format:
OPEN(UNIT=10,FILE='input.inp')
WRITE (10,00001) 1,2,3,4
...
...
...
00001 Format (/2x,'&input',
& /2x,'A = ',i4,
& /2x,'B = ',i4,
& /2x,'C = ',i4,
& /2x,'D = ',i4,
& /2x,'&end')
(or something like this that I can fiddle with when I get it working) but I am not sure how to create the input.inp file write this into it and then use this input file.
The input file needs to be used to run an executable called "exec". I would run this in bash as:
./exec < input.inp > output.out
Where output.out contains two arrays called eg(11) and ai(11,6,2) (with dimensions given) like:
eg(1)= 1
eg(2)= 2
...
...
...
eg(11)= 11
ai(1,1,1)= 111
ai(1,2,1)= 121
...
...
...
ai(11,6,2)=1162
Finally I need to read these inputs back into original.f so that they can be used further down in file. I have defined these arrays at the beginning of original.f as:
COMMON /DATA / eg(11),ai(11,6,2)
But I am not sure of the Fortran to read data line by linw from output.out to populate these arrays.
Any help for any of the stages in this process would be hugely appreciated.
Thank you very much
James
Since you have shown how you create the input file, I assume the question is how to read it. The code shows how "a" and "b" can be read from successive lines after skipping the first line. On Windows, if the resulting executable is a.exe, the commands a.exe < data.txt or type data.txt | a.exe will read from data.txt.
program xread
implicit none
character (len=10) :: words(3)
integer, parameter :: iu = 5 ! assuming unit 5 is standard input
integer :: a,b
read (iu,*) ! skip line with &input
read (iu,*) words ! read "a", "=", and "1" into 3 strings
read (words(3),*) a ! read integer from 3rd string
read (iu,*) words ! read "b", "=", and "1" into 3 strings
read (words(3),*) b ! read integer from 3rd string
print*,"a =",a," b =",b
end program xread
If I understand the expanded question correctly, you have to work with an output file, produced by some other code you did not write, with lines like eg(1) = ....
For the simplest case where you know the number of elements and their ordering beforehand, you can simply search each line for the equals sign from behind:
program readme
implicit none
character(100) :: buffer
integer :: i, j, k, pos, eg(11), ai(11,6,2)
do i = 1,11
read*, buffer
pos = index(buffer, '=', back = .true.)
read(buffer(pos+1:), *) eg(i)
enddo
! I have assumed an arbitrary ordering here
do k = 1,2
do i = 1,11
do j = 1,6
read*, buffer
pos = index(buffer, '=', back = .true.)
read(buffer(pos+1:), *) ai(i,j,k)
enddo
enddo
enddo
end program
Assuming here for simplicity that the data are provided to standard input.
I am having trouble using dplyr's tbl_df, respectively the regular data.frame. I got a big tbl_df (500x30K) and need to filter it.
So what I would like to do is:
filter(my.tbl_df, row1>0, row10<0)
which would be similar to
df[df$row1>0 & df$row10<0,]
Works great. But I need to build the filter functions dynamically while running, so I need to access the DF/tbl_df columns by one or multiple variables.
I tried something like:
var=c("row1","row10")
op=c(">","<")
val=c(0,0)
filter(my.tbl_df, eval(parse(text=paste(var,op,val,sep="")))
Which gives me an error: not compatible with LGLSXP
This seems to be deeply rooted in the Cpp code.
I would be thankful for any hint. Also pointing out the "string to environment variable" conversion would be helpful, since I am pretty that I am doing it wrong.
With best,
Mario
This is related to this issue. In the meantime, one way could be to construct the whole expression, i.e.:
> my.tbl_df <- data.frame( row1 = -5:5, row10 = 5:-5)
> call <- parse( text = sprintf( "filter(my.tbl_df, %s)", paste(var,op,val, collapse="&") ) )
> call
expression(filter(my.tbl_df, row1 > 0&row10 < 0))
> eval( call )
row1 row10
1 1 -1
2 2 -2
3 3 -3
4 4 -4
5 5 -5
I am using .svg files for thumbnails. The original .svg's came from CorelDraw, and they are about 400x400 each. But I wish to use them inside 75x75 thumbnail divs.
I have tried:
one_thumb__paper.setViewBox( 0 , 0 , 75 , 75 , true ) ;
one_thumb__set.transform( "s.5" )
also, the ScaleRaphaël class
I was doing two things wrong:
1 -
// the wrong way:
var my_set = my_paper.importSVG( my_svg_string); 5
// the correct way:
var my_set = my_paper.set();
my_paper.importSVG( my_svg_string , my_set );
2 -
// the wrong way:
my_set.transform( "s.5" )
// the correct way:
my_set.transform( "s.5,.5,0,0" )
see working fiddle
see raphaeljs svg importer
I am using some old fortran code for a biology project I am doing. I am posting the relevant snippets here. Here is a subroutine called "READCN". Earllier in the program MAXN was set to 108.
OPEN ( UNIT = CNUNIT, FILE = CNFILE,
: STATUS = 'OLD', FORM = 'UNFORMATTED' )
READ ( CNUNIT ) N, BOX
IF ( N .GT. MAXN ) STOP ' N TOO LARGE '
READ ( CNUNIT ) ( RX(I), I = 1, N ), ( RY(I), I = 1, N )
CLOSE ( UNIT = CNUNIT )
RETURN
END
I am inputting a file called "data.dat" to the program. Here is the file:
10, 4
0.8147, 0.1576
0.9058, 0.9706
0.1270, 0.9572
0.9134, 0.4854
0.6324, 0.8003
0.0975, 0.1419
0.2785, 0.4218
0.5469, 0.9157
0.9575, 0.7922
0.9649, 0.9595
Nevertheless, I always get the message "N TOO LARGE". Any advice? Thanks!
Don't open as unformatted, it will read your file as if it were binary data. Open as formatted instead, and use "*" format. Also, don't read in one line, as you would not read your data in the expected order.
program bob
implicit none
integer cnunit, n, maxn, box, i
parameter(maxn=108, cnunit=10)
real rx(maxn), ry(maxn)
open(unit=cnunit, file='bob.txt', status='old', form='formatted')
read(cnunit, *) n, box
print *, 'n=', n, 'box=', box
if(n .gt. maxn) stop 'n too large'
do i=1, n
read(cnunit, *) rx(i), ry(i)
print *, rx(i), ry(i)
end do
close(unit=cnunit)
end
Alternately, if you can't change the code, then change your input file to fit the needs of your program. The input file you give simply won't work: you need binary data, in the format expected by your compiler (there is the usual, non portable "record size"), and data must be given column-wise, not row-wise.
I have a problem with reading binary file in C++. Currently my code is like this:
FILE *s=fopen(source, "rb");
fseek(s,0,SEEK_END);
size_file size=ftell(s);
rewind(s);
char *sbuffer=(char *) malloc(sizeof(char) * size);
if(sbuffer==NULL){
fputs("Memory error", stderr);
exit(2);
}
size_t result=fread(sbuffer,1,size,s);
if(result != size){
fputs("Reading error",stderr);
exit(3);
}
fclose(s);
cout<<sbuffer<<endl;
However, the characters printed out on the terminal are all random characters instead of what I write in the PDF file. They are like:
% P D F - 1 . 3
% ? ? ? ? ? ? ? ? ? ? ?
4 0 o b j
< < / L e n g t h 5 0 R / F i l t e r / F l a t e D e c o d e > >
s t r e a m
x ? ? ? j ? 0 E ? ? ? k ? y Q E # ? ? ? m ? & ? ? # % + ? . ? ? ? ? A i ? 4 z \ 1 G W ? ? - , ? ? ? ( ? ? ? 9 ? ? ? ? ? \ ? } ? ? ? e ? ? ? ? 0 ? ? ? ~ ? , ? ? & 8 ? ? x e 4 ? r
| ? ? ?
? ? ? ? E > a ? ? z & ? Z ? < ? } ' ? ? ? j p ? ? Q 7 0 ? ? ? S % - p ? ? ? 7 D ? ? ? ' Q z Q ? ? ? ? ? ? ? ? ? ? \ 2 ? ? 7 ? ? ? < ? ? D ~ ? ? ?
e n d s t r e a m
e n d o b j
5 0 o b j
2 2 8
e n d o b j
2 0 o b j
And many others characters like the above. I tried to search for a long time but cannot find out how to get the actual characters out for later processing. By the way, I'm trying to write a compressor which takes binary file as input and output. Any help here is highly appreciated!
Only a few file formats like plain raw .TXT text files can be "read" and "understood" directly. Most of the file formats, including almost any binary format, is a .. format. This implies certain structure held inside the file. Completely contrary to the .TXT text file that is completely structure-less, or rather, it is one huge block of pure data.
Open a WordPad or Word or any other a least somewhat intelligent text editor and write some text there and then save it as RTF, DOC, ODT or any other non-TXT file. Then save it as TXT file too.
Download a HEX VIEWER/HEX EDITOR. Whatever one. Take one of those free, you don't need many features, just the one that displays raw binary values in one column and ASCII text in the other column. Almost any of free hex viewers/editors can do that.
Open and compare those two files. You will immediatelly see difference.
Back to the PDF:
The PDF even can contain graphics interleaved with the text. How'd you expect to keep it, if the text were "just sitting in the file" like in TXT? How would the image position/description/data be embedded? The PDF can even contain scripts, if I remember well, similar to JavaScripts. Executable. In PDF-type document you can have buttons that do something. That's much more complicated than just text-in a-file.
Binary files usually does not contain any plain-readable text for your eyes. They have that text structured in blocks, wrapped in metadata about colors, text layout, paging and such, or even special structures about document versioning, authoring, classification, (...). This everything has to be stored somewhere.
Usually, binary files have sections. First section usually is called the HEADER. Inside, there will be information about: format type, format version, file/block/data length, image resolution, and similar. All those most probably will be kept in binary form: no "800x600" texts, just "|00|00|03|20|00|00|02|58|" assuming 32bit BE. After your have read, decoded and understood the description, then you will know where the actual data starts, how the data blocks are laid out, and how to decode them and understand what they contain.
edit:
After you understand what is the difference between text files and binary files, check out the absolute basics on http://en.wikipedia.org/wiki/Entropy_(information_theory). Then try playing with RLE (http://www.daniweb.com/software-development/cpp/code/216388/basic-rle-file-compression-routine) or Huffman (http://www.cprogramming.com/tutorial/computersciencetheory/huffman.html) just to start on something relatively simple. Then start reading more about Huffman codes, and then, well, you will be reasonably prepared to the task, like ZIP or LZH..
To parse PDF as text, use some PDF library, such as gnupdf or
poppler.