Most efficient "next-day" function? - python-3.x

I want to write a function that takes 'yyyymmdd' as its input and generates the next day (same 'yyyymmdd' format output). This function can be applied to solve problems like next-week or next-t-days.
I know that this task can be easily done using the time.delta approach. However, what is inside the time library? I mean, what sort of computation did it do to solve this "next-day" problem?
In Python, I wrote my own next-day function using a long if-elif-..-else statement on the day-level when int(dd) >= 28. It worked, but looks extremely ugly and inefficient... So I start to be curious about what's written inside the time library...
In particular, I want to know what is the most efficient way of performing such next-day task, and can we prove its optimality of efficiency?
Thank you very much for your help!

Related

Difference between R::runif() and Rcpp::runif()

I'm learning to use Rcpp in R. Would you please explain me the difference between R::runif() and Rcpp::runif().
I mean 3 questions:
Do these 2 functions produce the same stream of random numbers given that we set the same seed before running each of them ?
Which function is preferable when using Rcpp ? I mean, it seems to me that the 2 functions produce the same thing, but Rcpp::runif() will run more fastly.
How to call Rcpp::runif() in a .R file ? Is it true that the Rcpp::runif() can be called only from a .cpp file and not in R? (I mean, it seems to me that the function Rcpp::runif() is of extensively used to write other C++ functions, then I will import that function by sourcecpp() to use in R)
Thank you very much for your help!
I suspect this question is a duplicate so I may close this but here goes:
Yes they do. The whole point of the RNG interfaces is guaranteeing just that
Entirely up to you. Sometimes you want to wrap or use a C API example and you have R::runif() for that. Sometimes you want efficient vector operations for which you have Rcpp::runif().
You write a C++ function accessing the C++ API. Note that not all those functions will be faster than calling what R offers when what R offers is already vectorised. Your wrapping of Rcpp::runif() will not be much different in performance from calling stats::runif(). You use the C++ accessor in C++ code writing something you cannot easily get from R.
Edit: This Rcpp Gallery post has some background and examples.

Computing numeric derivatives with pyomo

I need to repeatedly compute derivatives at a given point of nonlinear pyomo constraints. According to this post, there are basically two options: one symbolic approach (which uses sympy) and one numeric approach via the NL file generated by Pyomo.
I have tried the symbolic approach which looks like:
def gradient(constr, variables):
grad_num = np.array([value(partial) for partial in
differentiate(constr.body, wrt_list=variables)])
if (not (is_leq_constr(constr))):
grad_num = -grad_num
return grad_num
In principle, this approach works. However, when dealing with larger problems, the function call takes very long and I would expect the numeric approach to be much faster.
Is this true? If so, can anyone help me, to "translate" the above function to the gjh_asl_json approach? I am grateful for any hint.

Does Julia have a way to solve for unknown variables

Is there a function in Julia that is similar to the solver function in Excel where I can provide and equation, and it will solve for the unknown variable? If not, does anybody know the math behind Excel's solver function?
I am not expecting anybody to solve the equation, but if it helps:
Price = (Earnings_1/(1+r)^1)+(Earnings_2/(1+r)^2)++(Earnings_3/(1+r)^3)+(Earnings_4/(1+r)^4)+(Earnings_5/(1+r)^5)+(((Earnings_5)(RiskFreeRate))/((1+r)^5)(1-RiskFreeRate))
The known variables are: Price, All Earnings, and RiskFreeRate. I am just trying to figure out how to solve for r.
Write this instead as an expression f(r) = 0 by subtracting Price over to the other side. Now it's a rootfinding problem. If you only have one variable you're solving for (looks to be the case), then Roots.jl is a good choice.
fzero(f, a::Real, b::Real)
will search for a solution between a and b for example, and the docs have more choices for algorithms when you don't know a range to start with and only give an initial condition for example.
In addition, KINSOL in Sundials.jl is good when you know you're starting close to a multidimensional root. For multidimensional and needing some robustness to the initial condition, I'd recommend using NLsolve.jl.
There's nothing out of the box no. Root finding is a science in itself.
Luckily for you, your function has an analytic first derivative with respect to r. That means that you can use Newton Raphson, which will be extremely stable for your function.
I'm sure you're aware your function behaves badly around r = -1.

Shorter version of IF(ISERROR(...))

I'm wondering if there is a way to check if a formula returns an error, and if not, use the value found, without doing the following:
=IF(ISERROR(A1/B1), 0, A1/B1)
The syntax I'm looking for is something like this:
=EQ([value_if_not_error], [value_if_error])
The solution might contain more stuff, IF and ISERROR is perfectly fine, as long as I avoid having the main function several times in each cell. The reason why I want to do this is that my equations are quite long and the readability is drastically reduced when I have to write the equation twice, or even more (if several ifs).
Is there a simple solution to this?
Try
=IFERROR([formula], [value_if_error])

VBA Exp() Overflow

Here's a very easy question for the VBA guys. I'm running Newton's method on some functions, and occasionally I find a guess that I can only assume overflows the Exp() function (and stops the code). What suggestions do you guys have to simply handle this case? (Maybe some sort of error handling?)
If Newton's method fails because of this or any sort of blowup, I would like to proceed onto my bisection code below that point.
By the way, I have thought about maybe taking logs to make this situation less likely, but to be honest I am working with some math I do not yet completely understand, and I would like to handle the case of Newton's method failing in any case first.
Disclaimer: I'm a complete VBA beginner, so any suggestions would be read and appreciated. Thanks in advance.
Edit: I've been asked to post the code. First, thanks for reading. Unfortunately, I can't post the entire code due to business reasons, but I can give the very barebones outline. I've created a module and a function. Inside this function, I have:
Newtons Method Loop
Bisection Loop
Inside the Newton's method loop, I've traced to a point where I have a next guess of something around 28,000 or so, and I am assigning to a variable h the value Exp(28,000) or roundabouts. The debugger breaks at that point; my code essentially exits, and whatever value my function should be returning produces #VALUE! in my cell.
I know this is not a lot of information, but I hope (and think) it should be enough. Correct me if I am wrong.
Edit 2: If all else fails, I'm going to explicitly catch too large values, but I wonder if there is a more robust and elegant solution.
Not surprising it will overflow given that Exp(28,000) is 1.8x1012160, the maximum value you can pass to Exp is ~709.
If you want to exit your loop if you encounter a value that is too large, just check the value before passing it;
function Newton
const MAX_EXP_ARGUMENT as double = 709.782712893#
do ....
if (abs(var) <= MAX_EXP_ARGUMENT) then
r = exp(var)
else
exit do '// exit the loop
end if
'//use r
loop
do ....
There was a SO issue posted a while back dealing with numbers larger than Long in VBA.
The accepted answer pointed to a link called Large Number Arithmetic. I just tried to implement your example of exp(28000) using that example, but received a "Type Mismatch" error after processing a few loops. However, it may be the fault of my hasty implementation. If you've got no leads so far, I would start there.

Resources