SQL function with square roots causes problems - sql

I have a query that is erroring out:
SET #SumDiff = #SitesSum - (select power(SUM(Sums),2)/#NumSites from #SiteSums)
SET #SE = power((#SumDiff/(#NumSites - 1)),0.5)/power(#NumSites,0.5)
This function works most of the time, but I get an error some times.
Here are the values when i have the problem.
SUM(Sums) = .01 so squared = .001
#NumSites = 2
#SiteSum = 0
The value from this part:
select power(#Sums,2)/#NumSites from #SiteSums
comes out as 5E-05 which would match the value expected, .00005
When I try to set #SE it's basically finding the square root of (.00005 / 1)/ square root of 2 (1.4142135623731).
Should be something like 3.535533905932725. But I get the message:
Msg 3623, Level 16, State 1, Line 306
An invalid floating point operation occurred.
Is .00005 too small to divide in SQL? Could I do a cast? This isn't always going to be working with decimals, mostly larger numbers.

Actually, using your values this:
SET #SumDiff = #SitesSum - (select power(SUM(Sums),2)/#NumSites from #SiteSums)
..results in #SumDiff being set to -5E-05, and not 5E-05.
So when you then do:
power((#SumDiff/(#NumSites - 1)),0.5)
..you are trying to take the square root of a negative number which is invalid (undefined) for a Real number.
The ABS(..) function can fix this for you:
SET #SE = power((ABS(#SumDiff)/(#NumSites - 1)),0.5)/power(#NumSites,0.5)

Related

Error while transforming series with series.map() function

def locate (code):
string1 = str(code)
floor = string1[3]
if floor == '1':
return 'Ground Floor'
else:
if int(string1[5]) < 1:
lobby = 'G'
elif int(string1[5]) < 2:
lobby = 'F'
else:
lobby = 'E'
return floor + lobby
print(locate('S191009'))
print(locate('S087525'))
This function works fine with individual input code as above with Output
Ground Floor
7E
But when I use this to map a series in data frame, it shows error.
error_data1['location'] = error_data1['status'].map(locate)
Error message: string index out of range.
How can I fix this??
Your problem is with your series values:
se = pd.Series(['S191009', 'rt'])
se.map(locate)
produces the same error you reported. You can ignore these rows using try...except in function if it does not hurt you.
The problem is you are indexing an index on a string that doesn't exist (i.e the string is shorter than what you expect). As the other answer mentioned, if you try and use
my_string="foo"
print(my_string[5])
You will get the same error. To solve this you should add a try except statement, or for simplicity an initial if statement that returns "NotValid" or something like that. Your data probably has strings that do not follow the standard form you expect.

A type error: 'builtin_function_or_method' object does not support item assignment. How can I fix that?

I'm running the following code and got a type error:
TypeError: 'builtin_function_or_method' object does not support item assignment
Here's my code:
N_object4 = 4
alpha = np.random.normal(0, 10, N_object4)# Random alpha values (could be greater or less than 0.)
pa = np.abs(alpha)
num = pa.argsort()[-3:][::-1]
gs = np.zeros(N_object4).tolist
for i in range (len(num)): # Iterating from largest abs(alpha) to the smallest.
if alpha[num[i]] > 0:
gs[num[i]+1] = 1
The error happens in my last line. How can I fix this error? Thanks!!
I think its small typo in line 4. You should use tolist():
gs = np.zeros(N_object4).tolist()

Strange numbers in the output from Netcdf in Fortran

I'm fairly new to Fortran and try reading in 3D (80000*100*10) single precision NetCDF data (however I'd just read it 2D like (80000,100,1st)). I would need to convert them into double precision for some further code not shown below.
The .nc-File which I create to check if the reading/writing works does contain only "0" if I use real single precision for all NF90-functions as well as the variable 'values'.
It does contain mostly "0" and several weird numbers which don't seem to relate in a conceivable way to the input data if I use double precision (code shown below). At least I get any output in my nc file this way.
I don't get any compiling errors {nor error codes in 'STATUS' if I check for specific lines after NF90 functions. Update: That was mistaken}. Input_test.nc gets created exactly as expected regarding dimensions, but not regarding the actual values.
My code is:
PROGRAM read
Implicit None
INCLUDE 'netcdf.inc'
INTEGER :: NCID, horstart, verstart, horlen, verlen, horcount, vercount, STATUS, STATUS2
REAL(kind=8), DIMENSION(80000,100) :: values
horstart = 1
verstart = 1
horcount = 80000 !!(tried to use this instead of horlen, doesn't change anything)
vercount = 100 !!(tried to use this instead of verlen, doesn't change)
varname = 'pres'
!! get input data
STATUS = NF90_OPEN('my_valid_path/file.nc', 0, NCID)
STATUS = NF90_INQ_DIMID(NCID, 'ncells', horid)
STATUS = NF90_INQ_DIMID(NCID, 'height', verid)
STATUS = NF90_INQ_DIMLEN(NCID,horid,horlen)
STATUS = NF90_INQ_DIMLEN(NCID,verid,verlen)
STATUS = NF90_INQ_VARID(NCID,varname,varid)
STATUS = NF90_GET_VARA_DOUBLE(NCID,varid,[horstart,verstart,1],[horlen,verlen,1],values)
STATUS = NF90_CLOSE(NCID)
STATUS = NF90_CREATE ('some_path/input_test.nc', 0, ncid);
STATUS = NF90_DEF_DIM (ncid, 'hor',horcount, dimhor)
STATUS = NF90_DEF_DIM (ncid, 'ver',vercount, dimver)
STATUS = NF90_DEF_DIM (ncid, '1d',1, dimcode)
STATUS = NF90_DEF_VAR(ncid,'pres',NF90_DOUBLE,2,[dimhor,dimver],pres_id)
STATUS = NF90_DEF_VAR(ncid,'status',NF90_INT,1,dimcode,stat_id)
STATUS = NF90_ENDDEF(ncid)
STATUS = NF90_PUT_VARA_DOUBLE(ncid,pres_id,[horstart,verstart],[horcount,vercount],values)
STATUS = NF90_PUT_VARA_INT(ncid,stat_id,1,1,STATUS2)
STATUS = NF90_CLOSE(ncid)
The nc file I read from doesn't contain any zeroes, not even in the 3rd dimension. The Output file however does contain a lot zeroes, it is not empty though.
Example input: 0,095213220 0,099325478 0,10358732 0,10800611 0,11259078 0,11734842 0,12228279 0,12740466 0,13271827 0,13822863 0,14394356
Example output: 0 0 0 0 0 0,000493943283800036 0,000594558776356280 0,000234268474741839 2,88491937681101e-05 2,09666131608306e-16 7,30948746534081e-20
I'm probably doing something stupid, but I went temporarily out of ideas what to check for.
UPDATE: Thoroughly checking the error codes saved in STATUS did give a non-zero match at NF90_GET_VARA_DOUBLE/(also REAL). Getting back to this tomorrow.
With error handler I came to the conclusion that the above code works. The errors eventually came from a spelling mistake of the variables I tried to read from.

Octave keyboard input function to filter concatenated string and integer?

if we write 12wkd3, how to choose/filter 123 as integer in octave?
example in octave:
A = input("A?\n")
A?
12wkd3
A = 123
while 12wkd3 is user keyboard input and A = 123 is the expected answer.
assuming that the general form you're looking for is taking an arbitrary string from the user input, removing anything non-numeric, and storing the result it as an integer:
A = input("A? /n",'s');
A = int32(str2num(A(isdigit(A))));
example output:
A?
324bhtk.p89u34
A = 3248934
to clarify what's written above:
in the input statement, the 's' argument causes the answer to get stored as a string, otherwise it's evaluated by Octave first. most inputs would produce errors, others may be interpreted as functions or variables.
isdigit(A) produces a logical array of values for A with a 1 for any character that is a 0-9 number, and 0 otherwise.
isdigit('a1 3 b.') = [0 1 0 1 0 0 0]
A(isdigit(A)) will produce a substring from A using only those values corresponding to a 1 in the logical array above.
A(isdigit(A)) = 13
that still returns a string, so you need to convert it into a number using str2num(). that, however, outputs a double precision number. so finally to get it to be an integer you can use int32()

Using CONTAINS with variables sql

Ok so I am trying to reference one variable with another in SQL.
X= a,b,c,d (x is a string variable with a list of things in it)
Y= b ( Y is a string variable that may or may not have a vaue that appears in X)
I tried this:
Case when Y in (X) then 1 else 0 end as aa
But it doesnt work since it looks for exact matches between X and Y
also tried this:
where contains(X,#Y)
but i cant create Y globally since it is a variable that changes in each row of the table.( x also changes)
A solution in SAS would also be useful.
Thanks
Maybe like will help
select
*
from
t
where
X like ('%'+Y+'%')
or
select
case when (X like ('%'+Y+'%')) then 1 else 0 end
from
t
SQLFiddle example
In SAS I would use the INDEX function, either in a data step or proc sql. This returns the position within the string in which it finds the character(s), or zero if there is no match. Therefore a test if the value returned is greater than zero will result in a binary 1:0 output. You need to use the compress function with the variable containing the search characters as SAS pads the value with blanks.
Data step solution :
aa=index(x,compress(y))>0;
Proc Sql solution :
index(x,compress(y))>0 as aa