How do I implement strtod? - c

For example lets take the value 10.23E-4. What I did was break it up into several variables, whole=10, decimal=23, decimalLength=2, isNegative=1, exponent=4 (is there a better name for these?)
First I d = decimal; d*/exp10(decimalLength); d+=whole;. This gets me 10.23. Next I wrote e = exp10(exponent); if (isNegative) e = 1/e; result = d*e;. I suspect this part is wrong. My result is 0.0010229999999999998 while strtod is 0.001023. What's the proper way to handle the exponent? Is there a good+fast implementation of strtod I can compare my code to?

Related

Recursive unitless element type

I am trying to come up with a function that gives me the recursive unitless element type. So for example, to shorten it let's call it ruet, I would like to have:
A = zeros(5,5)
reut(A) == Float64
using Unitful
A = zeros(5,5)*1u"kg"
reut(A) == Float64
AA = [zeros(5,5) for i in 1:5]
reut(AA) == Array{Float64,2}
AofA = [copy(A) for i in 1:5]
reut(AofA) == Array{Float64,2}
using StaticArrays
AofSA = [#SVector [2.0,3.0] for i in 1:5]
reut(AofSA) == SVector{2,Float64}
AofuSA = [#SVector [2.0u"kg",3.0u"kg"] for i in 1:5]
reut(AofuSA) == SVector{2,Float64}
So basically strip away the units but still return the correct element type, which could be an array. It's the array part that's hard. I can recurse:
recursive_eltype(a) = recursive_eltype(eltype(a))
recursive_eltype{T<:Number}(a::Type{T}) = eltype(a)
and then get the unitless element type:
uEltype = recursive_eltype(u)
uEltypeNoUnits = typeof(one(uEltype))
but then this is always the number type, and I can't seem to find a good way to get back the array types when it's an array of arrays, i.e. this method is returning Float64 in all of the examples above. I am wondering if dispatching on static arrays and using similar_type is required here.
Note that I would like the solution to, if possible, not have a requirement on Unitful.jl. Getting the unitless type for the number can be done via one(u), so I think this should be possible.
(Somewhat related Julia issue: https://github.com/JuliaLang/julia/issues/22216)
I came up with:
Base.#pure recursive_unitless_eltype(a) = recursive_unitless_eltype(eltype(a))
Base.#pure recursive_unitless_eltype{T<:StaticArray}(a::Type{T}) = similar_type(a,recursive_unitless_eltype(eltype(a)))
Base.#pure recursive_unitless_eltype{T<:Array}(a::Type{T}) = Array{recursive_unitless_eltype(eltype(a)),ndims(a)}
Base.#pure recursive_unitless_eltype{T<:Number}(a::Type{T}) = typeof(one(eltype(a)))
This still isn't fully generic, but works on quite a broad range of things.
Using Julia version 0.6.2-something (code surely not very portable):
function _reut(T)
try
T.name == Quantity.body.body.body.name && return _reut(T.parameters[1])
getfield(T.name.module, T.name.name){_reut.(collect(T.parameters))...}
catch
T
end
end
reut(T) = _reut(eltype(T))
And the tests in the question pass. Still not inferrable, but replaced eval with getfield(Module,Symbol). Where do you get these questions?

Most efficient way to implement matrix functions?

I've been trying for a while to figure out the most efficient way of handling a two dimensional array (i.e. a matrix) that contains a variable.
I construct a large matrix by concatenating smaller arrays, weighted with a variable. As a simplified example, this is what I currently do.
function myMatrix(x::Float64)
M = vcat(hcat(x*A, C), hcat(C, 2*x*B))
return M
end
const A = ones(2,2)
const B = ones(2,2)
const C = zeros(2,2)
y_values = collect(0:0.01:10)
for y in y_values
eivals, eivecs = eig(myMatrix(y))
dosomething(eivals,eivecs)
end
The problem: I have more (and more complex, non-diagonal) matrices and the final size is pretty large. At this moment, I call the function a few hundred times. Does anyone have a suggestion on what to do to make this process more efficient in terms of runtime?
Thanks in advance
EDIT: Based on the comment below, this does not answer the question.
Do you mean constructing the Matrix? One way of doing that which is ideomatic is to use a block Matrix:
M = [x*A C
C 2x*B]
Not sure if that's the most efficient in terms of runtime, sorry.
This solution, using preallocation as proposed somewhere in the comments, does not solve my problem directly (because of the way I construct the matrices) but it may still be useful for some people reading this. Also I don't guarantee for the initially used statement of "most efficient", as this seems to widely depend on your purposes, size of the matrices and so on. The method is also mentioned in the Performance Tips section of Julia.
Because there has been some confusion, consider the following example:
function myMatrix(x::Float64)
M = vcat(hcat(x*A, C), hcat(D, 2*x*B))
return M
end
function doSomething(A::Array{Float64,2})
nothing
end
const ArraySize = 1000
const A = ones(ArraySize,ArraySize)
const B = ones(ArraySize,ArraySize)
const C = rand(ArraySize,ArraySize)
const D = rand(ArraySize,ArraySize)
for i = 1:1000
ret = myMatrix( convert(Float64,i) )
doSomething(ret)
end
This does literally nothing but construct a BlockMatrix(function) from initial matrices depending on one parameter. I was thinking this repeated construction is redundant and indeed, one can preallocate the memory for the matrix by writing:
function xinc!(ret::Array{T,2}, x::T) where T
ret[1:ArraySize, 1:ArraySize] = x*A
ret[1:ArraySize, ArraySize+1:2*ArraySize] = C
ret[ArraySize+1:2*ArraySize, 1:ArraySize] = D
ret[ArraySize+1:2*ArraySize, ArraySize+1:2*ArraySize] = 2*x*B
nothing
end
function doSomething(A::Array{Float64,2})
nothing
end
const ArraySize = 1000
const A = ones(ArraySize,ArraySize)
const B = ones(ArraySize,ArraySize)
const C = rand(ArraySize,ArraySize)
const D = rand(ArraySize,ArraySize)
ret = Array{Float64}(2*ArraySize, 2*ArraySize)
for i = 1:1000
xinc!(ret, convert(Float64,i))
doSomething(ret)
end
For me, the second code executes in 9.866s while the first takes up 38.076s.
Edit: In response to the previous comment, if i write
function xinc!(ret::Array{T,2}, x::T) where T
ret = [x*A C
D 2*x*B]
nothing
end
the code takes 16.173s to execute. I don't know why, but this way of assigning the matrix is substantially slower.

Need suggestion on Code conversion to Matlab_extension 2

This is an extension of the previously asked question: link. In a short, I am trying to convert a C program into Matlab and looking for your suggestion to improve the code as the code is not giving the correct output. Did I convert xor the best way possible?
C Code:
void rc4(char *key, char *data){
://Other parts of the program
:
:
i = j = 0;
int k;
for (k=0;k<strlen(data);k++){
:
:
has[k] = data[k]^S[(S[i]+S[j]) %256];
}
int main()
{
char key[] = "Key";
char sdata[] = "Data";
rc4(key,sdata);
}
Matlab code:
function has = rc4(key, data)
://Other parts of the program
:
:
i=0; j=0;
for k=0:length(data)-1
:
:
out(k+1) = S(mod(S(i+1)+S(j+1), 256)+1);
v(k+1)=double(data(k+1))-48;
C = bitxor(v,out);
data_show =dec2hex(C);
has = data_show;
end
It looks like you're doing bitwise XOR on 64-bit doubles. [Edit: or not, seems I forgot bitxor() will do an implicit conversion to integer - still, an implicit conversion may not always do what you expect, so my point remains, plus it's far more efficient to store 8-bit integer data in the appropriate type rather than double]
To replicate the C code, if key, data, out and S are not already the correct type you can either convert them explicitly - with e.g. key = int8(key) - or if they're being read from a file even better to use the precision argument to fread() to create them as the correct type in the first place. If this is in fact already happening in the not-shown code then you simply need to remove the conversion to double and let v be int8 as well.
Second, k is being used incorrectly - Matlab arrays are 1-indexed so either k needs to loop over 1:length(data) or (if the zero-based value of k is used as i and j are) then you need to index data by k+1.
(side note: who is x and where did he come from?)
Third, you appear to be constructing v as an array the same size of data - if this is correct then you should take the bitxor() and following lines outside the loop. Since they work on entire arrays you're needlessly repeating this every iteration instead of doing it just once at the end when the arrays are full.
As a general aside, since converting C code to Matlab code can sometimes be tricky (and converting C code to efficient Matlab code very much more so), if it's purely a case of wanting to use some existing non-trivial C code from within Matlab then it's often far easier to just wrap it in a MEX function. Of course if it's more of a programming exercise or way to explore the algorithm, then the pain of converting it, trying to vectorise it well, etc. is worthwhile and, dare I say it, (eventually) fun.

How to create numeric array from a string in MATLAB?

How to create numeric array from a string in Matlab?
For example I have such a string:
>> str = dec2bin(7);
s = 111
I need the array [1 1 1]. How to do it?
I see strread function strread
but I get difficulties to use it with non-space string input.
The standard solution is to use the solution posted by yuk,
a = (str == '1');
which produces a logical result. If you need a double,
a = double(str == '1');
or even just:
a = +(str == '1');
Perhaps the simplest looking solution is this one:
a = str - 48;
although I think the last is least obvious as to what it does. I prefer code that is easy to read and understand the purpose. That goal is best met by the second solution, IMHO.
Just answered another question and found a part of it might be useful here.
You can actually convert such a string to a logical vector:
a = str == '1';
You can cast it to another type, for example double(a).
I suppose, naively:
n = length(s);
myArray = zeros(1,n)
for i = 1:n
myArray(i) = double(s(i));
where "double()" is whatever the command is for changing a string element to a double precision number, if that is indeed what you want.
With strread:
a = strread('123', '%c')
The answer is using "bitget"
> x = bitget(7,1:3);
> class(bitget(7,1:3))
ans =
double
The result is double.

Most Pythonic way equivalent for: while ((x = next()) != END)

What's the best Python idiom for this C construct?
while ((x = next()) != END) {
....
}
I don't have the ability to recode next().
update: and the answer from seems to be:
for x in iter(next, END):
....
#Mark Harrison's answer:
for x in iter(next_, END):
....
Here's an excerpt from Python's documentation:
iter(o[, sentinel])
Return an iterator object.
...(snip)... If the second argument, sentinel, is given, then o must be
a callable object. The iterator
created in this case will call o
with no arguments for each call to its
next() method; if the value returned
is equal to sentinel,
StopIteration will be raised,
otherwise the value will be returned.
It depends a bit what you want to do. To match your example as far as possible, I would make next a generator and iterate over it:
def next():
for num in range(10):
yield num
for x in next():
print x
Short answer: there's no way to do inline variable assignment in a while loop in Python. Meaning that I cannot say:
while x=next():
// do something here!
Since that's not possible, there are a number of "idiomatically correct" ways of doing this:
while 1:
x = next()
if x != END:
// Blah
else:
break
Obviously, this is kind of ugly. You can also use one of the "iterator" approaches listed above, but, again, that may not be ideal. Finally, you can use the "pita pocket" approach that I actually just found while googling:
class Pita( object ):
__slots__ = ('pocket',)
marker = object()
def __init__(self, v=marker):
if v is not self.marker:
self.pocket = v
def __call__(self, v=marker):
if v is not self.marker:
self.pocket = v
return self.pocket
Now you can do:
p = Pita()
while p( next() ) != END:
// do stuff with p.pocket!
Thanks for this question; learning about the __call__ idiom was really cool! :)
EDIT: I'd like to give credit where credit is due. The 'pita pocket' idiom was found here
Maybe it's not terribly idiomatic, but I'd be inclined to go with
x = next()
while x != END:
do_something_with_x
x = next()
... but that's because I find that sort of thing easy to read
What are you trying to do here?
If you're iterating over a list, you can use for e in L where e is the element and L is the list. If you're filtering a list, you can use list comprehensions (i.e. [ e for e in L if e % 2 == 0 ] to get all the even numbers in a list).
Can you provide more information about what you're trying to accomplish? It's not clear to me why you can't just say
for x in everything():
...
and have the everything function return everything, instead of writing a next function to just return one thing at a time. Generators can even do this quite efficiently.
If you need to do this more than once, the pythonic way would use an iterator
for x in iternext():
do_something_with_x
where iternext would be defined using something like
(explicit is better than implicit!):
def iternext():
x = next()
while x != END:
yield x
x = next()

Resources