Formatting in the write statement in Fortran - fortran

I try to use write statement in Fortran. But the required format is not generated.
When I change the format
write(unit=28, fmt='(1X,E15.7,E15.7,E15.7)')
to
write(unit=28, fmt='(1X,E15.7,E15.7,I7)')
it will give me there value as *******
0.2375528E-01 0.3807880E-02 *******
0.1294881E-01 0.7272966E-01 *******
0.9220393E-02 -0.7748791E-01 *******
0.3838744E-02 -0.1024217E+00 *******
0.4709786E-02 -0.2939432E-01 *******
Main Code:
post = post+1
write(filename,'(a,i5.5,a)') 'particles', post, '.dat'
open ( unit=28, file=filename , recl=300 )
write(unit=28, fmt='(1X,E15.7,E15.7,E15.7)') (xp(i,:), i=1,npart)
close ( unit=28 )
Result form the code
0.2375528E-01 0.3807880E-02 0.1E+01
0.1294881E-01 0.7272966E-01 0.2E+01
0.9220393E-02 -0.7748791E-01 0.3E+01
0.3838744E-02 -0.1024217E+00 0.4E+01
0.4709786E-02 -0.2939432E-01 0.5E+01
Required result needed in this format
0.2375528E-01 0.3807880E-02 1
0.1294881E-01 0.7272966E-01 2
0.9220393E-02 -0.7748791E-01 3
0.3838744E-02 -0.1024217E+00 4
0.4709786E-02 -0.2939432E-01 5
Can anybody suggest me how I can get the required result?

Convert the numbers to integers
write(unit=28, fmt='(1X,E15.7,E15.7,I7)') (xp(i,1:2),nint(xp(i,3)), i=1,npart)

Request zero decimals
write(unit=28, fmt='(1X,E15.7,E15.7,F15.0)') (xp(i,:), i=1,npart)

Related

How to implement the Luhn alorithm in OCL

I am trying to find the Luhn algorithm in OCL to check the validity of ISIN. Can anyone provide a code example would it be great!
In the German Wikipedia article about the Luhns algorithm (https://de.wikipedia.org/wiki/Luhn-Algorithmus) you can find an example to calculate the value for an ident of 446-667-651. The algorithm below calculates the correct value of 40.
let list = '446667651'.ToCharArray.strToInt in
Sequence{1..list->size}
->Collect(i|
if (list->size-i).Mod(2)=0 then
list.at(i)
else
(list.at(i)*2).Mod(9)
endif)
->Sum
Maybe you need some adaptions for calculating the value for ISINs.
In pure OCL using the example from https://en.wikipedia.org/wiki/Luhn_algorithm
let s = Sequence{7,9,9,2,7,3,9,8,7,1} in
(Sequence{1..s->size()}
->collect(i |
let t = s->at(i) in
if i.mod(2) = 1
then t
else let tt = 2 * t in tt.div(10) + tt.mod(10)
endif)
->sum()*9)
.mod(10)
Create a class:
Attribute NextMult1Or2:Integer
Method MultWith2Or1SumBiggerThan10(v:Integer):Integer
Method GetCheckSum(input:String):Integer
The first method MultWith2Or1SumBiggerThan10:
let res=v*self.NextMult1Or2 in
(
if self.NextMult1Or2=2 then
self.NextMult1Or2:=1
else
self.NextMult1Or2:=2
endif;
if res>10 then
res-9
else
res
endif
)
And the second method GetCheckSum(input:String):Integer
self.NextMult1Or2:=2;
input.ToCharArray->collect(c|
let i=Integer.Parse(c) in (
self.MultWith2Or1SumBiggerThan10(i)
)
)->sum
To calculate the checksum - send in all but check digit to GetCheckSum - the check digit is (((res+10) / 10).Truncate*10)-res (ie the diff from nearest 10:th above)
To check a sequence send in all including check digit to GetCheckSum - if res.Mod(10)= 0 it has passed

Reading in TSP file Python

I need to figure out how to read in this data of the filename 'berlin52.tsp'
This is the format I'm using
NAME: berlin52
TYPE: TSP
COMMENT: 52 locations in Berlin (Groetschel)
DIMENSION : 52
EDGE_WEIGHT_TYPE : EUC_2D
NODE_COORD_SECTION
1 565.0 575.0
2 25.0 185.0
3 345.0 750.0
4 945.0 685.0
5 845.0 655.0
6 880.0 660.0
7 25.0 230.0
8 525.0 1000.0
9 580.0 1175.0
10 650.0 1130.0
And this is my current code
# Open input file
infile = open('berlin52.tsp', 'r')
# Read instance header
Name = infile.readline().strip().split()[1] # NAME
FileType = infile.readline().strip().split()[1] # TYPE
Comment = infile.readline().strip().split()[1] # COMMENT
Dimension = infile.readline().strip().split()[1] # DIMENSION
EdgeWeightType = infile.readline().strip().split()[1] # EDGE_WEIGHT_TYPE
infile.readline()
# Read node list
nodelist = []
N = int(intDimension)
for i in range(0, int(intDimension)):
x,y = infile.readline().strip().split()[1:]
nodelist.append([int(x), int(y)])
# Close input file
infile.close()
The code should read in the file, output out a list of tours with the values "1, 2, 3..." and more while the x and y values are stored to be calculated for distances. It can collect the headers, at least. The problem arises when creating a list of nodes.
This is the error I get though
ValueError: invalid literal for int() with base 10: '565.0'
What am I doing wrong here?
This is a file in TSPLIB format. To load it in python, take a look at the python package tsplib95, available through PyPi or on Github
Documentation is available on https://tsplib95.readthedocs.io/
You can convert the TSPLIB file to a networkx graph and retrieve the necessary information from there.
You are feeding the string "565.0" into nodelist.append([int(x), int(y)]).
It is telling you it doesn't like that because that string is not an integer. The .0 at the end makes it a float.
So if you change that to nodelist.append([float(x), float(y)]), as just one possible solution, then you'll see that your problem goes away.
Alternatively, you can try removing or separating the '.0' from your string input.
There are two problem with the code above.I have run the code and found the following problem in lines below:
Dimension = infile.readline().strip().split()[1]
This line should be like this
`Dimension = infile.readline().strip().split()[2]`
instead of 1 it will be 2 because for 1 Dimension = : and for 2 Dimension = 52.
Both are of string type.
Second problem is with line
N = int(intDimension)
It will be
N = int(Dimension)
And lastly in line
for i in range(0, int(intDimension)):
Just simply use
for i in range(0, N):
Now everything will be alright I think.
nodelist.append([int(x), int(y)])
int(x)
function int() cant convert x(string(565.0)) to int because of "."
add
x=x[:len(x)-2]
y=y[:len(y)-2]
to remove ".0"

Argument data type numeric is invalid for argument 3 of convert function

I have a varchar field that contains numbers i need to convert to decimal and remove commas before doing calculations. I am getting a numeric is invalid for argument 3 of convert.
Using these values as an example.
Value varchar(8000) = 12,545
Pct_cmpt decimal(8,2) = 23.00
SELECT
CONVERT(decimal(18,2),replace(value,',',''),ROUND(CASE WHEN ISNUMERIC(VALUE) = 1 THEN ISNULL(CONVERT(numeric, VALUE),0) *Pct_Cmpt/100 ELSE 0 END,2)) as Earned
Argument data type numeric is invalid for argument 3 of convert function. Any help would be appreciated.
So I figured it out after scratching my head for awhile. I had to move my REPLACE function inside the CASE WHEN to get the CONVERT to work.
Select
CONVERT(decimal(18,2),ROUND(CASE WHEN ISNUMERIC(VALUE) = 1 THEN ISNULL(CONVERT(numeric,replace(value, ',','')),0)*PCT_CMPT/100 ELSE 0 END,2)) as Earned

filter dplyr's tbl_df using variable names

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

My whole parsing logic suffers because of null character how to resolve this

This is DATA 1
RE00002200050046\00 0.00 0.1 0.125.9\0#####- 14 0##### \0 0##### 141.0\004.00 0: 00.000.0\00 4: 011:27 0: 015:27#\0###########2.00.0\0
Another data that i have is
This is DATA 2
RE000022601\0500460 0.00 0.1\0 0.236.8####\0# 57- 2#####- 3#####\0- 601.004.0\00 4: 00.000.\000 4: 013:37 0\0: 017:37#####\0#######2.00.\00
The above data is the response i get from an hospital machine,i have to parse the above values and fill it according to given format:-
BYTEs 2 2 4 128 2 2
+---------+--------+------------+-----------------+--------+-------+
| RE | 00 | machine no| Data part | Check | CRC |
| | | | | sum | |
+---------+--------+------------+-----------------+--------+-------+
As you can see from DATA 1 My data part begins from "000500.."
and DATA 2 My data part begins from "601\0500..."
While doing parsing i got into a problem that there is field named "Blood pump flow" whose length is 3 bytes from the "DATA 1" we get its value as "46" while from the
"DATA 2" i got its value as "460".
In actual its value should be "460"
If i get a data like DATA 1 my whole parsing logic suffers as because as "Blood pump flow" is "3 bytes" i get a value "46\0" and "0" is added to another field while "Blood pump flow" should be "460".
The above is just one case i get it many times for some other fields too.
How to resolve this problem.
DATA 1 and DATA 2 are the binary data that i get from the machine.
It seems from your example, and it is confirmed by your own comment, that you know the field sizes in your format. So you must treat this input as binary input. Use std::istream::read function.
unsigned char header[14];
is.read(header,14);
if (is.gcount() == 14)
{
// decide which DATA1 or DATA2 you read from header contents
if (header is for DATA1)
// read rest of input as DATA1
// decide which DATA1 or DATA2 you read from header contents
else if (header is for DATA2)
// read rest of input as DATA2
else
//report error
}