Regression for power law

Question: This is a cross post from Math SE.
I have some data (running time of an algorithm) and I think it follows a power law
$$y_\mathrm{reg} = k x^a$$
I want to determine $k$ and $a$. What I have done so far is to do a linear regression (least squares) through $\log(x), \log(y)$ and determine $k$ and $a$ from its coefficients.
My problem is that since the "absolute" error is minimized for the "log-log data", what is minimized when you look at the original data is the quotient
$$\frac{y}{y_\mathrm{reg}}$$
This leads to large absolute error for large values of $y$. Is there any way to make a "power-law regression" that minimizes the actual "absolute" error? Or at least does a better job at minimizing it?

Example:

The red curve is fit through the whole dataset. The green curve is fit through the last 21 points only.
Here is the data for the plot.
The left column are the values of $n$ ($x$-axis), the right column are the values of $t$ ($y$-axis)
<code>1.000000000000000000e+02,1.944999820000248248e-03
1.120000000000000000e+02,1.278203080000253058e-03
1.250000000000000000e+02,2.479853309999952970e-03
1.410000000000000000e+02,2.767649050000500332e-03
1.580000000000000000e+02,3.161272610000196315e-03
1.770000000000000000e+02,3.536506440000266715e-03
1.990000000000000000e+02,3.165302929999711402e-03
2.230000000000000000e+02,3.115432719999944224e-03
2.510000000000000000e+02,4.102446610000356694e-03
2.810000000000000000e+02,6.248937529999807478e-03
3.160000000000000000e+02,4.109296799998674206e-03
3.540000000000000000e+02,8.410178100001530418e-03
3.980000000000000000e+02,9.524117600000181830e-03
4.460000000000000000e+02,8.694799099998817837e-03
5.010000000000000000e+02,1.267794469999898935e-02
5.620000000000000000e+02,1.376997950000031709e-02
6.300000000000000000e+02,1.553864030000227069e-02
7.070000000000000000e+02,1.608576049999897034e-02
7.940000000000000000e+02,2.055535920000011244e-02
8.910000000000000000e+02,2.381920090000448978e-02
1.000000000000000000e+03,2.922614199999884477e-02
1.122000000000000000e+03,1.785056299999610019e-02
1.258000000000000000e+03,3.823622889999569313e-02
1.412000000000000000e+03,3.297452850000013452e-02
1.584000000000000000e+03,4.841355780000071440e-02
1.778000000000000000e+03,4.927822640000271981e-02
1.995000000000000000e+03,6.248602919999939054e-02
2.238000000000000000e+03,7.927740400003813193e-02
2.511000000000000000e+03,9.425949999996419137e-02
2.818000000000000000e+03,1.212073290000148518e-01
3.162000000000000000e+03,1.363937510000141629e-01
3.548000000000000000e+03,1.598689289999697394e-01
3.981000000000000000e+03,2.055201890000262210e-01
4.466000000000000000e+03,2.308686839999722906e-01
5.011000000000000000e+03,2.683506760000113900e-01
5.623000000000000000e+03,3.307920660000149837e-01
6.309000000000000000e+03,3.641307770000139499e-01
7.079000000000000000e+03,5.151283440000042901e-01
7.943000000000000000e+03,5.910637860000065302e-01
8.912000000000000000e+03,5.568920769999863296e-01
1.000000000000000000e+04,6.339683309999486482e-01
1.258900000000000000e+04,1.250584726999989016e+00
1.584800000000000000e+04,1.820368430999963039e+00
1.995200000000000000e+04,2.750779816999994409e+00
2.511800000000000000e+04,4.136365994000016144e+00
3.162200000000000000e+04,5.498797844000023360e+00
3.981000000000000000e+04,7.895301083999981984e+00
5.011800000000000000e+04,9.843239714999981516e+00
6.309500000000000000e+04,1.641506008199996813e+01
7.943200000000000000e+04,2.786652209900000798e+01
1.000000000000000000e+05,3.607965075100003105e+01
1.258920000000000000e+05,5.501840400599996883e+01
1.584890000000000000e+05,8.544515980200003469e+01
1.995260000000000000e+05,1.273598972439999670e+02
2.511880000000000000e+05,1.870695913819999987e+02
3.162270000000000000e+05,3.076423412130000088e+02
3.981070000000000000e+05,4.243025571930002116e+02
5.011870000000000000e+05,6.972544795499998145e+02
6.309570000000000000e+05,1.137165088436000133e+03
7.943280000000000000e+05,1.615926472178005497e+03
1.000000000000000000e+06,2.734825116088002687e+03
1.584893000000000000e+06,6.900561992643000849e+03
</code>
(sorry for the messy scientific notation)
Comment: Be aware that the [help/on-topic] says "*Please note, however, that cross-posting is not encouraged on SE sites. Choose one best location to post your question. Later, if it proves better suited on another site, it can be migrated.*"
Comment: it's quite plain from your plot that a single power law does not fit these data. You may find some value in the other power law questions on site. It may also be worth your time to read the paper by Aaron Clauset, Cosma Rohilla Shalizi, M. E. J. Newman (2009),
"Power-law distributions in empirical data,"
SIAM Review 51, 661-703
(see the arXiv version here [arXiv:0706.1062v2](http://arxiv.org/abs/0706.1062)); and [this](http://bactra.org/weblog/491.html) post by Shalizi.
Comment: Some relevant information [here](http://stats.stackexchange.com/questions/61600/strange-outcome-when-performing-nonlinear-least-squares-fit-to-a-power-law)
Answer: If you want equal error-variance on every observation in the untransformed scale, you can use nonlinear least squares.
(This will often not be suitable; errors over many orders of magnitude are rarely constant in size.)
If we go ahead and use it nonetheless, we get a much closer fit to the later values:

And if we examine residuals we can see that my warning above is entirely well-founded:

This shows that the variability is not constant on the original scale (and that the fit of this single power curve doesn't fit all that well at the high end either, since there's distinct curvature in the third quarter of range of the log values on the x-scale -- between about 0 and 5 on the x-axis above). The variability is nearer to constant in the log scale (though it's a little more variable in relative terms at low values than high ones there).
What it would be best to do here depends on what you're trying to achieve.
Comment: @kjetilbhalvorsen There's better ways to achieve that, but the fact that the model doesn't fit is a clear warning that if interest was to predict even slightly outside the range of the data, the predictions will be too low, perhaps dramatically so.  The fit to only the last 21 points still doesn't fit the curvature within that part of the data.
Comment: In this case, a fit that is better for the largest $y$-values might be appropriate, as we dont matter so much the running time when it is low ...
Answer: A paper by Lin and Tegmark nicely summarizes the reasons why lognormal and/or markov process distributions fail to fit data displaying critical, power law behaviors... https://ai2-s2-pdfs.s3.amazonaws.com/5ba0/3a03d844f10d7b4861d3b116818afe2b75f2.pdf . As they note, "Markov processes...fail epically by predicting exponentially decaying mutual information..." Their solution and recommendation is to employ deep learning neural networks such as long-short-term memory (LSTM) models.
Being old school and neither conversant nor comfortable with NNs or LSTMs, I will give a tip of the hat to @glen_b's nonlinear approach. However, I prefer more tractable and readily accessible workarounds such as value-based quantile regression. Having used this approach on heavy tailed insurance claims, I know that it can provide a much better fit to the tails than more traditional methods, including multiplicative, log-log models. The modest challenge in using QR is finding the appropriate quantile around which to base one's model(s). Typically, this is much greater than the median. That said, I don't want to oversell this method as there remained significant lack of fit in the most extreme values of the tail.
Hyndman, et al (http://robjhyndman.com/papers/sig-alternate.pdf), propose an alternative QR they term boosting additive quantile regression. Their approach builds models across a full range or grid of quantiles, producing probabilistic estimates or forecasts which can be evaluated with any one of the extreme value distributions, e.g., Cauchy, Levy-stable, whatever. I have yet to employ their method but it seems promising.
Another approach to extreme value modeling is known as POT or peak over threshold models. This involves setting a threshold or cut-off for an empirical distribution of values and modeling only the largest values that fall above the cutoff based on a GEV or generalized extreme value distribution. The advantage to this approach is that any possible future extreme value can be calibrated or located based on the parameters from the model. However, the method has the obvious disadvantage that one is not using the full PDF.
Finally, in a 2013 paper, J.P. Bouchaud proposes the RFIM (random field ising model) for modeling complex information displaying criticality and heavy tailed behaviors such as herding, trends, avalanches, and so on. Bouchaud falls in a class of polymaths that should include the likes of Mandelbrot, Shannon, Tukey, Turing, etc. I can claim to be highly intrigued by his discussion while, at the same time, being intimidated by the rigors involved with implementing his suggestions. https://www.researchgate.net/profile/Jean-Philippe_Bouchaud/publication/230788728_Crises_and_Collective_Socio-Economic_Phenomena_Simple_Models_and_Challenges/links/5682d40008ae051f9aee7ee9.pdf?inViewer=0&pdfJsDownload=0&origin=publication_detail

Regression for power law


Help for understanding a vectorial equation found in a paper.

Question: Trying to implement a code for the algorithm described in this paper I found something not very clear to me that leads me to misunderstand the whole concept.
To calculate the vector $\vec{b_{3d}}$ the paper (page 3) suggests to use the following equation:

To me it look like a normal vectorial equation where you divide one vector by its normalized value. Something very trivial.
But I was wondering that the variables $e_{x}, e_{v}$ are previously defined as a vector of the error of the variables $x, v$ as:
$ e_{x} = x-x_{d} \quad e_{v} = v-v_{d} $
at the beginning of the paper where $x,v$ are vectors: $ x \in \mathbb{R}^{3} $ and $ v \in \mathbb{R}^{3} $
So now I have the following questions:

I suppose that the equation above is not useful to get $\vec{b_{3d}}$ if I treat $x,v$ as vectors. But in this case I cannot do nothing since in the paper doesn't explain which vector element should I consider to calculate $\vec{b_{3d}}$;
In the equation compares the term $e_{3}$ which is the unity vector of the $Z$ axis. Maybe the authors wanted  to point out that you should consider the gravity along Z, but why should I put a unity vector in an equation, if it is suppose to have all term as scalar?;

Anyway... good day.
Comment: I don't know about anyone else, but your picture of the equation isn't displaying on my page and I've tried refreshing a couple of times.
Comment: Look at equation nr. 12 on this [paper](http://www.math.ucsd.edu/~mleok/pdf/LeLeMc2010_quadrotor.pdf)
Answer: the expression is a vector equation as you initially thought.
ex,ev,e3,xd and hence xd'' are also vectors.
2. mg is a scalar and the force acts in -e3(-Z) direction.Force is a vector.
From a first glance it seems like you find b3d corresponding to a the xd'' acceleration.Also b3d doesnt seem to be vertically downward (as its name suggests) but in a direction depending on centre of mass vs rotor position.Maybe thats why its b3d and not just b3
Even if you dont have x,v it appears like you know ex,ev (based on geometry?)


Block is not showing up in a custom taxonomy page template

Question: I am using Drupal 7
I have a content type courses. I have a page folder in template folder, with courses.tpl.php file. When i go to courses page it is showing contents from courses.tpl.php. Issues is that courses.tpl.php is not rendering blocks.
I checked region in info file. I cleared cache etc, but block is not rendering.
I am not sure, if this page is a taxonomy page. 'footer_testimonials' is the block i am trying to render in this page, but it is not showing up.
'footer_testimonials' is showing up in page.tpl.php but not in courses.tpl.php

    <?php print drupal_render(drupal_get_form('course_searchbox'));?>

<?php print $page['page']['course_page_top_ad']['block_1']['#markup'];?>

Popular Courses: Certificate IV in Bookkeeping, Diploma of Accounting, Diploma of Business, Advanced Diploma of Accounting

Courses

    Central College Distance Education offers a range of Government and Industry Accredited courses ready to help launch your career.  Remember, Central College has been launching careers for over 30 years.  You may also be interested in our specialty Central College Certificates that will help you better manage your existing business and do it in your own time at your own pace, right in the comfort of your home.

<?php
//$taxoTerm = course::get_taxonomy_term(1);
$categories = course::getAllCategories();

foreach($categories as $category):?>

<?php echo $category->name;?> Courses

            <?php //get courses by taxonomy
                $courses_term = course::courses_by_term($category->tid);
                $i = 0;
                foreach($courses_term as $course):
                $courseObj = course::getCourseByNid($course->nid);
                    $i++;
                ?>

<a href="<?php echo course::courseUrl_alias($course->nid);;?>"><?php echo $courseObj->title;?>

                <?php endforeach;?>

<?php
if($i%2==0):
    echo '

';
endif;

endforeach;
?>

Testimonials

<?php print render($page['footer_testimonials']);?>

Comment: Hello, and welcome to Drupal Answers. May you show the code you are using? Without that, it is not possible to answer about what is wrong in your code.
Comment: Are you using module_invoke?
Comment: @kiamlaluno : added code of the page into question.
Comment: @Sathiya: not using module_invoke
Comment: That is perfect. Is that the code for courses.tpl.php?
Comment: @kiamlaluno: yes, courses.tpl.php is the page.
Answer: A page.tpl.php template file doesn't get blocks to render; it gets the theme regions to render, which by default are the following ones:

$page['help']: Dynamic help text, mostly for admin pages
$page['highlighted']: Items for the highlighted content region
$page['content']: The main content of the current page
$page['sidebar_first']: Items for the first sidebar
$page['sidebar_second']: Items for the second sidebar
$page['header']: Items for the header region
$page['footer']: Items for the footer region

Themes can define other regions as well.
When you want to show a blocks that has been associated to the "highlighted" region, the page template file needs to contain the following code (or similar).
<?php if ($page['highlighted']): ?>
<?php print render($page['highlighted']); ?>
<?php endif; ?>

Without that code, a block that should visible in the "highlighted" region would not be visible.
In a taxonomy term template file, you need to get the block content using code similar to the following one.
  global $theme_key;

  $query = db_select('block', 'b');
  $block = $query
    ->fields('b')
    ->condition('b.theme', $theme_key)
    ->condition('b.status', 1)
    ->condition('b.delta', $delta)
    ->condition('b.module', $module)
    ->orderBy('b.region')
    ->orderBy('b.weight')
    ->orderBy('b.module')
    ->addTag('block_load')
    ->addTag('translatable')
    ->execute()
    ->fetchObject();

  if ($block) {
    $array = module_invoke($block->module, 'block_view', $block->delta);
    drupal_alter(array('block_view', "block_view_{$block->module}_{$block->delta}"), $array, $block);

    if (isset($array) && is_array($array)) {
      foreach ($array as $k => $v) {
        $block->$k = $v;
      }
    }

    if (isset($block->content) && $block->content) {
      // Normalize to the drupal_render() structure.
      if (is_string($block->content)) {
        $block->content = array('#markup' => $block->content);
      }

      print render($block->content);
    }
  }

You need to know the module implementing the block, and the delta used for that block. It is better to put the code in the preprocess function to set a variable that is then rendered in the template file. As usual, the logic code should be in the template.php file of the theme, or in the module file, and the rendering code in the template file. (Don't confuse the template.php file with the template file.)
Instead of executing that code, it is easier to assign the block you want to show in the taxonomy term page to taxonomy term pages. In the settings page for the block (admin/structure/block/manage/<block id>/form/configure) select Only the listed pages, and in the textarea enter:

taxonomy/term
  taxonomy/term/*
Comment: courses.tpl.php is a taxonomy page template. I tried your suggestion and did not work. Do you know, how to render block in a taxonomy page template.


Oracle - Order by Alpha numeric

Question: I need to order the rows in my result set by a column that holds varchar2 K-12.
Example:
ID Grade Expense
1 1 500
1 10 500
1 11 500
1 12 500
1 2 500
1 3 500
1 4 500
1 5 500
1 6 500
1 7 500
1 8 500
1 9 500
1 K 500

This is my order by clause which works, but I would like to have the
  row with Grade = K as the first row for each ID in my result set.

order by ID, to_number(regexp_substr(grade, '^[[:digit:]]*'))

As it stands, the result set has the row with ID = K is last and not
  first.  How can i make it the first row for each ID in my result set?

ID Grade Expense
1 K 500
1 1 500
1 2 500
1 3 500
1 4 500
1 5 500
1 6 500
1 7 500
1 8 500
1 9 500
1 10 500
1 11 500
1 12 500

Thanks in advance
Answer: Simply use a case statement to set K to something below 1.  this has the advantage if you have a Pre-K later, you can modify the case to handle it as well.
With CTE as
  (SELECT '1' as grade from dual union
   SELECT '2' from dual union
   select '10' from dual union
   select 'K' from dual)
SELECT * FROM CTE
ORDER BY CASE GRADE when 'K' then -1
                    else to_number(regexp_substr(grade, '^[[:digit:]]*')) end

Answer: This is a bit of a kludge, but since the regex for 'K' returns null, change the order by to:
order by ID, nvl(to_number(regexp_substr(grade, '^[[:digit:]]*')),0)

This will return 0 for 'K' and sort it properly.
Answer: Only change the ORDER BY clause, this way:
order by ID asc, decode(grade,'K',-1,grade) asc

Comment: You'll also want to convert `grade` to a number (I think what you have above will give a type mismatch plus it will sort ASCII meaning `12` will precede `2`, etc.).
Comment: No, I've tested with a column of type VARCHAR2(2), and he's sorting ASCending this way : K, 1, 2, 10, 12, 20 --- He's doing implicit conversion apparently. If you add one record with for example 'L', the same SELECT replies with `01722:Invalid Number`. But that means that if you make sure all generated values (1,2,10,12,20 but also -1) are numbers ... it works. (11gR2 database)
Comment: Apparently, the output of the first result is the data type which DECODE returns. In the above case, that is the data type of -1, which is a NUMBER. If column `GRADE` is a number (even if implicitally converted from VARCHAR2) for all other records, he's consequently converting to a number, and also ordering likewise. Until he spots a record from which he can't convert the value to a number, like 'L'. In that case, the Select indeed fails, and you would need other code. The OP didn't specify that case, but it could be interesting to include handling of other values.
Answer: You can do the following:
WITH g1 AS (
  SELECT 1 AS id, TO_CHAR(level) AS grade, 500 AS expense FROM dual
 CONNECT BY level <= 12
   UNION
  SELECT 1, 'K', 500 FROM dual
   UNION
  SELECT 1, 'J', 500 FROM dual
)
SELECT g1.*, TO_NUMBER(REGEXP_SUBSTR(grade, '^\d+'))
     , DECODE(grade, 'K', -1, TO_NUMBER(REGEXP_SUBSTR(grade, '^\d+')))
  FROM g1
 ORDER BY DECODE(grade, 'K', -1, TO_NUMBER(REGEXP_SUBSTR(grade, '^\d+'))) NULLS LAST

In this query I'm using CONNECT BY to build your grade table; of course you'll want to ignore that part. Note I added an extra row with a J for the grade level.
In my order by I am using DECODE() so that if grade = 'K', it will give a value of -1. For any grades that can be converted to numeric values (that is, if they start with at least one digit), I use a regex to get as many digits if possible (you can use [:digit:] or [0-9] in place of \d; but \d is nice and short).
I am specifying NULLS LAST so that any rows for which grade cannot be converted to a number, other than K, will be last.
I'm including the extra computed columns just to give a glimpse into what is actually going on and how the values are generated. They aren't needed for the query.
Please see SQL Fiddle demo here.

Oracle - Order by Alpha numeric


Excel - Matching data and writing to another column

Question: I am looking to do some matching of data to display in a 3rd.
I have a column of folders and the second column shows what users have access to that folder, So:
Column1 Column2
Folder1 User1
Folder1 User2
Folder2 User1
Folder3 User1

I have a list of users I want to search for (I made a "unique" list based off of Column2) and want to find the match in Column2 and in Column3 put the User with the corresponding folders separated by a delimiter. It does not have to be column3 it could even be another sheet, the data is column3 is the only data I will need once done. So Column3 would look like:
User1|Folder1|Folder2|Folder3
User2|Folder1

Does this make sense?
Thanks for any help you can give.
Scott
Comment: If you are using the latest 365 or online excel then `TEXTJOIN("|",TRUE,IF($B$1:$B$5 = "User1", $A$1:$A$5, ""))`. This is an array formula so confirm with ctrl-shift-enter..  Other wise you will need to sort on column 2 and use a helper column to concatenate each row with the one below it. Then use a vlookup to get the first.  If sorting is not what you want then vba is the only way. [HERE](http://stackoverflow.com/questions/35565694/get-unique-value-list-in-excel/35566254#35566254) are examples of the formula route.
Comment: This helped me figure it out. Thanks!!!
Answer: Using Chip's UDF located here and included below you can then on your target cell place the following formula:
=TRIM(stringconcat(" | ",IF(B:B="User1",A:A,"")))

This will output all folders concatenated with the "|" delimiter for "User1". Cheers,
StringConcat code:
Function StringConcat(Sep As String, ParamArray Args()) As Variant
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' StringConcat
' By Chip Pearson, firstname.lastname@example.com, www.cpearson.com
'                  www.cpearson.com/Excel/stringconcatenation.aspx
' This function concatenates all the elements in the Args array,
' delimited by the Sep character, into a single string. This function
' can be used in an array formula. There is a VBA imposed limit that
' a string in a passed in array (e.g.,  calling this function from
' an array formula in a worksheet cell) must be less than 256 characters.
' See the comments at STRING TOO LONG HANDLING for details.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim S As String
Dim N As Long
Dim M As Long
Dim R As Range
Dim NumDims As Long
Dim LB As Long
Dim IsArrayAlloc As Boolean

'''''''''''''''''''''''''''''''''''''''''''
' If no parameters were passed in, return
' vbNullString.
'''''''''''''''''''''''''''''''''''''''''''
If UBound(Args) - LBound(Args) + 1 = 0 Then
    StringConcat = vbNullString
    Exit Function
End If

For N = LBound(Args) To UBound(Args)
    ''''''''''''''''''''''''''''''''''''''''''''''''
    ' Loop through the Args
    ''''''''''''''''''''''''''''''''''''''''''''''''
    If IsObject(Args(N)) = True Then
        '''''''''''''''''''''''''''''''''''''
        ' OBJECT
        ' If we have an object, ensure it
        ' it a Range. The Range object
        ' is the only type of object we'll
        ' work with. Anything else causes
        ' a #VALUE error.
        ''''''''''''''''''''''''''''''''''''
        If TypeOf Args(N) Is Excel.Range Then
            '''''''''''''''''''''''''''''''''''''''''
            ' If it is a Range, loop through the
            ' cells and create append the elements
            ' to the string S.
            '''''''''''''''''''''''''''''''''''''''''
            For Each R In Args(N).Cells
                If Len(R.Text) > 0 Then
                    S = S & R.Text & Sep
                End If
            Next R
        Else
            '''''''''''''''''''''''''''''''''
            ' Unsupported object type. Return
            ' a #VALUE error.
            '''''''''''''''''''''''''''''''''
            StringConcat = CVErr(xlErrValue)
            Exit Function
        End If

    ElseIf IsArray(Args(N)) = True Then
        '''''''''''''''''''''''''''''''''''''
        ' ARRAY
        ' If Args(N) is an array, ensure it
        ' is an allocated array.
        '''''''''''''''''''''''''''''''''''''
        IsArrayAlloc = (Not IsError(LBound(Args(N))) And _
            (LBound(Args(N)) <= UBound(Args(N))))
        If IsArrayAlloc = True Then
            ''''''''''''''''''''''''''''''''''''
            ' The array is allocated. Determine
            ' the number of dimensions of the
            ' array.
            '''''''''''''''''''''''''''''''''''''
            NumDims = 1
            On Error Resume Next
            Err.Clear
            NumDims = 1
            Do Until Err.Number <> 0
                LB = LBound(Args(N), NumDims)
                If Err.Number = 0 Then
                    NumDims = NumDims + 1
                Else
                    NumDims = NumDims - 1
                End If
            Loop
            On Error GoTo 0
            Err.Clear
            ''''''''''''''''''''''''''''''''''
            ' The array must have either
            ' one or two dimensions. Greater
            ' that two caues a #VALUE error.
            ''''''''''''''''''''''''''''''''''
            If NumDims > 2 Then
                StringConcat = CVErr(xlErrValue)
                Exit Function
            End If
            If NumDims = 1 Then
                For M = LBound(Args(N)) To UBound(Args(N))
                    If Args(N)(M) <> vbNullString Then
                        S = S & Args(N)(M) & Sep
                    End If
                Next M

            Else
                ''''''''''''''''''''''''''''''''''''''''''''''''
                ' STRING TOO LONG HANDLING
                ' Here, the error handler must be set to either
                '   On Error GoTo ContinueLoop
                '   or
                '   On Error GoTo ErrH
                ' If you use ErrH, then any error, including
                ' a string too long error, will cause the function
                ' to return #VALUE and quit. If you use ContinueLoop,
                ' the problematic value is ignored and not included
                ' in the result, and the result is the concatenation
                ' of all non-error values in the input. This code is
                ' used in the case that an input string is longer than
                ' 255 characters.
                ''''''''''''''''''''''''''''''''''''''''''''''''
                On Error GoTo ContinueLoop
                'On Error GoTo ErrH
                Err.Clear
                For M = LBound(Args(N), 1) To UBound(Args(N), 1)
                    If Args(N)(M, 1) <> vbNullString Then
                        S = S & Args(N)(M, 1) & Sep
                    End If
                Next M
                Err.Clear
                M = LBound(Args(N), 2)
                If Err.Number = 0 Then
                    For M = LBound(Args(N), 2) To UBound(Args(N), 2)
                        If Args(N)(M, 2) <> vbNullString Then
                            S = S & Args(N)(M, 2) & Sep
                        End If
                    Next M
                End If
                On Error GoTo ErrH:
            End If
        Else
            If Args(N) <> vbNullString Then
                S = S & Args(N) & Sep
            End If
        End If
        Else
        On Error Resume Next
        If Args(N) <> vbNullString Then
            S = S & Args(N) & Sep
        End If
        On Error GoTo 0
    End If
ContinueLoop:
Next N

'''''''''''''''''''''''''''''
' Remove the trailing Sep
'''''''''''''''''''''''''''''
If Len(Sep) > 0 Then
    If Len(S) > 0 Then
        S = Left(S, Len(S) - Len(Sep))
    End If
End If

StringConcat = S
'''''''''''''''''''''''''''''
' Success. Get out.
'''''''''''''''''''''''''''''
Exit Function
ErrH:
'''''''''''''''''''''''''''''
' Error. Return #VALUE
'''''''''''''''''''''''''''''
StringConcat = CVErr(xlErrValue)
End Function


Programmatically create Azure AD Service Principals in .NET

Question: I'm looking for a current example on how to programmatically create Service Principals in .NET (using another Service Principal). Including setting a certificate credential (ideally one which is stored/generated in Azure Key Vault)
I found this old answer but no current equivalent which would probably need to use the Microsoft.Graph Nuget package?!
Answer: I tried to reproduce the same in my environment and got below results:
I have one certificate named sricert in my Azure Key Vault like below:

Now I registered one Azure AD application and added same certificate in it like below:

Make sure to add and grant admin consent to below API permission that is required to create Azure AD application:

Now I ran below c# code that fetches certificate from Azure key vault and creates new Azure AD application using Microsoft.Graph package via existing service principal like below:
using Azure.Identity;
using Azure.Security.KeyVault.Certificates;
using Azure.Security.KeyVault.Secrets;
using Microsoft.Graph;
using Microsoft.Identity.Client;
using System;
using System.Net.Http.Headers;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string tenantId = "58e70374-11f2-4e91-af40-xxxxxxxxx";
            string clientId = "cd88c0ed-30e9-4624-bfc6-xxxxxxxx";
            string certificateName = "sricert";
            string keyVaultUri = $"https://srikeyv.vault.azure.net/";

            // Authenticate with Azure Key Vault and obtain an access token
            var credential = new DefaultAzureCredential();
            var certClient = new CertificateClient(new Uri(keyVaultUri), credential);
            var cert = await certClient.GetCertificateAsync(certificateName);

            var secretClient = new SecretClient(new Uri(keyVaultUri), credential);
            var secret = await secretClient.GetSecretAsync(cert.Value.Name, cert.Value.Properties.Version);

            var certificateBytes = Convert.FromBase64String(secret.Value.Value);
            var certificate = new X509Certificate2(certificateBytes);

            var app = ConfidentialClientApplicationBuilder
                .Create(clientId)
                .WithAuthority($"https://login.microsoftonline.com/{tenantId}")
                .WithCertificate(certificate)
                .Build();

            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
            var accessToken = result.AccessToken;

            // Create a new Azure AD application using the Microsoft Graph API
            var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
            {
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
                return Task.FromResult(0);
            }));

            var application = new Application
            {
                DisplayName = "Sri GraphSP",
                SignInAudience = "AzureADMyOrg",
                PublicClient = new Microsoft.Graph.PublicClientApplication
                {
                    RedirectUris = new[] { "https://jwt.ms" },
                },
            };

            await graphClient.Applications
                .Request()
                .AddAsync(application);

            Console.WriteLine("New Azure AD application created successfully");
        }
    }
}

Response:

To confirm that, I checked the same in Azure Portal where new Azure AD application named Sri GraphSP created successfully like below:
Comment: Take a look at my solution, you can also create the GraphClient with a TokenCredential
Comment: Thanks for this! In the meantime I also figured this out. Posted my solution below, including creating the certificate and service principal
Answer: I figured it out after a while, including creating a certificate in Key Vault first:
var graphClient = new GraphServiceClient(new DefaultAzureCredential());

var certName = "my-sp-cert";

var certificateClient = new CertificateClient(new Uri("https://my.vault.azure.net"), new DefaultAzureCredential());
var op = await certificateClient.StartCreateCertificateAsync(certName, CertificatePolicy.Default);
var certResponse = await op.WaitForCompletionAsync();
var cert = certResponse.Value;

// First, we need to create an App Registration, aka "AAD Application"
var application = new Application
{
    DisplayName = "my-app",
    KeyCredentials = new List<KeyCredential>()
    {
        new KeyCredential
        {
            StartDateTime = cert.Properties.CreatedOn,
            EndDateTime = cert.Properties.ExpiresOn,
            Type = "AsymmetricX509Cert",
            Usage = "Verify",
            Key = cert.Cer,
            DisplayName = certName
        }
    }
};
var createdApp = await graphClient.Applications.Request().AddAsync(application);

_logger.LogInformation("Created AAD application {appId}, {objectId}", createdApp.AppId, createdApp.Id);

// Next, we add a Service Principal (SPN) to the App Registration
var spn = new ServicePrincipal()
{
    AppId = createdApp.AppId
};

var createdSpn = await graphClient.ServicePrincipals.Request().AddAsync(spn);

_logger.LogInformation("Created AAD service principal {clientId}, {objectId}", createdSpn.AppId, createdSpn.Id);

Comment: I guess I must have misunderstood your last sentence in the question as _`I don't want to` use the Microsoft.Graph Nuget package_
Answer: az ad sp create-for-rbac using the Azure CLI is current and cross-platform.
Comment: yes, but I'm asking for a way in .NET ;)


munin plugin with broken graph

Question: I've modified this munin plugin so that it tracks multiple values on the same graph. The symbolic link file name in my case is ipt_accounting_mail_web.
Its purpose is to track both web and mail traffic, and I added the needed iptables rules and munin configuration to run it as root. Here is my code, just for reference:
#!/bin/sh
#
# iptables Accounting Tool - based on ipt_accounting_ by Markus Frosch aka lazyfrosch
#
# See comments in original version for instructions
#
# Magic markers (optional - used by munin-config and some installation
# scripts):
#
#%# family=auto
#%# capabilities=autoconf suggest

if [ "$2" = "debug" ] ; then
    set -x
fi

ACCS=`basename $0 | sed s/^ipt_accounting_//g`

if [ "$1" = "autoconf" ]; then
        if [ -r /proc/net/dev ]; then
                iptables -L INPUT -v -n -x -w >/dev/null 2>/dev/null
                if [ $? -gt 0 ]; then
                        echo "no (could not run iptables as user `whoami`)"
                else
                        echo yes
                fi
        else
                echo "no (/proc/net/dev not found)"
        fi
        exit 0
fi

if [ "$1" = "suggest" ]; then
    iptables -L INPUT -v -x -n -w 2>/dev/null | sed -n 's/^.*\/\* ACC\-\([a-zA-Z]*\) \*\/.*$/\1/p' | sort | uniq
        exit 0
fi

if [ "$1" = "config" ]; then

        echo -n "graph_order "
    for ACC in $(echo "$ACCS" | tr '_' ' ') ; do
        echo -n $ACC-out" "$ACC-in" "
    done
    echo
    echo "graph_title iptables traffic for $(echo $ACCS | sed -e 's/_/,/g')"
#        echo graph_args --base 1000
        echo graph_vlabel bytes
        echo graph_category network
    for ACC in $(echo "$ACCS" | tr '_' ' ') ; do
          echo $ACC-out.label $ACC-sent
          echo $ACC-out.type DERIVE
          echo $ACC-out.min 0
          echo $ACC-out.cdef $ACC-out,8,*
          echo $ACC-in.label $ACC-recv
          echo $ACC-in.type DERIVE
          echo $ACC-in.min 0
          echo $ACC-in.cdef $ACC-in,8,*
    done
        exit 0
fi;

for ACC in $(echo "$ACCS" | tr '_' ' ') ; do
  iptables -L INPUT -v -n -x -w | grep "\/\* ACC\-"$ACC" \*\/" | awk "{s+=\$2} END { printf \"$ACC-in.value %.0f\\n\", s}"
  iptables -L OUTPUT -v -n -x -w | grep "\/\* ACC\-"$ACC" \*\/" | awk "{s+=\$2} END { printf \"$ACC-out.value %.0f\\n\", s}"
done

If I test it through munin-run it works.
root@m-node:~# munin-run ipt_accounting_mail_web
mail-in.value 3120
mail-out.value 760
web-in.value 23273
web-out.value 357000

This is the output of munin-run ipt_accounting_mail_web config:
root@m-node:~# munin-run ipt_accounting_mail_web config
graph_order mail-out mail-in web-out web-in
graph_title iptables traffic for mail,web
graph_vlabel bytes
graph_category network
mail-out.label mail-sent
mail-out.type DERIVE
mail-out.min 0
mail-out.cdef mail-out,8,*
mail-in.label mail-recv
mail-in.type DERIVE
mail-in.min 0
mail-in.cdef mail-in,8,*
web-out.label web-sent
web-out.type DERIVE
web-out.min 0
web-out.cdef web-out,8,*
web-in.label web-recv
web-in.type DERIVE
web-in.min 0
web-in.cdef web-in,8,*

However on the master images are broken. Here is what I get:

Unfortunately the error reported in the master logfile is not very useful:
root@controlpanels:/var/log/munin# cat munin-cgi-graph.log | tail -n1
2018/12/08 10:26:22 [WARNING] Could not draw graph "/var/lib/munin/cgi-tmp/munin-cgi-graph/m-node/m-node/ipt_accounting_mail_web-pinpoint=1544153129,1544261129.png?&lower_limit=&upper_limit=&size_x=800&size_y=400": /var/lib/munin/cgi-tmp/munin-cgi-graph/m-node/m-node/ipt_accounting_mail_web-pinpoint=1544153129,1544261129.png?&lower_limit=&upper_limit=&size_x=800&size_y=400

Are there more detailed logs of what's going on? Or can you spot the problem in my code right away?
Comment: The only idea I have is that the dash in variable names may be forbidden. Could you update the script to see if you can use "web_in" instead of "web-in" (and other of course) ?
Comment: Could you add the result of "munin-run ipt_accounting_mail_web config" ?
Comment: Replacing dashes with underscores does it. If you turn your comment into an answer I can accept it, thanks!
Answer: You can change the variables names with a dash to an underscore. Munin is sensible on the names. So you must use 'web_in' instead of 'web-in'

munin plugin with broken graph


On the complete solution to $x^2+y^2=z^k$ for odd $k$?

Question: While trying to answer this question, I was looking at a computer output of solutions to $x^2+y^2 = z^k$ for odd $k$ and noticed certain patterns. For example, for $k=5$ we have $x,y,z$,
$$10, 55, 5\\25, 50, 5\\38, 41, 5\\117, 598, 13\\122, 597, 13\\338, 507, 13\\799, 884, 17$$
Question: Is it true that all integer solutions to $x^2+y^2=z^k$ for odd $k>1$ are given by just two formulas, namely,

Primitives $\gcd(x,y)=1$:
$$A^2+B^2 = (a^2+b^2)^k\tag1$$
where $A,B$ is the expansion of $(a+bi)^k = A+Bi$. Example, $$(a+bi)^3 =(a^3 - 3 a b^2)+ (3 a^2 b - b^3)i$$ hence, $$(a^3 - 3 a b^2)^2+ (3 a^2 b - b^3)^2 = (a^2+b^2)^3$$ and so on for other $k$.
Non-primitives $\gcd(x,y)\neq1$:
$$a^2(a^2+b^2)^{k-1}+b^2(a^2+b^2)^{k-1} = (a^2+b^2)^k\tag2$$
where, for both Forms $1$ and $2$, we use some rational $a,b$?

Example. The first three solutions for $k=5$ above use:
$$a,b = 2/5,\;11/5\quad \text{Form 2}$$
$$a,b = 1,\;2\quad \text{Form 2}$$
$$a,b = -2,\;1\quad \text{Form 1}$$
Comment: For $k=3,$ all coprime solutions with $x,y\le10^4$ can be found [here](http://i.stack.imgur.com/rO9ZA.png). For $k\in\{5,7,9\},$ they can be found [here](http://i.stack.imgur.com/D8nGm.png).
Comment: @Lucian: Thanks. I can barely see the fine print. :)
Comment: Click on the picture to enlarge it.
Comment: @Lucian: Fine! are you sure they are all the solutions for 5, 7 and 9?
Comment: @Ataulfo: For coprime $x,y\le10^4.$
Comment: @ Thank you. So, for big coprimes it could be more....? The ideal is to find some parameterization because all computer has its limits of calculation.
Answer: If $x^2+y^2=z^k$, then $(x+iy)(x-iy)=z^k$. The Gaussians are a UFD, so if $\gcd(x+iy,x-iy)=1$ then this forces $x+iy=(r+si)^k$ for some integers $r$ and $s$. I think this is your form 1.
If there exists $d$ dividing both $x+iy$ and $x-iy$, then $d$ must divide their sum, $2x$, and their difference, $2iy$. So if $x$ and $y$ are relatively prime, then $d$ is nontrivial if and only if $x$ and $y$ are both odd, and $d=1+i$. But if $x$ and $y$ are odd, then $x^2+y^2\equiv2\bmod4$, while $z^k\equiv0\bmod4$, contradiction.
So we're left with the case where there is an integer $d>1$ dividing both $x$ and $y$. To be continued....
Comment: Yes, that is my form 1. I added $k=3$ as an example.
Answer: Good news: the answer is Yes. Moreover, all integer solutions are given by just one formula - the second one. It covers both primitive and non-primitive solutions. That is, for every solution $(x,y,z)$ to $x^2+y^2=z^k$  there exist rational numbers $a,b$ such that $x=a(a^2+b^2)^{(k-1)/2}$, $y=b(a^2+b^2)^{(k-1)/2}$ and $z=a^2+b^2$.
Bad news: This formula is quite useless. The reason is that you use rational parameters to describe integer solutions, and the question for which rational parameters the corresponding values of $x,y,z$ are integers is as difficult as solving the equation.
Now the proof. Take $a=\frac{x}{z^{(k-1)/2}}$, $b=\frac{y}{z^{(k-1)/2}}$. Then $a^2+b^2=(x^2+y^2)/z^{k-1} = z^k/z^{k-1}=z$, $a(a^2+b^2)^{(k-1)/2}=\frac{x}{z^{(k-1)/2}}z^{(k-1)/2}=x$ and $b(a^2+b^2)^{(k-1)/2}=y$.
Answer: Must write more correctly. $Z$ representation as a sum of squares gives the standard approach.
$$X^2+Y^2=Z^{k}$$
Uses the standard formula of the Brahmaputra. View the sum of the squares piece.
$$X^2+Y^2=(ab+cd)^2+(cb-ad)^2=(a^2+c^2)(b^2+d^2)$$
So the sum of squares is converted until
$$(b_1^2+d_1^2)...(b_{k}^2+d_{k}^2)=Z^{2k}$$
$$Z^2=b_1^2+d_1^2=...=b_{k}^2+d_{k}^2$$
Odd.
$$(a^2+b^2)(b_1^2+d_1^2).....(b_{k}^2+d_{k}^2)=Z*Z^{2k}$$
$$Z^2=b_1^2+d_1^2=...=b_{k}^2+d_{k}^2$$
$$Z=a^2+b^2$$
$$b_1=b_2=...=b_{k}=a^2-b^2$$
$$d_1=d_2=...=d_{k}=2ab$$
Will as an example this equation.
$$X^2+Y^2=Z^5$$
$$(xb+yd)^2+(yb-xd)^2=(x^2+y^2)(b^2+d^2)=(a^2+c^2)(b^2+d^2)(b^2+d^2)=Z*Z^2*Z^2$$
$$Z=a^2+c^2$$
$$x=ab+cd$$
$$y=cb-ad$$
$$d=a^2-c^2$$
$$b=2ac$$
And then the decision on the record.
$$X=a^5-10c^2a^3+5ac^4$$
$$Y=c^5-10a^2c^3+5ca^4$$
So these numbers are multiples of each its option.


Realizar consulta após conclusão de cadastro com Ajax/PHP

Question: help!
Estou realizando um cadastro com AJAX e PHP, o Cadastro está OK, porém, preciso executar uma busca logo depois que o Cadastro é concluído, a estrutura é MVC:
VIEW -cadastrarencomenda.php
CONTROLLER - controle-encomenda.php
MODEL - modelsencomenda.php
ASSETS - encomenda.JS
Quando realizo o o cadastro de uma encomenda, o meu controle retorna o ID dessa encomenda cadastrada, eu quero pegar esse ID e executar logo em seguida o outro método LocalizarViagem($idencomenda), se eu colocar no navegador (http://localhost/levarte/controle-encomenda/localizaviagem/15) com o parâmetro eu consigo executar, porém, preciso que seja de forma automática para o usuário.
O meu Javascript é o que faz os POSTs.

<code>$('#form_encomendabarco').submit(function () {

 //dados encomenda aviao

    var datasaida = $('#datasaida').val();
    var estadoorigem = $('#estadoorigem').val();
 var estadodestino = $('#estadodestino').val();

    $.ajax({
        url: "http://localhost/levarte/controle-encomenda/salvarEncomendaBarco",
        type: "post",
        data: {
 datasaida : datasaida,
    estadoorigem : estadoorigem,
 estadodestino : estadodestino,

 },

      beforeSend: function () {

       alertify.message('PROCESSANDO!');

        },

        success: function (resultado) {
            if (resultado != 2) {

              alertify.message('LOCALIZANDO VIAGEM');

     ////AQUI INICIO O ENVIO DO RESULTADO QUE NA VERDADE É O IDENCOMENDA
                $.ajax({
                    type: "POST",
                    url: "http://localhost/levarte/controle-encomenda/localizaViagemBarco",
                    data: {
                        resultado: resultado,
                    },

                    beforeSend: function () {
                     alertify.message('LOCALIZANDO VIAGEM!');
                     alertify.message(resultado);
                    },

                    success: function (data) {

      //AQUI DEVERIA CARREGAR A TELA COM O RESULTADO
      $('#content').html(resultado);
        alertify.message("VOLTOU");

        }
    });</code>

O cadastro ocorre, mas nada depois disso.
Comment: Você não pode fazer o chamada da função dentro do próprio php?
Ao finalizar o cadastro e com o ID do da Encomenda, chama dentro do php a function LocalizarViagem($idencomenda) dentro do próprio php e não retornando um resultado do AJAX em seguida chamar outro AJAX.
Answer: Isso porque o resultado do primeiro ajax está influenciando na execução do segundo. Evite colocar um $.ajax dentro de outro, o melhor método para fazer isso acredito que seja algo parecido com:
<code>$('#form_encomendabarco').submit(function () {
        //dados encomenda aviao
        var datasaida = $('#datasaida').val();
        var estadoorigem = $('#estadoorigem').val();
        var estadodestino = $('#estadodestino').val();

        var finalResult = 0;

        $.ajax({
            url: "http://localhost/levarte/controle-encomenda/salvarEncomendaBarco",
            type: "post",
            data: {
                datasaida: datasaida,
                estadoorigem: estadoorigem,
                estadodestino: estadodestino,
            }, beforeSend: function () {
                alertify.message('PROCESSANDO!');
            }, success: function (resultado) {
                finalResult = resultado;
                if (resultado != 2) {
                    alertify.message('LOCALIZANDO VIAGEM');
                    func();
                }
            }
        });
    });

function func() {
    $.ajax({
        type: "POST",
        url: "http://localhost/levarte/controle-encomenda/localizaViagemBarco",
        data: {
            resultado: resultado,
        },

        beforeSend: function () {
            alertify.message('LOCALIZANDO VIAGEM!');
            alertify.message(resultado);
        },

        success: function () {

            //AQUI DEVERIA CARREGAR A TELA COM O RESULTADO
            $('#content').html(resultado);
            alertify.message("VOLTOU");

        }
    });
}
</code>
Porém, isso chama um $.ajax dentro de outro (através da função). Outra alterativa (o que eu acho melhor) é colocar o segundo ajax no mesmo nível do primeiro, algo como:
<code>$('#form_encomendabarco').submit(function () {
    //dados encomenda aviao
    var datasaida = $('#datasaida').val();
    var estadoorigem = $('#estadoorigem').val();
    var estadodestino = $('#estadodestino').val();

    var finalResult = -1;

    $.ajax({
        url: "http://localhost/levarte/controle-encomenda/salvarEncomendaBarco",
        type: "post",
        data: {
            datasaida: datasaida,
            estadoorigem: estadoorigem,
            estadodestino: estadodestino,
        }, beforeSend: function () {
            alertify.message('PROCESSANDO!');
        }, success: function (resultado) {
            finalResult = resultado;
            if (resultado != 2) {
                alertify.message('LOCALIZANDO VIAGEM');
                func();
            }
        }
    });

    if (finalResult >= 0 && finalResult != 2) {
        $.ajax({
            type: "POST",
            url: "http://localhost/levarte/controle-encomenda/localizaViagemBarco",
            data: {
                resultado: resultado,
            },

            beforeSend: function () {
                alertify.message('LOCALIZANDO VIAGEM!');
                alertify.message(resultado);
            },

            success: function () {

                //AQUI DEVERIA CARREGAR A TELA COM O RESULTADO
                $('#content').html(resultado);
                alertify.message("VOLTOU");

            }
        });
    }
});
</code>


Statistical uncertainty in average of continuous variable, where values are not expected to be the same

Question: I have been trying to understand statistical uncertainties on measurements made, I have found many partial sources, but they seem to either deal with

simply counting whether something happened or not
dealing with repeated measurements of the same quantity, therefore assuming that any variation is due only to uncertainties or statistical fluctuations.

Neither of the above is my case ... My current requirement is to take the results of some blood tests and find an average for particular blood markers. Let's say cholesterol.
I have a group of 100 people, I measure their cholesterol. I will get 100 different results with continuous values. There will be a distribution due to the fact that people can have different values rather than due only to uncertainty. I would probably assume (not sure if correctly) this would still be normally distributed.
I want to extract the average value and the uncertainty.
Questions:

What would you suggest as the best approach?
How would I need to modify this approach if I had a million people to test, or if I only had one?
Comment: Thanks Bruce, I understand this is the standard deviation. A measure of the spread around the mean. Is this a measure I can quote as the sampling or statistical "uncertainty" in the mean.
Comment: If data are normal and sample random, use the sample mean $\bar X = \frac 1 n \sum_{i=1}^n X_i$ to estimate the population mean $\mu$ and the sample standard deviation $S = \sqrt{\frac{1}{n-1}\sum_i (X_i - \bar X)^2}$ to est the pop. SD $\sigma.$ // You can find confidence intervals to assess how good these point estimates are. Look at stat text.  // A random sample of size $n = 100$ should give useful results unless the data have outliers. Precision of estimates gets better as $n$ gets larger. If $n = 1,$ you can't estimate $\sigma$ and the single observation may be relatively far from $\mu.$
Answer: For normal data a 95% confidence interval for the population mean $\mu$ is of the form
$$\bar X \pm t^*S/\sqrt{n},$$ where $t^*$ cuts 2.5% from the upper tail of Student's t dist'n with $n-1$ degrees of freedom.

For $n > 30,$ a good approximation is $t^* \approx 2.$ You can get the exact value from a printed table of t dist'n; look at row $n-1$.
For $n=100$ it's $t^* = 1.984$ obtained in R by code qt(.975, 100), which returns 1.983972.

The quantity $S/\sqrt{n}$ is called the standard error of the mean. The quantity $t^*S/\sqrt{n}$ (half-width of the confidence interval) is called 95% margin of error for estimating $\mu.$
Example:
Suppose cholesterol determinations from 100 men randomly chosen from a particular population are as generated below. (I'm no expert on cholesterol,
so these may not be realistic. However, these fake data are good enough for a demonstration of the statistical ideas.)
Computations from R:
set.seed(1234);  x = round(rnorm(100, 130, 12));  x
  [1] 116 133 143 102 135 136 123 123 123 119
 [11] 124 118 121 131 142 129 124 119 120 159
 [21] 132 124 125 136 122 113 137 118 130 119
 [31] 143 124 121 124 110 116 104 114 126 124
 [41] 147 117 120 127 118 118 117 115 124 124
 [51] 108 123 117 118 128 137 150 121 149 116
 [61] 138 161 130 122 130 151 116 146 146 134
 [71] 130 125 126 138 155 128 113 121 133 126
 [81] 128 128 114 128 140 138 137 125 128 116
 [91] 129 133 150 142 124 134 116 141 142 155

Their sample mean is $\bar X =128.13$ and their sample standard deviation is
$S = 12.04.$ The standard error is $S/\sqrt{100} = 1.204.$
mean(x);  sd(x)
[1] 128.13
[1] 12.04005

From these data, the best estimate of $\mu$ is $\bar X = 128.13.$ But that is unlikely to exactly correct. In R, the t.test procedure gives a 95% confidence interval $(125.741, 130.519)$. The relevant
part of the output is shown below:
t.test(x)

...
95 percent confidence interval:
 125.741 130.519
...

Notice that these 100 observations have smallest value 102 and largest value 161. However, using the information from all 100 values we can be reasonably sure that the population
mean $\mu$ is between 125.741 and 130.519. [Because my fake data were generated using $\mu = 130$ we know that the confidence interval contains the 'true' mean in this particular example. But in a real-life study, you would
not know the true mean.]
min(x);  max(x)
[1] 102
[1] 161

Note: Now you should try to find formulas in your textbook that match
the ones I have shown here. Then follow through any specific examples given there.
Comment: Thanks Bruce, really helpful :)


Como trabalhar com rotas no wordpress

Question: É possível trabalhar com rotas personalizadas dentro do wordpress?
Eu estou construindo um site que consome uma api onde o usuário poderá fazer buscas de produtos.
Eu gostaria de personalizar a url do site para determinas páginas da seguinte maneira meusite.com/estado/cidade/bairro/aqui_vem_o_meu_produto
essa url seria direcionada para uma página do wordpress.
att,
Answer: Você está procurando pela Rewrite API. Ela é usada pra personalizar as URLs de forma que você consiga buscar os valores dentro da sua query da forma que precisar.
Exemplo do Codex:
<code><?php
  function custom_rewrite_rule() {
    /**
     * add_rewrite_tag() cria "tags" que podem ser acessadas pelo
     * objeto de query padrão, usando get_query_var()
     */
    add_rewrite_tag('%food%', '([^&]+)');
    add_rewrite_tag('%variety%', '([^&]+)');

    /**
     * add_rewrite_rule() cria regras para traduzir as queries em URLs
     * no formato desejado.
     *
     * A regra aqui cria a URL: http://example.com/nutrition/milkshakes/strawberry/
     * transformada em food=milkshakes e variety=strawberry
     */
    add_rewrite_rule('^nutrition/([^/]*)/([^/]*)/?','index.php?page_id=12&food=$matches[1]&variety=$matches[2]','top');
  }
  add_action('init', 'custom_rewrite_rule', 10, 0);
?>
</code>
Links:
add_rewrite_rule()
add_rewrite_tag()
Comment: Obrigado, funcionou perfeitamente.

Como trabalhar com rotas no wordpress


How to search in v-data-table by values from merged columns?

Question: I have this v-data-table:
<v-data-table
   v-model="selectedAssets"
   :headers="headers"
   :items="assetList"
   :items-per-page="10"
   :search="search"
   :custom-filter="filterWithBarcodes"
   :dense="isDense"
   item-key="id"
   multi-sort
   show-select>

with 3 headers:
headers:[
            { text: 'Asset id', align: 'start', sortable: true, value: 'id'},
            { text: 'Name', sortable: true, value: 'name'},
            { text: 'Type', sortable: true, value: 'assetType'},
            etc...
        ],

How I can search in this table by merging values from two columns? I have to search by "barcode" which is "assetType-id" like. LAP-130. Searching works only for single column, when I type LAP or by id, but when I type LAP-130 it finds nothing.
Maybe something with custom-filter property?
Comment: Can you post assetList, or at least a sample of it?
Answer: I believe you can also supply a custom filter for each column:
      headers: [
        { text: "Asset id", align: "start", sortable: true, value: "id" },
        { text: "Name", sortable: true, value: "name" },
        {
          text: "Type",
          sortable: true,
          value: "assetType",
          filter: (value , search, item) => {
            if (!search) return true;
            return `${item.assetType}-${item.id}`.includes(search.toLowerCase())
          },
        },
      ],

Please note that this function will always be run even if search prop has not been provided. Thus you need to make sure to exit early with a value of true if filter should not be applied.
Comment: Agreed, idk why tho


Tentando imprimir a soma total de todos os valores dentro da lista?

Question: Estou tentando imprimir a soma total de todos os valores dentro da lista, porém o que meu código faz é somar o primeiro elemento com o primeiro elemento e dar o resultado, depois o segundo elemento com o segundo elemento e assim por diante. Segue código:
const valor = [1, 2, 3, 4, 5];

for (let i in valor){
    i++;
    let soma = i + i;
    console.log(`i = ${soma}`);
}

Comment: me desculpem por fazer um tópico tão mal feito, é a primeira vez que publico aqui.
Answer: Na sua pergunta o seu código tem bastante problemas um deles é colocar a soma para imprimir dentro do for, nesse momento a variável soma só deveria receber a soma do próximo elemento com a soma e fora desse for mostrar, outro problema é que usou o for errado, nesse caso deveria ser o for..of que realmente pega o valor de cada posição.
Alguns exemplos básicos:

Para somar elementos de um array em Javascript:
Utilizando for

const valor = [1,2,3,4,5];
let soma = 0;
for (let i = 0; i < valor.length; i++){
    soma = valor[i] + soma;
}

document.getElementById("app").innerHTML = soma;

Utilizando for..of:

const valor = [1,2,3,4,5];
let soma = 0;
for (let i of valor){
    soma = soma + i;
}

document.getElementById("app").innerHTML = soma;

Utilizando reduce:

const valor = [1,2,3,4,5];

const soma = valor.reduce((a,b) => a + b, 0);

document.getElementById("app").innerHTML = soma;

Comment: muito obrigado, você ajudou demais!
Answer: Há muitos problemas no seu código e um deles, é que você utiliza o operador in ao invés do operador of. O in é um operador que irá percorrer os atributos de um objeto e não seu valores.
const objeto = {nome: "lucas", idade: 32};

for (let chave in objeto) {
    console.log(chave);         // Saída: nome; idade
}

Por outro lado, o for..of irá percorrer cada elemento do seu Array. Veja abaixo o que você na verdade estava percorrendo com seu código:

const valores = [1, 2, 3, 4, 5];

for (let i in valores){
    console.log(i);
}

Um outro problema no seu código, é que você não estava incrementando a variável soma. A cada loop que o laço for fazia, você apenas atribuía um valor de i + i, ou seja, o que acontecia era isso:
soma = 1 + 1;
soma = 2 + 2;
soma = 3 + 3;
// ...

Para corrigir o seu código, você deve utilizar o operador of para percorrer os elementos do Array e depois você deve incrementar à variável soma o valor de i, dessa foma:

const valores = [1, 2, 3, 4, 5];
var soma = 0;

for (let i of valores){
    soma += i; // O operador "+=" equivale a fazer "soma = soma + 1"
}

console.log(`Soma de ${valores.join(" + ")} = ` + soma);

Além de utilizar o laço for, você pode utilizar o método reduce para percorrer os elementos do Array e realizar alguma ação com eles utilizando um dado auxiliar que será modificado ao longo do processo e por fim retornado.
Ficou um pouco difícil a explicação ? Então veja o método funcionando na prática.

const valores = [1, 2, 3, 4, 5];

// O número 0 na chamada do método é o valor inicial que será manipulado.

var resultado = valores.reduce(function(valor, valores_somados){
    return valores_somados + valor;
}, 0);

console.log(resultado);

// É possível também reduzir o código acima utilizando arrow function.

var resultado = valores.reduce((valor, soma) => valor + soma, 0);
console.log(resultado);
Comment: muito obrigado, ajudou muito!


Query-By-Committee with abstention

Question: I've some difficulties understanding how abstention works in Active Learning. A teacher asked me to implement the active learning algorithm Query-by-Committee which helps a committee to ask the better points to the oracle. I've understand how the algorithm works but he also asked me to implement QBC where the oracle has the possibility to abstain from giving labels of some inputs.
Actually, I read things about abstention in active learning but I'm unable to put things together. Besides, I find a contradiction with my teacher's tips (that's certainly because I don't understand how abstention works).
For me, QBC finds the most uncertain point for the committee (with vote entropy or average KL divergence for instance) and asks their label to the oracle. But my teacher tells me that, in QBC with abstention, the oracle abstains on the most uncertain points… But that's precisely the labels we want to…
Does somebody know the high-level idea behind QBC with abstention?
Comment: Actually there isn't… I think, anyway nothing on internet… here are the instructions my teacher gave me https://www.icloud.com/iclouddrive/0eagSBgcZbSre6ZEOsNtk6fYg#Active_Learning_with_abstention He asks to implement classic QBC and then QBC with abstention (last paragraph). This is the only thing I have about it…
Comment: There are good reasons to not label datapoints corresponding to high uncertainties. But this is not referred to as "QBC with abstention". I have not heard of this terminology. Can you share some reference on this?
Answer: There are good arguments to sample points corresponding to high uncertainties and good arguments not to.
You could think of these points as points lying in not so dense regions in the features' space; since these points are sparse in your training data, each member of the committee (i.e., the set of classifiers/regressors) will predict their labels rather arbitrarily. Hence, there will be a disagreement between them on the labels of these points. Adding these points to your training dataset will help improve the performance of your final machine learning model in poorly-populated regions of the features' space. This is an argument in favour of sampling points corresponding to high uncertainties.
However, data points lying in low-populated regions of the features' space can be considered outliers. And in application, you don't care about the behaviour of your machine learning (ML) model on outliers. You care about its behaviour in densely populated regions of the features space (assuming that your ML model in real life will need to make predictions on data points following the same distribution of the data points in your pool. More on this can be found here). This is an argument against sampling points corresponding to high uncertainties.
Your question touches on the heart of active learning research; how to build an optimal active learning algorithm/query strategy. Recent research indicates that QBC-like algorithms are not the best we can do. More optimal strategies can be achieved by sampling points corresponding to high uncertainties while not deviating much from the true distribution of data points in the pool, i.e., while not sampling too many outliers. The following paper provides a more formal discussion.
Comment: Thanks for you answer  So the QBC with abstention would be : ask the oracle the most uncertain points but not too uncertain ?
Comment: Yes. This is what the document asks to implement.

Query-By-Committee with abstention


Intuition behind strong vs weak laws of large numbers (with an R simulation)

Question: This rather looks quite basic, but when referring to weak and strong law of large numbers this is the definition I look at (Casella and Berger)

Can you please give an 'intuition' in understanding the difference between them.
Also, what does the limits inside the probability signify for the strong law?
Can you give me a simulation in R to signify the difference between them?
Comment: The difference is the difference between convergence in probability and convergence almost surely. For convergence in probability, the random variable can take values far from the limit infinitely many times, while for convergence almost surely, this is impossible -- after a certain $n$, with probability $1$ the random variables will be and _remain_ arbitrarily close to the limit. Thus in some sense converge almost surely better resembles our intuition for deterministic limits.
Comment: Your "weak" law is really convergence in probability, unless you're referring to the weak law of large numbers, where $Y$ is constant.
Answer: It might be clearer to state the weak law as $$\overline{Y}_n\ \xrightarrow{P}\ \mu \,\textrm{ when }\ n \to \infty , \text{ i.e. } \forall \varepsilon \gt 0: \lim_{n\to\infty}\Pr\!\left(\,|\overline{Y}_n-\mu| \lt \varepsilon\,\right) = 1$$
and the strong law as
$$\overline{Y}_n\ \xrightarrow{a.s.}\ \mu \,\textrm{ when }\ n \to \infty , \text{ i.e. } \Pr\!\left( \lim_{n\to\infty}\overline{Y}_n = \mu \right) = 1$$
You might think of the weak law as saying that the sample average is usually close to the mean when the sample size is big, and the strong law as saying the sample average almost certainly converges to the mean as the sample size grows.
The difference happens when failures of the sample average to be close to the mean are big enough to prevent convergence.
As an illustration using R, take Wikipedia's first example, with $X$ being exponentially distributed random variable with parameter $1$ and $Y= \dfrac{\sin(x) e^x}{x}$ so $E[Y]=\frac{\pi}{2}$.  Let's consider $100$ cases where the sample size is $10000$:
set.seed(1)
cases <- 100
samplesize <- 10000
Xmat <- matrix(rexp(samplesize*cases, rate=1), ncol=samplesize)
Ymat <- sin(Xmat) * exp(Xmat) / Xmat
plot(samplemeans <- rowMeans(Ymat),
    main="most sample averages close to expectation")
abline(h=pi/2, col="red")

but now look at the failure of the running sample average over the same $1$ million observations to get to the mean and stay there
plot(cumsum(Ymat)/(1:(samplesize*cases)),
    main="running sample average not always converging to expectation")
abline(h=pi/2, col="red")

Comment: so this was an illustration of convergence in probability? My doubt is, how does the definition that interchanges the limit and probability in the mathematical expression of weak and large law reflect this difference in concept?
Comment: @Nisha $ \forall \varepsilon \gt 0: \lim\limits_{n\to\infty}\Pr\!\left(\,|\overline{Y}_n-\mu| \lt \varepsilon\,\right) = 1$ is the definition of convergence in probability to $\mu$ while  $\Pr\!\left( \lim\limits_{n\to\infty}\overline{Y}_n = \mu \right) = 1$ is almost sure convergence to $\mu$.  The latter implies the former (Fatou's lemma) but this and other examples show that the former does not imply the latter


Capturar "textarea" do plugin Responsive WYSIWYG (editor de texto)

Question: Estou com o seguinte código:
<!doctype html>

<meta charset="utf-8">
<title>Untitled Document</title>

<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<link href="http://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">

<link href="editor.css" type="text/css" rel="stylesheet"/>

Minha pergunta é como posso capturar as informações que são passadas pelo editor de texto na "DIV = txtEditor" para um textarea para que o PHP possa capturar os dados e enviar pelo metodo post para o banco de dados?
Comment: atualmente eu estou usando o editor [TinyMCE](http://www.tinymce.com/). Ele é usado por grandes empresas e totalmente configuravel, sem contar que de fácil implementação, basta você criar um  realizando o bind com o seu modelo e inicializar o plugin ``
Comment: João. Como funciona exatamente esse bind, com esse plugin Responsive WYSIWYG (editor de texto)r?
Comment: Bem, sou desenvolvedor Java, mas o principio é o mesmo. você tem o atributo name no qual você realiza a ligação  para popular ou gravar as informações
Answer: No site TinyMCE tem este exemplo; se você puder mudar o DIV para um TextArea, poderá fazer desta forma:
<?php
 $allowedTags='

';
 $allowedTags.='

';
// Should use some proper HTML filtering here.
  if($_POST['elm1']!='') {
    $sHeader = '
Ah, content is king.
';
    $sContent = strip_tags(stripslashes($_POST['elm1']),$allowedTags);
} else {
    $sHeader = '
Nothing submitted yet
';
    $sContent = '
Start typing...
';
    $sContent.= '
This rover has crossed over
';
  }
?>

<title>My test editor - with tinyMCE and PHP</title>

 <?php echo $sHeader;?>

Sample using TinyMCE and PHP

 <form method="post" action="<?=$_SERVER['REQUEST_URI']?>">
  <textarea id="elm1" name="elm1" rows="15" cols="80"><?php echo $sContent;?></textarea>

<input type="submit" name="save" value="Submit" />
<input type="reset" name="reset" value="Reset" />
</form>

Fonte: http://www.tinymce.com/wiki.php/TinyMCE3x:How-to_implement_TinyMCE_in_PHP
Comment: Tony, poderia me passar seu skype para me auxiliar de uma maneira mais integra? Estou com alguns problemas ainda... Abraços
Answer: Para pegar o valor do texto, vc usa a seguinte linha, conforme no link a seguir:
https://github.com/suyati/line-control/blob/master/README.md
$('#txtEditor').Editor('getText');

eu comecei mês passado a usar esse editor de texto, e pra mim foi o melhor WYSIWYG gratuito que achei :)
Answer: Voçê pode capturar o conteúdo que esta no ID "texteditor" com jQuery desde que tenha conteúdo nele...exemplo:
//Verifica se há conteúdo!
if($('#texteditor').html() != ""){

  //Cria uma variavel para pegar o conteúdo!
  var content = $('#texteditor').html();

    //Inicia o envio!
    $.ajax({
        url: 'sua_pagina.php',
        type: 'POST',
        data: {
        content: content
        },
        success: function(data){
            if(data){

                //Em caso de sucesso, faça alguma coisa aqui!

            }else{

                //Em caso de erro, faça algo aqui!
            }
        }
    });

}else{//Caso a tentativa de envio esteja sem conteúdo!

     //Adicione alguma ação aqui!

}

Já para uma página PHP deixo só uma idéia, voçê trabalha ela como bem achar melhor..lembrando que retorno será true ou false representados por 0 ou 1 !!!
<?php

$content = $_POST['content']; //peda os dados do POST
$content = mysql_real_escape_string($content);  //escapa a string

$sql = true; // aqui você pode colocar sua SQL para salvar no banco

if ($sql)
{
    echo 1;
}else{
    echo 0;
}


Horowitz' improvised run during performances of fantaisie-impromptu

Question: I'm hoping this is the best place to ask how to play the decorated run that Horowitz improvises towards the end of the second section in his performance of Chopin's fantaisie impromptu? I can't find any transcriptions of it on the Internet but if anyone can transcribe by ear or has a script already, I would be very grateful! Below is an image of the standard notation as well as a link to Horowitz' interpretation (the phrase in question is played at 2.35 on the video)
Answer: This looks like it:

Note that the bass notes for the last beat are copied from the first two editions I looked at (Paderewski and Fontana); the treble notes are also written as straight quavers, though no doubt many people play them dotted.
Comment: "though no doubt many people play them dotted." Why? If Chopin had wanted to write this section in 12/8 time, he would have done that. The whole piece is built on cross-rhythms, for example 4 against 3 in the fast sections. The first edition gives two 8th-notes on the last beat, though some later editors (who obviously knew how to compose better that Chopin!!!) - not to mention some performers - are notorious for messing around with the score to suit their own whims and fancies. Also Chopin wrote the third beat as Horowitz played it, but grace notes- only the first two beats are by Horowitz.


Orthocenter of triangle $DEF$ is same as the circumcenter of triangle $ABC$

Question: $D,E,F$ are mid points of the sides of the triangle $ABC$,then prove that the orthocenter of triangle DEF is same as the circumcenter of triangle ABC.
I cannnot figure out what coordinates to suppose for A,B,C.I tried taking $(x_1,y_1),(x_2,y_2),(x_3,y_3)$ but calculations go messy and clumsy.Can someone help me in proving this question?
Comment: What are you allowed to assume in your proof? Can you use the fact that the circumcenter is at the intersection of the perpendicular bisectors of the sides? Can you use the fact that the line through the midpoints of two sides of a triangle is parallel to the third side? Those two facts combined make the proof easy: just use a diagram and look at those perpendicular bisectors of $\triangle ABC$ and how they interact with the vertices and sides of $\triangle DEF$.
Comment: @Théophile,Oh sir,thanks for notifying,that was a typo!
Answer: Algebra isn't necessary. Instead, use geometrical definitions: in particular, the circumcentre $O$ of $ABC$ is at the intersection of the perpendicular bisectors of $AB$, $AC$, and $BC$. Do you see how to continue from there?
Comment: @Brahmagupta Incidentally, if you did want to approach this algebraically, I would recommend assuming without loss of generality that one point of $ABC$ is at the origin and a second point is on the $x$-axis: $A = (0,0), B = (x_2,0), C = (x_3,y_3)$. That would simplify the calculations. But algebra should really be a last resort in questions like this; not only can the algebraic approach be cumbersome, but you also miss out on what's fundamentally happening in terms of the geometry. As a rule of thumb, start by drawing a diagram and then reflect on the definitions.
Comment: Does that mean that perpendicular bisector of BC will be perpendicular to EF,perpendicular bisector of AC will be perpendicular to DF,perpendicular bisector of AB will be perpendicular to DE.
Comment: @Brahmagupta Yes, exactly. The perpendicular bisector of $BC$ will be perpendicular to $EF$, and furthermore passes through $D$ (because $D$ is by definition the midpoint of $BC$). In other words, the perpendicular bisector of $BC$ coincides with the altitude of $D$ in $DEF$. (And the same holds, of course, for the other sides.)
Answer: Let's consider a $\triangle ABC$ with side BC coinciding with the x-axis such that vertex B is at origin $(0, 0)$ & vertex C is at $(a, 0)$ then vertex A will be at $\left(\frac{a\tan C}{\tan B+\tan C}, \frac{a\tan B\tan C}{\tan B+\tan C}\right)$. The mid-points $D, E$ & $F$ of the sides $BC$, $AC$ & $AB$ respectively of $\triangle ABC$ are calculated as follows $$D\equiv\left(\frac{0+a}{2}, \frac{0+0}{2}\right)\equiv\left(\frac{a}{2}, 0\right)$$
$$E\equiv\left(\frac{\frac{a\tan C}{\tan B+\tan C}+a}{2}, \frac{\frac{a\tan B\tan C}{\tan B+\tan C}+0}{2}\right)\equiv\left(\frac{a(\tan B+2\tan C)}{2(\tan B+\tan C)},\frac{a\tan B\tan C}{2(\tan B+\tan C)}\right)$$
$$F\equiv\left(\frac{\frac{a\tan C}{\tan B+\tan C}+0}{2}, \frac{\frac{a\tan B\tan C}{\tan B+\tan C}+0}{2}\right)\equiv\left(\frac{a\tan C}{2(\tan B+\tan C)},\frac{a\tan B\tan C}{2(\tan B+\tan C)}\right)$$  Hence, the ortho-center $H$ of $\triangle DEF$ is determined as follows
Now, the orthocenter H can be calculated by finding intersection point of any two altitude equations. We can get orthocenter as  $$H\equiv\left(\frac{a}{2}, \frac{a(\tan B\tan C-1)}{2(\tan B+\tan C)} \right)$$
& the circumscribed center say $P$ can be calculated as $$P\equiv\left(\frac{a}{2}, \frac{a(\tan B\tan C-1)}{2(\tan B+\tan C)} \right)$$ Hence, we can find that orthocenter (H) of $\triangle ABC$ will coincide with the circumcenter (P) of $\triangle ABC$ i.e. both H & P are same.
Answer: The circumcenter of the triangle $ABC$ is the intersection of the perpendicular bisectors of its sides, i.e. perpendiculars through $D, E, F$. Now, if you join the middle points of the sides you obtain a similar triangle to the original one with its sides parallel to those of the original.
The orthocenter of $DEF$ is the intersection of its altitudes. From the picture you can see that the altitudes of $DEF$ coincide with the perpendicular bisectors of the sides of $ABC$


Loop de DIV's em PHP

Question: Estou gerando um loop de uma div de tamanho fixo e gostaria que na hora da impressão cada uma fique em uma página.
Porém a primeira Div do Loop fica com uma margem-top impedindo que isso aconteça !
Existe alguma maneira que faça essa primeira div ser dirente das demais nessa questão da margin ?
Segue Código para melhor entendimento
Grato :)

<?php for($i=0;$i<=4;$i++):
        if($i == 1) $cor = "blue";
        if($i == 2) $cor = "black";
        if($i == 3) $cor = "yellow";
        if($i == 4) $cor = "green";
    ?>
    <div class="papel" id="papel" style="margin-top:calc(210mm * <?php echo $i?>)!important;background-color:<?php echo $cor ?>">

<?php endfor;?>

<input type="button" id="imprimir" onclick="imprimir()" value="Imprimir">

Comment: Você quer div para cima? não saia inserindo por baixo é isso?
Comment: @KingRider  Ao clicar no botão imprimir, é possível notar que a ultima pagina esta em branco. Gostaria de remove-la. Acredito que seja por alguma gafe no loop. Editei a questão para melhor compreensão. Obrigado !
Answer: Bom, se já esta no loop, basta verificar qual é a primeira posição com um IF:
<?php for($i=1;$i<=5;$i++): ?>

  <?php if($i == 1): ?>
    // Faz o que precisa na primeira posição
    <div class="papel" id="papel" style="margin-top:calc(105mm * <?php echo $i?>)!important;">

  <?php endif; ?>

    // Demais posições
    <div class="papel" id="papel" style="margin-top:calc(105mm * <?php echo $i?>)!important;">

<?php endfor; ?>

Não alterei a div pois não sei o que quer fazer nelas, mas basta montar as divs e deixar neste formato o loop.
Espero que ajude, qualquer dúvida pergunte.
Att;
Comment: Obrigado pelo apoio ! Consegui realizar o que queria, alterei o codigo para melhor compreensão... Seria possivel tirar esses espaços brancos antes da impressão?  Pois na impressão eles não aparecem =s
Comment: Espaço em branco?
Comment: Caso a tenha respondido sua pergunta marque como resposta
Answer: Se o primeiro elemento não tiver problema com margin-top igual a 0 (zero), inicie o incrementador $i com ele:
<?php for($i=0; $i<=5; $i++): ?>
    <div class="papel" id="papel" style="margin-top:calc(105mm * <?php echo $i?>)!important;">

<?php endfor; ?>

Dica: Ao utilizar blocos estruturais com PHP mesclado ao HTML, há uma sintaxe alternativa, como usado acima no for, que facilita a leitura do código.
Comment: não entendi, você abriu um bloco `for` e fechou com `foreach`?
Comment: Editei e corrigi para `endfor`.
Answer: Basta usar o seletor :first-child do css. Veja o exemplo abaixo:

.minha_div{
   background-color:red;
   color:white;
   display:block;
   margin-top:5px;
   padding:2px;
   text-align:center;
}

.minha_div:first-child{
   background-color:blue;
}

   DIV 1

   DIV 2

   DIV 3

   DIV 4

Loop de DIV's em PHP


"Cannot find module" when running unit tests on node.js with react as peer dependency in common package

Question: I have following app structure:

Application A
Application B
Common package

Now Application A and B have in package.json the common package added:
{
  dependencies: {
      "commonPackage": "file:../../../commonPackage"
  }
}

both apps use React, as well as the common package, all had React added with npm, and it worked, before we started to use react hooks.
Because when we started, we got an Invalid Hook Call Warning due to having "more than one copy of React", so to avoid that, in the common package, the react dependency was moved to peerDependencies so that the react instance from the app is used and not from the package.
It works great in the browser when we run both apps A and B, but when I run my mocha tests in the console, I get:
ERROR in ../commonPackage/~/@uifabric/utilities/lib/customizations/Customizer.js
   Module not found: Error: Can't resolve 'react' in 'D:\myProject\commonPackage\node_modules\@uifabric\utilities\lib\customizations'

this is from the office-ui-fabric-react package we use, but it seems like a more general issue with dependency resolution.
Project is in TypeScript, we use webpack for the compilation of the app for the browser, and tsc to compile for the unit tests.
I found some answers, suggesting to npm link react in the common package to the react package in the application node_modules, but it seems wrong, since the common package is used by two applications, it would solve the issue only for one.
Comment: If your `mocha` tests are in `commonPackage` and you need React for test purposes in common, you could it as `devDependency`.
Answer: In the case above we finally came to a solution which was

adding back react as devDependency to Common package
using esm package to help our test runs understand es6 module export/import that came with fabric package. Just using mocha --require esm ...
ejecting and adding alias to webpack.config.js in Application
    alias: {
        'react': path.resolve('./node_modules/react')
    }

Application A, B and hooks work now.
Comment: Yea! This worked for me.  I was only using fabric (so far) in my applications, not the common package, so I didn't need the `--require esm` part, but this last step, adding the `alias` to the `resolve` section of my `webpack.config.js` is what got me working again.
Comment: For me this one was the solution:

https://stackoverflow.com/questions/64365016/jest-cannot-find-module-react-and-other-node-modules


How to use regex in python to catch patterns like keyword1 [arbitrary characters with a max length of 5] keyword2

Question: How can I use re package to recognize a pattern as
keyword1 [arbitrary characters with a max length of 5] keyword2

For example, keyword1 abc keyword2 is valid, keyword1 a . keyword2 is also valid
but keyword1 1234abd keyword2 is not valid
Thank you
Comment: Are the spaces after `keyword1` and before `keyword2` included in the 5 character limit? e.g. is `keyword1 abcd keyword2` valid?
Answer: r"(?:keyword1).{5}(?:keyword2)

Checked this on https://regex101.com/


differential family of straigh line

Question: I have a problem with exercice:
Find a differential equation whose solution is a family of straight line whose y intercept is a function of its slope.
My concepts are:
$$y = ax + b$$
$$b = f(a)$$
I'm trying something to do with tangent function as a slope of straight line.
The correct answer of the exercise is:
$$y = xy^{'} + f(y')$$
Could someone help me with solwing this problem?
I'll be very grateful
Best regards
Answer: Seems like they've started with
$$y = ax + f(a)$$
and then simply replaced $a = y'$ to make
$$y = y'x + f(y')$$
Conceptually at least it makes sense that this is consistent even though I'd have to check that there weren't additional solutions besides the straight line ones.

differential family of straigh line


RS485 communication over TCP/IP

Question: I have a device which has a rs485 output and another gsm device connected to this output.
I can access gsm device with a special IP and port. Gsm device is working transparently. Sending all data recieved from tcp socket to rs485, and retrieving data on rs485 then write to the opened tcp socket.
connection between gsm device and rs485 existing device is wired and fix baudrate on 300.
when I tried to read rs485 existing device with my laptop, connect rs485 to my usb, setting serial parameters like;

parity : even
databit : 7
stopbit : 1
baudrate : 300

(This configuration is universal and described with a standard documentation)
With this configuration everything is right and I can transfer data.
When I tried to use gsm device, I have to manipulate the generated byte array to fit the rs485 exsiting device.
for instance;
I want to send /?![CR][LF] as UTF-8.
When using wired connection to device basically sending
"/?!\r\n".getBytes("UTF-8") and device answering.
When using gsm, I monitor over wireshark, device is only answering with byte array : AF-3F-21-8D-A0
AF is signed format of "/" - 2F
Also 8D is signed format of [CR] - 0D
I thinking that, the difference with bytes is causing from my configuration that used on serial connection.
Is there a way to set this configurations (parity, stop, data bits ) when using tcp/ip sockets ?
Thanks for your reply.
Answer: There is a way to set RS232/485 serial COM parameters for these kinds of devices via TCP. The standard for it is
RFC 2217 Telnet Com Port Control Option
but not all manufacturers of "Serial COM via TCP/IP" or similar devices support it.
You need to check your GSM device if it supports different operation modes. E.g. a "raw mode" (which is probably what is used now), and a "managed mode" like RFC 2217 or something proprietary that allows control on serial line parameters.
For example both the MOXA and Lantronix brands support RFC 2217 based operation mode.

RS485 communication over TCP/IP


Python find all two letter pairs in a word string

Question: Let's say I've got a list of words:
word_list = ['WELCOME', 'TO', 'MY', 'B', 'DAY']

So...
for x in word_list:
    #Do something

How do I make it so that the outcome is a list of all of the possible two letter combinations in each word?

WELCOME: WE, EL, LC, CO, OM, ME
TO: TO
MY: MY
B:
DAY: DA, AY
list = ['WE', 'EL', 'LC', 'CO', 'OM', 'ME', 'TO', 'MY', 'DA', 'AY']

Answer: You can use zip:
>>> s='WELCOME'
>>> list(zip(s, s[1:]))
[('W', 'E'), ('E', 'L'), ('L', 'C'), ('C', 'O'), ('O', 'M'), ('M', 'E')]
>>> [''.join(t) for t in zip(s, s[1:])]
['WE', 'EL', 'LC', 'CO', 'OM', 'ME']
>>> word_list = ['WELCOME', 'TO', 'MY', 'B', 'DAY']
>>> for word in word_list:
...    print(word, [''.join(t) for t in zip(word, word[1:])])
...
WELCOME ['WE', 'EL', 'LC', 'CO', 'OM', 'ME']
TO ['TO']
MY ['MY']
B []
DAY ['DA', 'AY']

And to get your desired list:
>>> result=[]
>>> for word in word_list:
...    result.extend([''.join(t) for t in zip(word, word[1:])])
...
>>> result
['WE', 'EL', 'LC', 'CO', 'OM', 'ME', 'TO', 'MY', 'DA', 'AY']

Answer: Since you can slice strings, you can slide each word into all its two-character chuncks (i:i:2) in a for-loop, and then check that the slice has a length of two (to get rid of the 1-char string at the end, or if the word has a length of 1):
doublet_list = []
for word in word_list:
    for i,j in enumerate(word):
        doublet = word[i:i+2]
        if len(doublet) == 2:
            doublet_list.append(doublet)
print doublet_list
# ['WE', 'EL', 'LC', 'CO', 'OM', 'ME', 'TO', 'MY', 'DA', 'AY']

Answer: So you want an iterator that return 2 consecutive objects at a time, and step until the 2nd iterator reach the end. You could set up an iterator iter1 to the beginning of your iterable, clone it to iter2 and step iter2 once. Then zip both.
Let's say:
from itertools import tee, izip
def pairwise(iterable):
    iter1, iter2 = tee(iterable)
    next(iter2, None)
    return izip(*[iter1, iter2])

you'll have:
word_list = ['WELCOME', 'TO', 'MY', 'B', 'DAY']
for word in word_list:
    print [''.join(t) for t in pairwise(word)]


how would you query this in linq?

Question: lets say i have 2 tables: products (just product ID and name) and sales (sale ID, product ID, amount, date)
now, given a start date and end date, i want to sum for every product its total sales amount in the given time frame
notice that naturally some products will just have zero sales
how should i write this query?
Answer: <code>var products =
    from p in mycontext.Products
    select new
    {
       Product = p,
       Sales = p.Sales
          .Where(s=>s.StartDate > startDate && s.EndDate < endDate)
          .Sum(s=>s.amount)
    }
</code>

how would you query this in linq?


add an event listener to an element with javascript

Question: Through a script tag I've inserted on an external site, I am trying to load in some javascript which iframes a widget I am hosting on a webpage,
I only want the small launcher icon iFramed initially and then when its opened, iFrame the entire chat window when it's expanded. As Iframing the whole thing takes up a lot of the external site and means everything behind is isnt reachable!
My thought was to have a small iframe initialy and then when it's clicked, increase it's size to the entire window and then while doing so, add an element in the area where the launcher is to then close it when pressed and reduce the iframe size again! hacky I know but i dont know how else I can do this?
What you can see is me creating an iframe, and trying to give it an id
of 'ifrm' with the line:
the code so far:          ifrm.setAttribute("id", "ifrm");
 .  AND then try to change the iframes CSS or append a new one? BUT this doesnt call the function when clicked so i may
have the setting of the ID / calling it wrong?
Then how would I append an element? sorry ive probably gone the wrong way about this.
prepareFrame();

function prepareFrame() {
    console.log("yes this consoles inside of prepareFrame")
    var ifrm = document.createElement("iframe");
    ifrm.setAttribute("src", "https://5efae1b1.ngrok.io");
    ifrm.style.width = "100px";
    ifrm.style.height = "100px";
    ifrm.style.position="fixed";
    ifrm.style.right="0";
    ifrm.style.bottom="0";
    ifrm.style.border="0";
    ifrm.setAttribute("id", "ifrm");

    document.body.appendChild(ifrm);

    document.getElementById("ifrm").addEventListener("click", function(){click1(1);}, false);

    }

function click1() {
    alert("calling");
    document.getElementById("ifrm").style.backgroundColor = '#ff0000';
    colorcheck = document.getElementById("ifrm").style.backgroundColor;
    console.log('colour check' + colorcheck);
  };

Thanks so much if you can help!
Comment: First off, no need to wait until appended to set its id, do that before and you avoid a second lookup, e.g. `ifrm.setAttribute("id", "ifrm")` or `ifrm.id = 'ifrm'`. Then, when it comes to click event on an iframe, check this post: https://stackoverflow.com/questions/6452502/adding-click-event-handler-to-iframe
Comment: I did change it around before I saw your comment to this:   ifrm.setAttribute("id", "ifrm");

    document.body.appendChild(ifrm);
Comment: Then update your code sample to avoid further comments about that.
Comment: And the function was still not called, i'm finding it extremely difficult to iFrame something small and then for the iFrame to be a larger size to fit the content thats increased in size! maybe theres something im missing
Comment: An iframe doesn't catch an on click event as other html elements does...read my above link
Comment: And here is another that might be useful: https://stackoverflow.com/questions/9393532/cross-domain-iframe-issue
Comment: Thanks for all of the help! I need all of it I can get!
Answer: Instead of doing this:
document.getElementsByTagName("iframe")[0].setAttribute("id", "ifrm");

Try this at the beginning of your iframe creation:
var ifrm = document.createElement("iframe");
ifrm.setAttribute('id', 'ifrm'); // assign an id

Comment: The question ask about adding event listener, not set an id.
Comment: Sure but if your event listener is not working maybe it is because you did not set it right and as pointed you do not need to do all that to set an id to it.
Comment: If you check the OP's edit and their/mine comments, you'll see they already made that change, making this answer of no value. I recommend you delete it.
Comment: Yep, but it was not visible when i was answering. Just trying to help anyway. Maybe you think it was set but did not realize.
Answer: add ifrm.contentDocument.addEventListener("click", function(e){click1(e)}, false) to inside the prepareFrame function, this will then call the click1 function.


Rounding up one column in pandas dataframe

Question: I have a pandas dataframe df that looks like this:
          no_obs  price_cleaning  house_size
0         1             585          30
1         1             585          40
2         1             585          43
3         1             650          43
4         1             633          44
5         1             650          45
6         2             585          50
7         1             633          50
8         1             650          50
9         2             750          50

I want to round up the values in the price_cleaning column with this function:
def roundup(x):
    return int(math.ceil(x / 10.0)) * 10
I have tried the solution from this answer (Applying function to Pandas dataframe by column):
cols = [col for col in  df.columns if col != 'price_cleaning']
df[cols] = df[cols].apply(roundup)
I get the following error:
TypeError: ("cannot convert the series to ", 'occurred at index no_obs')
Can anyone help me understand why this is not working? How do I apply the roundup function to the column? Any help is much appreciated.
Answer: I'd vectorize like
In [298]: df['p'] = (np.ceil(df.price_cleaning / 10) * 10).astype(int)

In [299]: df
Out[299]:
   no_obs  price_cleaning  house_size    p
0       1             585          30  590
1       1             585          40  590
2       1             585          43  590
3       1             650          43  650
4       1             633          44  640
5       1             650          45  650
6       2             585          50  590
7       1             633          50  640
8       1             650          50  650
9       2             750          50  750

For 10K rows, timings - vectorized method is ~15x times faster to apply
In [331]: %timeit (np.ceil(dff.price_cleaning / 10) * 10).astype(int)
1000 loops, best of 3: 436 µs per loop

In [332]: %timeit dff['price_cleaning'].apply(roundup)
100 loops, best of 3: 7.86 ms per loop

In [333]: dff.shape
Out[333]: (10000, 4)

Atleast in this case, performance gap, with more rows, will increase.
Comment: great answer to use use np.ceil, very useful, thank you
Answer: You are filtering the columns upside down, do this instead:
cols = [col for col in  df.columns if col == 'price_cleaning']

Now, if you need to cleanup only one columns, then no need to create cols. Just do:
df['price_cleaning'] = df['price_cleaning'].apply(roundup)

Comment: `apply` methods are great. However, based on your use-case, size of data, you may to benchmark with vectorized methods.
Comment: @JohnGalt I think OP is far away from your accurate remark at this point
Comment: True, that's just for posterity's sake :)
Answer: This might work:
>>> df['price_cleaning_ceiling']= df.price_cleaning.apply(lambda x: int(math.ceil(x / 10.0)) * 10)

Answer: I think you can use apply and lambda as:
In [6]: df['p'] = df['price_cleaning'].apply(lambda x: int(math.ceil(x / 10.0)) * 10)

In [7]: df
Out[7]:
   no_obs  price_cleaning  house_size    p
0       1             585          30  590
1       1             585          40  590
2       1             585          43  590
3       1             650          43  650
4       1             633          44  640
5       1             650          45  650
6       2             585          50  590
7       1             633          50  640
8       1             650          50  650
9       2             750          50  750


Environment.userName is Unknown in Hololens2

Question: I have an hololens2 application that I built in Unity. The Environment.Username works in the Unity editor correctly, but when I deployed to the HL2, the Environment.UserName is Unknown. Under the player settings I've applied Enterprise Application, WebCam,Microphone, HumanInterfaceDevice, UserAccountInformation, Bluetooth, SystemManagement, UserDataTasks and Contacts.
It builds just fine and deploys to HL2 without problems, but the UserName is Unknown. When I go to the HL2 start menu, it displays my name correctly.
Please Help,
Martin F.
Answer: Please try the following code, it should display the account name signed into the HoloLens2:
<code>using System;
using System.Collections.Generic;
using System.Text;
using TMPro;
using UnityEngine;
#if WINDOWS_UWP
using Windows.System;
#endif
public class UserName : MonoBehaviour
{
    [SerializeField]
    private TMP_Text userNameText = null;
    // Start is called before the first frame update
    async void Start()
    {
#if WINDOWS_UWP
        IReadOnlyList<User> users = await User.FindAllAsync();
        StringBuilder sb = new StringBuilder();
        foreach (User u in users)
        {
            string name = (string)await u.GetPropertyAsync(KnownUserProperties.AccountName);
            if (string.IsNullOrWhiteSpace(name))
            {
                name = "unknown account name";
            }
            sb.AppendLine(name);
        }
        userNameText.text = sb.ToString();
#endif
    }
}
</code>


How to stop clang-format break fields seperator(::)?

Question: clang-format version 15.0.6
Question> Which settings I can use to get the expected format shown below?
Thank you
What I see:
auto amount =
    msg[AAAAAAAAAAAAAAAAAAAAAAAAA::
                bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb]
        .get<double>();

What I expect:
auto amount =
    msg[AAAAAAAAAAAAAAAAAAAAAAAAA::bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb]
        .get<double>();

My current settings for Break:
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: Always
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ObjCBreakBeforeNestedBlockParam: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10

Answer: I think you haven't set the option ColumnLimit. By default, it is set to 80. Try setting that manually, or if you don't want it to wrap, then set it to 0.
Without setting ColumnLimit:
auto amount =
    msg[AAAAAAAAAAAAAAAAAAAAAAAAA::
            bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb]
        .get<double>();

After setting ColumnLimit: 0:
auto amount =
    msg[AAAAAAAAAAAAAAAAAAAAAAAAA::bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb]
        .get<double>();

Comment: I have used ColumnLimit: 80. By setting ColumnLimit: 0 will NOT work my case since it will stop splitting long line into smaller segments.
Comment: Then why not increase columnlength ? 90 or 100 maybe?
Comment: Any hard-code ColumLimit will have issues later. I was looking for a rule that enforces the non-breakable token for ::