Could we use uniroot() instead of optimize() for this minimization in R?
In the last line of my R code below, I use optimize()
to find the df2
that minimizes the ncp_diff
function.
However, I was wondering if I could uniroot()
instead of optimize()
for this minimization?
alpha = c(.025, .975); df1 = 3; peta = .3 # The input
f < function(alpha, q, df1, df2, ncp){ # Notice `ncp` is the unknown
alpha  suppressWarnings(pf(q = (peta / df1) / ((1  peta)/df2), df1, df2, ncp, lower = FALSE))
}
ncp < function(df2){ # Root finding: finds 2 `ncp` for a given `df2`
b < sapply(c(alpha[1], alpha[2]),
function(x) uniroot(f, c(0, 1e7), alpha = x, q = peta, df1 = df1, df2 = df2)[[1]])
b / (b + (df2 + 4))
}
ncp_diff < function(df2, target = 0.15){
the_ncp < ncp(df2)
return(abs(abs(the_ncp[2]  the_ncp[1])  target))
}
optimize(ncp_diff, c(0, 1000)) ## HERE can I use `uniroot()` instead of `optimize()`
1 answer

alpha = c(.025, .975); df1 = 3; peta = .3 # The input f < function(alpha, q, df1, df2, ncp){ # Notice `ncp` is the unknown alpha  suppressWarnings(pf(q = (peta / df1) / ((1  peta)/df2), df1, df2, ncp, lower = FALSE)) } ncp < function(df2){ # Root finding: finds 2 `ncp` for a given `df2` b < sapply(c(alpha[1], alpha[2]), function(x) uniroot(f, c(0, 1e7), alpha = x, q = peta, df1 = df1, df2 = df2)[[1]]) b / (b + (df2 + 4)) } ncp_diff < function(df2, target = 0.15){ the_ncp < ncp(df2) return((the_ncp[2]  the_ncp[1])  target) } uniroot(ncp_diff, c(100, 1000)) # $root [1] 336.3956 $f.root [1] 3.74663e09 $iter [1] 7 $init.it [1] NA $estim.prec [1] 6.103516e05
Edit:
In order to use the same interval of (0,1000) we can look for a way to go around a scenario where both the lower and upper values produce results on the same side of the number line. Since this is an error in r, we can go through it by
tryCatch
ncp < function(df2){ # Root finding: finds 2 `ncp` for a given `df2` b < sapply(c(alpha[1], alpha[2]), function(x) tryCatch(uniroot(f, c(0, 1e7), alpha = x, q = peta, df1 = df1, df2 = df2)[[1]], error =function(e)NA )) if(any(is.na(b)))b= c(1,10) b / (b + (df2 + 4)) } uniroot(ncp_diff, c(0, 1000)) # $root [1] 336.3956 $f.root [1] 2.132438e09 $iter [1] 8 $init.it [1] NA $estim.prec [1] 6.103516e05
See also questions close to this topic

Using ts() with multiple observation per date
Date u v w Feeder 01/01/2017 7 7 3 a 01/01/2017 2 49 32 e 01/01/2017 79 87 23 i 01/02/2017 0 0 0 a 01/02/2017 0 0 0 e 01/02/2017 789 78 33 i ... ... ... 01/05/2018 0 0 0 a 01/05/2018 789 78 33 e 01/05/2018 25 78 22 i
Above is the data table format I am using. My ultimate goal is to do time series analysis using
tslm()
.df is myDataFrame.
TS < ts(df1[,2:length(df)],frequency = 12, start = c(2017,1), end =c(2018,12)
My Problem:
With such a code I will have only one row per month and my "Feeder" column will be filled with NA's. Moreover, it will exhibit 24 months whereas I only have 17 months.
EDIT: I assume to have the same hour for every row.

How to apply database queries in R on .txt file
I want to select some portion from
file1.txt
and save that in another file namedfile2.txt
. File1.txt:abc 1 6 a abc 2 7 b efg 3 8 c abc 4 9 d efg 5 10 e
Database query that I want to apply here is (not syntax specific):
file2.txt < select col2, col3, col4 from file1.txt where col1=abc
File2.txt:
1 6 a 2 7 b 4 9 d
Is there any way to apply database type query in R on a text file? I know that we can search a letter in .txt file by using
grep()
function. But I'm unable to find any online help for this? Does anyone know how can I resolve my problem? Thank you in advance :) Please don't mark this question duplicate to:This question is something different. Moreover I cannot use
sqldf
because this package is not for .txt files. 
VisCap all WE samples fail qc
Hello I am trying to analyse Whole Exome CNVs with viscap, but all samples qc turn up as FAIL.
The paper writes: 'VisCap automatically repeats the analysis until all samples pass this automated QC. In our laboratory, an entire batch is failed if fewer than three samples pass QC. Files generated by each iteration are stored in separate output folders.'
So my run returns an error because it is trying to work with a new matrix with zero columns (because zero samples passed qc). The Tool xhmm is creating the covarage sample interval_summary files viscap works with. We use this ViscapRscript also to analyse CNVs for genepanels and it works perfectly, always creating multiple output folders without failur.Does anyone have an idea what might cause this error? Am I using some default values that are unappropriate for WE data?

"i" variable in "for" statement valid inside functions?
I am using this structure in python: call a function from a for loop; the function, inside, uses the i value of the iterator. Should I also pass the i value? or is it global? what is the best practice here?
def function_1(A): (use if "i" inside here) for i in range (etc etc): function_1(A)

Reverse direction of loop through an array containing functions, without jQuery
I have the following Javascript which aims to enable users to cycle backwards and forwards through a number of menus using an
array
of functions executed by aneventListener
...var i = 0; var steps = [ function one() {do something}, function two() {do something}, function three() {do something}, function four() {do something}, function five() {do something} ] //loops forward through array *successfully* document.getElementById('forward').addEventListener("click", function() { steps[i](); } //loops backwards through array *unsuccessfully* document.getElementById('backward').addEventListener("click", function() { steps[i]().reverse(); }
The first
eventListener
function is successful in looping through thearray
, however, the secondeventListener
function backward, fails to loop through the array in reverse (to little suprise).Firstly, I am unsure on how to loop an
array
, that contains functions, in reverse.Secondly, I am wondering if it's possible to loop an
array
in reverse from a particular point in thatarray
; essentially, if thearray
has looped through, and had gone no further than function three(), is there a script that will allow a user to loop backwards to functions two() then one(), then forward again?I have read a number of questions here on StackOverflow concerning array.reverse(), however none that I've found seem to concern the presence of functions and/or reversing the loop midarray.
So thank you for any answers, they're greatly appreciated!

how to sort the result of multiquery?
i use mysqli_multi_query() function and get result of two query in such a way that all first query result is sort date wise first and second query after that! here is my queries:
$query = "SELECT `b`.`class_name`, `c`.`main_code`,`c`.`account_title`, `a`.`ledger_entry`, `a`.`balance` AS `debit`, `a`.`date`, `a`.`v.no`, `a`.`interactive_person` FROM `vouchers` AS `a`,`classes` AS `b`, `data` AS `c` WHERE `a`.`post_status`='yes' AND `a`.`cancel_status`='off' AND `a`.`type`='CP' AND `a`.`class_id` = `b`.`class_id` AND `a`.`account_id`=`c`.`account_code` AND `a`.`date` BETWEEN '$fromDate' AND '$toDate';"; $query .= "SELECT `b`.`class_name`, `c`.`main_code`,`c`.`account_title`, `a`.`ledger_entry`, sum(`a`.`balance`) AS `credit`, `a`.`date`, `a`.`v.no`, `a`.`interactive_person` FROM `vouchers` AS `a`,`classes` AS `b`,`data` AS `c` WHERE `a`.`post_status`='yes' AND `a`.`cancel_status`='off' AND `a`.`type`='CP' AND `a`.`class_id` = `b`.`class_id` AND `a`.`interactive_person`=`c`.`main_code` AND `a`.`date` BETWEEN '$fromDate' AND '$toDate' GROUP BY `v.no`;";
these are two query and now let me show you the way i use multiquery function:
if (mysqli_multi_query($con, $query)) { do { // Store first result set if ($result = mysqli_store_result($con)) { // Fetch one and one row while ($row = mysqli_fetch_assoc($result)) { if ($y > 700) { $y = 30; $pdf>AddPage(); $x = 36; } $y = $y + 18; $x = 10; $debit = 0; $credit = $row['credit']; if($credit==""){$credit = 0;} $debit = $row['debit']; if($debit==""){$debit = 0;} $pdf>SetFont('Times', 'B', 12, '', true); $pdf>MultiCell(70, 18, $row['date'], 1, 'L', true, 1, $x, $y, true, 0, false, true, 0, 'T', false); $x = $x + 70; $pdf>SetFont('Times', '', 12, '', true); $pdf>MultiCell(50, 18, $row['v.no'], 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false); $x = $x + 50; $pdf>MultiCell(100, 18, $row['main_code'], 1, 'L', true, 1, $x, $y, true, 0, false, true, 0, 'T', false); $x = $x + 70; $pdf>MultiCell(100, 18, $row['account_title'], 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false); $x = $x + 100; $pdf>MultiCell(100, 18, $row['class_name'], 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false); $x = $x + 100; $pdf>MultiCell(120, 18,$credit , 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false); $x = $x + 120; $pdf>MultiCell(80, 18, $debit , 1, 'C', true, 1, $x, $y, true, 0, false, true, 0, 'T', false); } // Free result set mysqli_free_result($result); } } while (mysqli_next_result($con));
}
everything work perfectly and i get output in pdf as: thats what i get in pdf
so now what i want to do is sort the details i got order by "ref number" or "date". TEll ME HOW TO DO IT ? Can i do it with any php function ? or should i change the technique of fetching data from database ?

How to rebuild a linear model from NBC?
In sklearn doc, there are two functions named as intercept_ and coef_ for interpreting MultinomialNB as a linear model. May I ask how to use these two parameters to rebuild a linear model? Math derivation is really appreciated.

How to convert a string with mathematical operators to number (integer or float)
I have a problem with converting a string like "(5+2)*3" to be able to evaluate to be 21.
Here is my code:
char inp[10]; printf("Write it: "); scanf("%s", inp); printf("Okay, computing!\n"); printf("INPUT: %s \n", inp); printf("It's %d \n", (int)inp);
I would like to get a number at the end. This code is working:
printf("19+31 is '''%d'''", 19+31);
And i need to use the second argument  the 19+31  to be my input.
The first code outputs this:
Write it: 4+4 Okay, computing! INPUT: 4+4 It's 329554704

Split a number per unit of token python function
I'm trying to create a function that receive as argument a number and return an array of 3 numbers max.
I have 3 tokens. 1 unit, 5 unit and 25 unit.
calculateUnit(4) should = [4,0,0] calculateUnit(7) should = [2,1,0] (because 2 unit of 1 and 1 unit of 5 = 7) calculateUnit(36) should = [1,2,1] (because 1 unit of 1, 2 unit of 5 and 1 unit of 25 = 36)
Could someone help me? I have a basic code and I think I need to use math., but I'm kinda bad in math and tired of multiple trying. I already tried to search here but I may not use the correct terms.

VS2017 compiler emitting 2 division instructions for a division/remainder pair
I'm working on improving the runtime speed of a C program while trying to avoid the use of inline assembly. The program is not far from 100x speedup compared to its initial performance, which means improvements are getting harder to come by.
While inspecting the assembly code generated by VS2017 (in Release x64 mode, with /O2 compiler optimizations), it came to my attention that one of my hotspots in the program corresponds to the following code (anonymized):
bool j, k = false; do { j = false; for (i = 0; i < c>a; i++) { int x, y, z; x = c>b[i]; y = (x/c>d) + 1; z = (x % c>d) + 1; if (c>e[y][z] == 0) { c>b[i] = c>b[c>a]; continue; } else if (c>e[y][z] + c>g[y][z] == c>h[y][z]  '0') { f(c, y, z); k = true; j = true; c>b[i] = c>b[c>a]; continue; } } } while (j); return k;
In particular, I call attention to the following statements (c>d is an int):
y = (x/c>d) + 1; z = (x % c>d) + 1;
This is a division followed by remainder using the same parameters in both cases. Since the division instruction in x86/x64 returns both the division and the remainder at the same time, I expected this would be compiled to a single division instruction. Yet the compiler's output basically does everything twice, including reloading c>d from memory:
00007FF790061FA0 42 8B 04 1F mov eax,dword ptr [rdi+r11] 00007FF790061FA4 99 cdq 00007FF790061FA5 F7 7E 28 idiv eax,dword ptr [rsi+28h] 00007FF790061FA8 4C 63 D0 movsxd r10,eax 00007FF790061FAB 42 8B 04 1F mov eax,dword ptr [rdi+r11] 00007FF790061FAF 99 cdq 00007FF790061FB0 F7 7E 28 idiv eax,dword ptr [rsi+28h] ... a few instructions later ... 00007FF790061FC3 4C 63 D2 movsxd r10,edx
I've tried various transformations to my code to make it emit a single division instruction, unsuccessfully. The closest I got was transforming the above C code block into this:
y = (x/c>d); z = (x % c>d); y++;
This led to the following code being emitted:
00007FF7942B1FA0 42 8B 04 17 mov eax,dword ptr [rdi+r10] 00007FF7942B1FA4 99 cdq 00007FF7942B1FA5 F7 7E 28 idiv eax,dword ptr [rsi+28h] 00007FF7942B1FA8 4C 63 F2 movsxd r14,edx 00007FF7942B1FAB 44 8D 60 01 lea r12d,[rax+1] 00007FF7942B1FAF 48 98 cdqe 00007FF7942B1FB1 48 8D 14 C5 08 00 00 00 lea rdx,[rax*8+8]
Unfortunately, without adding
z++
as well, the code is wrong and my program no longer works, as expected. As soon as I add backz++
aftery++
, the first version of the assembly code is emitted again.What kind of code transformations or compiler flags could I use to coerce VS2017 to emit efficient code in this case?

How to optimize complicated layout
open activity is slowly.How to optimize it?

Create a function that always returns zero, but the optimizer doesn't know
I would like to create a function that always returns zero, but this fact should not be obvious to the optimizer, so that subsequent calculations using the value won't constantfold away due to the "known zero" status.
In the absence of linktime optimization, this is generally as simple as putting this in its own compilation unit:
int zero() { return 0; }
The optimizer can't see across units, so the alwayszero nature of this function won't be discovered.
However, I need something that works with LTO and with as many possible future clever optimizations as well. I considered reading from a global:
int x; int zero() { return x; }
... but it seems to me that a sufficiently smart compiler could notice that
x
is never written to and still decidezero()
is always zero.I considered using a
volatile
, like:int zero() { volatile int x = 0; return x; }
... but the actual semantics of the required side effects of volatile reads aren't exactly clear, and would not seem to exclude the possibility that the function still returns zero.
Such an alwayszerobutnotatcompiletime value is useful in several scenarios, such as forcing a noop dependency between two values. Something like:
a += b & zero()
causesa
to depend onb
in the final binary, but doesn't change the value ofa
.Don't answer this by telling me the "standard doesn't guarantee any way to do this"  I'm well aware and I'm looking for a practical answer and not language from the standard.

How to determine the components of A·v, where A is the coefficient matrix and v is a vector
For solving conjugate gradient problems in Python, I am trying to determine how to properly input and calculate each row of matrix Av put into this function:
from numpy import dot from math import sqrt def conjGrad(Av,x,b,tol=1.0e9): n = len(b) r = b  Av(x) s = r.copy() for i in range(n): u = Av(s) alpha = dot(s,r)/dot(s,u) x = x + alpha*s r = b  Av(x) if(sqrt(dot(r,r))) < tol: break else: beta = dot(r,u)/dot(s,u) s = r + beta*s return x,i
Here is the full question I'm aiming to solve: Conjugate Gradient Example

how to check the following in matlab?
I have a problem with writing a simple code. I am trying to check if my matrix is [a 0 0 ;0 a 0;0 0 a,b 0 0;0 b 0;0 0 b] (it's like stacking two different identity matrices).
How can I do this in matlab?
Thanks in advance

optimize sequence of matlab operations for solving a system of linear equations
I need to apply some sequence of operations with big matrices in matlab in order to solve a system of linear equations. In the beginning I though that this would be quite fast considering that matlab applies some internal optimization (at least that was my understanding). However, it seems that I was wrong. The initial sequence of operations that I am applying are the following:
nbf = size(F,1); if issparse(F) F = sparse(diag(r))*F; else F = diag(r)*F; end if issparse(F) K = sparse(eye(nbf))  F; else K = eye(nbf)  F; end R = K\E;
where
F
is a square matrixMxM
, whereM = 50000
or more and almost all the times is not sparse.r
andE
areMx1
vectors, andE
is sparse. These operations on a powerful machine take around 25 minutes which is quite slow for my needs(in the ideal case I would like to have some real time computation but I do not think I can achieve that, I would be happy though if someone proves me wrong). Thus, I am trying to find some ways to reduce this computation time. A first idea was to see what I can get if I replace theF = diag(r)*F;
andK = eye(nbf)  F;
withF = bsxfun(@mtimes,diag(r),F);
andK = bsxfun(@minus,eye(nbf),F);
respectively. Regarding solving the inverse matrix operationR = K\E
I came upon this https://it.mathworks.com/help/matlab/ref/decomposition.html or if you are not able to use thedecomposition()
function you could replace it with the correspondingfactorize()
from here, thus it could be replaced withR = decomposition(K)\E
. However, this improved the computation time just by little 22 mins instead of 25. Therefore, I would like to ask if someone knows any suggestions or other ways how I could optimize these operations for computation speed.