Power function in SPARQL - math

Does anybody know if there is any power function added for SPARQL?
My quick-fix is using BIND and string process but it's not optimal at all:
?contract part:price ?price_E ;
part:Currency ?currency .
BIND(strafter(str(?price_E), "E") as ?E)
BIND(replace(strbefore(str(?price_E), "E"), "\[.\]", "") as ?nodot)
BIND(substr(?nodot, 0, xsd:integer(?E)+2) as ?first)
BIND(substr(?nodot, xsd:integer(?E)+2, 2) as ?second)
BIND(concat(?first, ".", ?second) as ?price)
Example of input and output data:
Input 1.51535E3
Output 1515.35

Sadly, the answer is no. As of SPARQL 1.1, the current W3C version, there is no exponentiation operator. You can accomplish it with repeated multiplication (x*x) as in:
(COUNT(?items)*COUNT(?items)
However, in your example, it is not clear that you actually need to do any arithmetic. The input 1.51535E3 is in fact already equal to 1515.35 because it is valid scientific notation, which SPARQL supports.

Related

How one can delete % from the name of an infix function?

Is it possible to define (+) function by R, s.t. able to work between its two arguments?
In other words, I'd like to delete % from the following infix function (but I can not and I don't know how this problem can be solved):
`%(+)%` <- function(x,y) { x+(2*y) }
2 %(+)% 3
User-defined infix operators must be surrounded by percent signs in R. So the answer to your question is, "you can't". Sorry.
From the R language definition:
R allows user-defined infix operators. These have the form of a string of characters delimited by the ‘%’ character. The string can contain any printable character except ‘%’. The escape sequences for strings do not apply here.
The only alternatives I can think of, both rather desperate:
if x and y are defined as members of an S4 class then you can overload dispatch for the + symbol
you could hack the R parser (not recommended!), as in this example, where someone forked a read-only Github mirror of R to modify the parser (described here).
I agree with Ben Bolker that you cannot define (+) without the %. However, if you are looking to create a function as per above why not use the following:
`&`<- function(x, y) { x+(2*y) }
2&3
#Use rm to remove the defined function
rm(`&`)

Marklogic collate sequence in XQuery

Is there a way to modify the elements a sequence so only collated versions of the items are returned?
let $currencies := ('dollar', 'Dollar', 'dollar ')
return fn:collated-only($currencies, "http://marklogic.com/collation/en/S1/T00BB/AS")
=> ('dollar', 'dollar', 'dollar')
The values that are stored in the range index (that feeds the facets) are literally the first value that was encountered that compared equal to the others. (Because, the collation says you don't care...)
You can get a long way by calling
fn:replace(fn:lower-case(xdmp:diacritic-less(fn:normalize-unicode($str,"NFKC"))),"\p{P}","")
This won't be exactly the same in that it overfolds some things and underfolds others, but it may be good for your purposes.
Is this the expected output? There is no fn:collated-only function, so I'm assuming you're asking how to write such a function or whether there is such a function.
The thing is, there isn't a mapping from one string to another in collation comparisons, there is only a comparison algorithm (the Unicode Collation Algorithm) so there really is no canonical kind of string to return to you, and therefore no API to do so.
Stepping back, what is the problem you are actually trying to solve? By the rules of that collation, "dollar" and "Dollar" are equivalent, and by using it you declare you don't care which form you use, so you could use either one.
If these values are in XML elements and you have a range index using http://marklogic.com/collation/en/S1/T00BB/AS, you can do something like this:
let $ref := cts:element-reference(xs:QName("currency"), "collation=http://marklogic.com/collation/en/S1/T00BB/AS")
for $curr in cts:values($ref, (), "frequency-order")
return $curr || ": " || cts:frequency($curr)
This will produce results like:
"dollar: 15",
"euro: 12"
... and so on. The collation will disregard the differences among your sample inputs. These results could be formatted however you want. Is that what you're looking to do?

Scientific notation issue in R

I have an ID variable with 20 digits. Once i read the data in R , it changes to Scientific notation and then if i write the same id to csv file, the value of ID changes.
For example , running the below code should print me the value of x as "12345678912345678912",but it prints "12345678912345679872":
Code:
options(scipen=999)
x <- 12345678912345678912
print(x)
Output:
[1] 12345678912345679872
My questions are :
1) Why it is happening ?
2) How to fix this problem ?
I know it has to do with the storage of data types in R but still i think there should be some way to deal with this problem. I hope i am clear with this question.
I don't know if this question was asked or not in so point me to a link if its a duplicate.I will remove this post
I have gone through this, so i can relate with the issue of mine, but i am unable to fix it.
Any help would be highly appreciated. Thanks
R does not by default handle integers numerically larger than 2147483647L.
If you append an L to your number (to tell R its an integer), you get:
x <- 12345678912345678912L
#Warning message:
#non-integer value 12345678912345678912L qualified with L; using numeric value
This also explains the change of the last digits as R stores the number as a double.
I think the gmp-package should be able to handle large numbers in general. You should therefore either accept the loss of precision, store them as character stings, or use a data-type from the gmp package.
To circumvent the problem due to number storing/representation, you can import your ID variable directly as character with the option colClasses, for example, if using read.csv and importing a data.frame with the ÌD column and another numeric column:
mydata<-read.csv("file.csv",colClasses=c("character","numeric"),...)
Using readr you can do
mydata <- readr::read_csv("file.csv", col_types = list(ID=col_character()))
where "ID" is the name of your ID column

Use greek letters to name list elements in R

Is there any way to name list elements in R using Greek letters? I'm asked to create a list that should look like list(α = 42).
The actual result of this expression is equivalent to the result of list(a = 42).
I know it is possible to use Greek letters in plots using expression(symbol("a")), but I couldn't find a solution to use Greek letters as names for list elements. Using as.character("\U03B1") results in an error, using just "\U03B1" leads to an "a". I doubt that it makes sense to use Greek letters anywhere else than plots, but this is homework, so I have to find a way (if there is one).
I have not tested this thoroughly, but R seems to take pretty much any symbol as a variable name without any problems and I could not find any specific rule about variable naming aside from this, taken from ?name
Names are limited to 10,000 bytes (and were to 256 bytes in versions of R before 2.13.0).
The code you posted works perfectly for me (R 3.0.1 running under Fedora Core 18):
> a <- list(α = 42)
> a
$α
[1] 42
That said, I would definitely suggest to just spell out the letter as "alpha", as it is more practical to write and maintain.
a <- list(alpha = 42)
Alternatively, name the list afterwards using your unicode symbols in the character vector:
ll <- as.list( 42 )
names(ll) <- "\u03B1"
ll
#$α
#[1] 42
Interesting that you need to do this. Also your list holds the meaning of life.

HTML and XML Parsing in Fortran

I am studying mathematical computation and I am completely stuck on this task! I don't even know how to go about starting it!
**Write a program in Fortran that can parse a single line of well-formed HTML or XML markup so that it takes input on a single line (guaranteed to not exceed 80 characters in total) like
-lots of lovely text
where
tag might be anything from 1 to 37 ASCII characters and will not contain spaces
text could contain spaces and be anything from 1 to 73 characters in length
so that the program outputs one of two lines:
tag : text if the two occurrences of tag match inside <...> and
syntax error if anything else is input.
Any help is hugely appreciated !**
There are a number of intrinsic functions for working with strings that may be helpful.
result = index(string, substring) - returns the position of the start of the first occurrence of string substring as a substring in string, counting from one. (Fortran 77)
result = scan(string, set) - scans a string for any of the characters in a set of characters. (Fortran 95)
result = verify(string, set) - verifies that all the characters in a string are present in a set. (Fortran 95)
There are a few user-contributed string tokenization functions on the Fortran Wiki that might be helpful:
delim, strtok, and find_field. Also, FLIBS includes some string manipulation and tokenization routines that might be useful as examples.
Finally, there are a number of existing open-source XML parsers written in Fortran: xmlf90 and xml-fortran. Looking at the source code for these libraries should be helpful.

Resources