Beginner What is asterisk stands for on local address

Question: I am new to ubuntu server as well as server too.
When I am checking the open port using command 'netstat' .On the column of local address, there is *.http and *:ssh
what does * meaning of?
Comment: Did you mean "*:http" (a colon instead of a period)?
Answer: It means that services http and ssh are listening on all addresses available on your server.
This means at least listening on localhost (127.0.0.1) and probably on the LAN address (eg: 10.x.x.x or 192.168.x.x).
Answer: The asterix indicates that the service is listening for traffic on ALL available addresses.
Localhost and the machines defined ip-address.
If the system has multiple ip-interfaces (and thus multiple ip-addresses), all of those are included.
If a service is only bound to a specific ip-address you will see that listed here by address.
Please note this also includes services listing for multicast addresses. (So you will see 224.0.0.* appear as an ip-address in some cases.)


Help with simultaneous equation with additional term

Question: I hoped someone can help me with 3 simultaneous equations with an additional condition. I can easily solve the following 3 equations using substitution in terms of $S_1$, $S_2$ and $S_3$"
$$\begin{align*}
\text{Eq 1)} &\qquad& (O_{1}-1)S_1 - S_2 - S_3 &= 0.5P\\
\text{Eq 2)} && (O_{2}-1)S_2-S_1-S_3 &= 0.29P\\
\text{Eq 3)} && (O_{3}-1)S_3-S_1-S_2 &=0.21 P
\end{align*}$$
However, I'm struggling to solve these same equations with an additional condition
$$\text{Eq4)}\qquad S_1+S_2+S_3 = T.$$
Essentially, I want to be able to specify $T$ and calculate the values required for $S_1$, $S_2$ and $S_3$ to make Eq1 50% , Eq2 29% and Eq3 21% of the total.
$O_1$, $O_2$, & $O_3$ are known; $P$ = Eq1+Eq2+Eq3
Any advice is appreciated, thanks. (this is not homework!)
Comment: @Arturo: $O_i$ are part of the coefficients I believe...
Comment: Adding the three equations, we see $P = -T$.
Comment: @Aryabhata: Oh, I see! Sorry about that. That was pretty bad mangling I did there. Fixed.
Comment: Can't you just solve for $S_i$ in terms of $P$ (they will all be multiples of $P$ from equations 1-3, using matrix multiplication/inverse to give the solution). Then you can choose P so that Equation 4 is satisfied (except in exceptional singular cases). Alternatively take the P over to the other side in 1-3. P has zero coefficient in eq4, and you have a four-dimensional problem with unknowns $S_i$ and $P$
Comment: Thanks both Mark & Shai! I've been put back on the right track and used both of your suggestions. Thank you.
Answer: It may be worth noting that
$$
O_1 S_1 - T = 0.5P
$$
$$
O_2 S_2 - T = 0.29P
$$
$$
O_3 S_3 - T = 0.21P.
$$
Answer: Thanks to the help from both Shai Covo and Mark Bennet I've put put back on the right track.
I thought I'd just post up the method I used.


Good Tutorial for Interop.Excel.PivotTable

Question: does anyone know if there's any good online tutorial for building pivot tables using Interop.Excel.PivotTable?
These are the only two I can find:
http://blogs.msdn.com/andreww/archive/2008/07/25/creating-a-pivottable-programmatically.aspx
http://www.dreamincode.net/code/snippet1217.htm
And I'm looking for more complicated examples?
Thanks,
Comment: Could you describe as to what you are looking for?
Comment: For example, one of the above links use a PivotTableWizard, the other one doesn't. I wanted to learn what is a PivotTableWizard and what roll it plays in creating a pivot table.
One more example is grouping of data: eg http://www.technospot.net/blogs/got-huge-data-in-excel-group-columns-or-rows/
Answer: If you're going to write your own COM Excel code, you aren't going to use the Wizard.  Simply record a macro of a pivot table creation and translate the resultant VBA into your language (C#?)  Record macros of yourself playing around with pivot tables and you'll soon get a feel for how to write this code.
Some words of warning:

The pivot table API sometimes changes between versions, so what works in one installation may not work in others.  Test!
Macro code cannot always be directly translated.  There are implied order-of-operation and read-only issues that recorded macros will not show you.  If you get stuck, it's usually because you need to do things in the correct order.  These issues are hard to resolve.  E.G. - I believe charts are read-only once you relocate them to a separate sheet, so do relocation last!
Answer: you can read in MSDN with interop.Excel


C# WinService with Timer-controlled threadpool operations

Question: I have got a Windows Service class ( inherits from ServiceBase ) which at construction time is  provided with a List of objects.
Each Operation describes a DoWork() virtual method.
The point of of the Service class is to manage all the underlying Operations, add/remove them to the List at runtime and execute their DoWork() method in a ThreadPool thread.
Each Operation has a System.Timers.Timer object which is instantiated and run with the Operation class. Each Operation exposes up an event to signal the managing class that its own timer is up and it has to fire its own DoWork() method in a thread.
The TimedService class binds each elapsed timer event to a method that requests a thread from the pool:
private void CheckOperationsList(object source, EventArgs e)
{
 try
  {
    foreach (Operation op in this._operationsList)
    {
      lock (this._operationsList)
      {
        op.TimerElapsed += new Operation.ElapsedEventHandler(this.RequestThreadFromPool);
      }
    }
  }
  catch (Exception ex) { this._ManEV.WriteError(this._serviceName, ex.Message); }
}

And of course, the RequestThreadFromPool(object sender, EventArgs e) method does the following thing:
    ThreadPool.QueueUserWorkItem(new WaitCallback(((Operation)sender).DoWork), new object());

I am receiving weird behavior from the processes. I have tried to implement a dummy Operation with a timer set to 10 seconds which simply keeps the processor occupied for a couple of seconds:
for (Int16 i = 0; i < Int16.MaxValue; i++)
  {
    for (short j = 0; j < short.MaxValue; j++)
    { }
  }

Every time an Operation runs alone in the queue ( I pass a List of only one element to the Service ) everything is normal. The process spawns its own thread, keeps the CPU up for a couple of seconds, then leaves.
I implemented a lighter version of the dummy method above ( basically the same without the inner loop ) to simulate a lightweight thread.
As soon as I get two or more operations in the queue ( with different timers! ) all the threads spawn three or four times and then everything stops.
I get no activity at all. In Visual Studio it looks like the Timer of each Operation has stopped ticking - I get no calls to the event, I get no calls to the delegate that is supposed to handle the event; each Operation class has its own timer and always refer to the timer via this. !
I tried monitoring all the catch{}es, and tried wrapping each DoWork() method in a try{} , but I am getting no exceptions.
What is puzzling me is that if I run several identical Operations ( i.e. two or three long operations or two or three short ones ) everything runs as normal. Looks like as the Event comes from another class, everything gets messed up.
I am starting to think that the System.Timers.Timer class is the cause of all the above - I have tried calling Stop() to the timer right before calling the Event, but to no avail.
I get 4-5 iterations of each Operation's DoWork() method and then everything quites down.
edit I forgot to clarify: Each Operation I run is inherited from the base Operation. I.e:
public class TestLongOperation : Operation
{
  public TestLongOperation(double secondsInterval) : base(secondsInterval) { }
  public override void Work(object buh)
  {
    for (Int16 i = 0; i < Int16.MaxValue; i++)
    {
      for (short j = 0; j < short.MaxValue; j++)
      { }
  }
}

}
Any suggestions? I can provide more code if the above is not sufficient.
Thanks in advance.
Comment: Have you tryed quartz.net from http://quartznet.sourceforge.net/? StatefulJob from Quartz.net should help you out. Also check one of my posts over here: http://stackoverflow.com/questions/3995562/what-happens-in-quartz-net-quartz-when/4225590#4225590
Comment: I will look at it, but I was hoping to learn a little bit by doing it by myself. Thanks!
Answer: I have a feeling the timer creates a new thread on each tick event, I had a problem with my service that caused lots of threads to be spawned if lots of processing was still going on from the previous tick when the next tick starts.
I ended up removing the timer altogether and replaced it with a loop. Then in my main processing thread I added a Thread.sleep. This then acts like a timer but does not keep spawning threads. Also, the loop does not finish until the processing is finished.
eg:
(Main Thead)
bool running = true;

while (running)
{

  //do processing

  /sleep for 'timer' interval
  Thread.Sleep(interval)
}

does that help at all?
Comment: That is exactly what I think is happening. System.Threading.Timer should do the same. In the original implementation the supervising class had its own timer, and all the Operations were merely just wrappers for the DoWork() method, owning a nextRun DateTime property and an Interval property; each iteration the supervisor cycled through the queue, running the DoWork() threads that were expired and adding Interval to the NextRun Property, but I hoped that threads would have been a better implementations (polling or running an infinite while just doesn't sound right). Thanks!
Answer: Some random thoughts:
When the program hangs, did you hit break and cheeck if you were on a couple of 'lock' lines.
Are these one shot timers? If so, did wire up the event handler before enabling the timer. If not, is your work reentrant?
In the timer handler, why are you spawning another thread to do the work?  The timer already placed it in a new thread.  Maybe you hit thread pool exhaustion? If several timerhandlers start and use up the threadpool, then they would hang waiting to start the work threads.
Read Timer.SynchronizingObject Property  to see that Timer can use the ThreadPool.
Comment: Tried removing lock()s aswell. Tried implementing locks with boolean checks (busy/not busy). Those tasks are supposed to spring up periodically. I don't know if the timer spawns its job in another thread, what do you mean? The delegate should run in the same thread as the caller. I'll check on that as soon as I get to work and I am able to do some logical reasoning. In the meanwhile, thanks for the insight!


Tonal Harmony - Does a classical composer ever use the #6 chord in minor?

Question: In melodic minor you get a #6 (and #7) ascending.
Do composers commonly use the #6 to build a chord on? Are there exceptions?
So in a min you'd get an diminished triad like (F# A C) which would probably sound pretty dissonant.
Doesn't seem like their style?
But what even further confuses me: if you create a melody that had the f# played on a strong beat over a normal VI (F,A,C) it would sound even more dissonant.
Answer: Let's straighten out a bit of terminology to start with. Let's call the melodic degrees in question 6 and 7 when used in the major, and ♭6 and ♭7 when used unsharped in the minor. You're talking about the substitution of 6 for ♭6. Also, there's nary a major third to be seen in F♯-A-C - the chord is diminished, not augmented.
Normally F♯ is going to be a passing note in a rising scale segment from 5 to 1. However, it does present a possibility of being interpreted as part of a strong predominant. A diminished chord on F♯ would fall into this category fairly neatly (as shown in ex. a), as it would tend to suggest a truncated version of vii°7/V (ex. b) or V♭9/V (ex. c). So here, the diminished triad is standing in for the dominant of the dominant. I can't pull out an example in the literature off the top of my head, but the voice leading is so unexceptional that I don't doubt that it has been done.

Diminished chords are generally considered only moderately dissonant - they form, after all, part of the dominant seventh chord.
6 against ♭6 does show up in the literature, generally where ♭6 is falling to 5 and 6 is rising to 7. There is a rather baldfaced example in 2 voices in Domenico Scarlatti's Sonata in A minor K. 3:

Take a look at what is happening in ms. 17, 21 & 25. These all use 6 of the current key in the bass againgst a held ♭6 in the soprano. Yes, it is dissonant, probably more dissonant than it would be if mellowed by accompanying thirds, but it makes sense in the context of the voice leading. I haven't run across this cross relation used in exactly the way you posit, but I don't see anything inherently wrong with it, provided the voice leading works:

This uses your cross relation transposed by a fifth to fill out the Scarlatti example.
Comment: @11684, not a parallel fifth, a direct fifth - that's a diminished fifth (C over F♯) moving to a perfect fifth, using an inner voice at the top of the fifth, and that is quite common and quite "legal" in common practice harmony.
Comment: I edited my post to reflect `dim` instead of `aug`.
Comment: There is a rather terrible parallel fifth in your example a (doubling the a instead of the f sharp should solve this). As for a literature example, I assume any rising faulx-bourdon by a baroque composer would use the raised sixth.
Comment: Maybe a better fix would just be to resolve to V6 doubling the b.
Comment: The cross relation is fairly common, but examples that pit 6 directly against ♭6 (sounding at the same time) are a bit rare: 7 against ♭7 is more common.


Is there a way to clear the cache without dropping tables?

Question: I am new to Drupal and realized that when we clear cache in Drupal 'drush cr'. It executes drop db commands.
This is a very big security issue in our company because granting drop privileges to a mysql user on a production server would be dangerous
What would be an alternate solution?
  In Connection.php line 686:
  SQLSTATE[42000]: Syntax error or access violation: 1142 DROP command denied to user 'xxxxx'@'126.96.36.199' for table 'webprofiler': TRUNCATE {webprofiler}; Array()

  In Statement.php line 59:
  SQLSTATE[42000]: Syntax error or access violation: 1142 DROP command denied to user 'xxxxx'@'126.96.36.199' for table 'webprofiler'

Correction
drush cr is not dropping tables. MySQL Truncate requires the DROP privilege as of MySQL 5.1.16. (Before 5.1.16, it requires the DELETE privilege).
http://dev.mysql.com/doc/refman/5.1/en/truncate-table.htm
So this means when drush cr is run you need a user with drop privileges.
Comment: What do you mean it executes DROP statements?
Comment: In Connection.php line 686:

  SQLSTATE[42000]: Syntax error or access violation: 1142 DROP command denied to user 'xxxxx'@'22.214.171.124' for table 'webprofiler': TRUNCATE {webprofiler}; Array
  (
  )

In Statement.php line 59:

  SQLSTATE[42000]: Syntax error or access violation: 1142 DROP command denied to user 'xxxxx'@'126.96.36.199' for table 'webprofiler'
Comment: It's not doing DROP statements, it's running TRUNCATE statements to flush tables. TRUNCATE requires the DROP permission. Clearing the cache does not delete/restore tables.
Comment: Thanks @kevin and Clive for pointing this out. Sucks that MySQL requires drop privilage for truncate to work.  http://dev.mysql.com/doc/refman/5.1/en/truncate-table.html This is a big security issue for a production web deployment at least in my company.
Answer: As Kevin correctly points out in the comments, the cache clear is issuing a TRUNCATE command rather than DROP, but TRUNCATE does require the DROP permission.
If you look at the manual installation method in the install guide, you'll see that DROP is one of the permissions that is granted as part of the setup process:
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES ON databasename.* TO 'username'@'localhost' IDENTIFIED BY 'password';

That's followed shortly after by:

Note: Unless the database user/host combination for your Drupal installation has all of the privileges listed above (except possibly CREATE TEMPORARY TABLES, which is currently only used by Drupal core automated tests and some contributed modules), you will not be able to install or run Drupal.

So DROP is simply a required permission for Drupal to work.
Comment: Plus, most host setups have the database server isolated from external access without ssh, or at all. So this permission really isn't an issue.
Comment: @Kevin you are right. even with isolated access having a db user with drop privileges if drush is in the server any hacker could run 'drush sql:drop' and so that makes it risky. thanks for providing some answers
Comment: Same thing if (heaven forbid) another remotely exploitable SQL vulnerability is found. It's a necessary evil unfortunately. Thinking about it more, it's not even just `TRUNCATE` that's required - the field API manages deleted data in tables that it needs to create and drop. So even if the permissions were separated, it's still not going to work.


Gamma Distribution Moments

Question: Show that for X ~ Gamma($\alpha$, $\beta$), for positive constant $\nu$,
$E[X^\nu] = \dfrac{\beta^\nu*\Gamma(\nu + \alpha)}{\Gamma(\alpha)}$.
I have the following solution:
Solution
However, I don't understand how we get that $\dfrac{1}{\Gamma(\alpha)*\beta^\alpha}*\int_{0}^{\infty}x^{(\nu+\alpha)-1}e^{-x/\beta}dx = \dfrac{\Gamma(\nu+\alpha)*\beta^{\nu+\alpha}}{\Gamma(\alpha)*\beta^{\alpha}}$
Would appreciate any help on how this step was completed. Basically, I don't understand how: $\int_{0}^{\infty}x^{(\nu+\alpha)-1}e^{-x/\beta}dx = \Gamma(\nu+\alpha)*\beta^{\nu+\alpha}$.
I see that the left side is close to the definition of the Gamma function, but can't see how exactly to turn it into the right side.
Comment: If it helps, substitute $\frac{x}{\beta}=t$. Basically it is the definition of Gamma function.
Comment: If that substitution results in $(-\beta)\int_{0}^{\infty}(\beta*t)^{\nu+\alpha-1}e^{-t}dt$, then what would I do?
Answer: One way to understand the calculation is to recall that for a gamma distribution with shape $\alpha$ and scale $\beta$, $$f_X(x) = \frac{x^{\alpha-1} e^{-x/\beta}}{\beta^\alpha \Gamma(\alpha)}, \quad x > 0.$$  The denominator, being independent of $x$, suggests that $1/(\beta^\alpha \Gamma(\alpha))$ is the required multiplicative factor for the density such that $$\int_{x=0}^\infty f_X(x) \, dx = 1.$$  In other words, $$\beta^\alpha \Gamma(\alpha) = \int_{x=0}^\infty x^{\alpha - 1} e^{-x/\beta} \, dx.$$  This holds true for any $\alpha, \beta > 0$.  Now, with this in mind, $$\operatorname{E}[X^\nu] = \int_{x=0}^\infty x^\nu f_X(x) \, dx = \int_{x=0}^\infty \frac{x^{\nu + \alpha - 1} e^{-x/\beta}}{\beta^\alpha \Gamma(\alpha)} \, dx = \frac{\beta^{\nu + \alpha} \Gamma(\nu + \alpha)}{\beta^\alpha \Gamma(\alpha)}\int_{x=0}^\infty \frac{x^{\nu + \alpha - 1} e^{-x/\beta}}{\beta^{\nu + \alpha}\Gamma(\nu + \alpha)} \, dx.$$  This is what the provided solution does, but the motivation for doing so should now be clear, because now the integrand represents a gamma density with shape parameter $\nu + \alpha$, and rate $\beta$.  Therefore, its integral over its support is also $1$, provided $\nu + \alpha > 0$.  It follows that $$\operatorname{E}[X^\nu] = \frac{\beta^\nu \Gamma(\nu + \alpha)}{\Gamma(\alpha)}, \quad \nu > -\alpha,$$ as claimed.

Gamma Distribution Moments


How do I find out stuff (what it does and how) about a compiled ELF 64-bit LSB executable that can't be run (invalid pointer)?

Question: When I run the file (my-file) command, I get
my-file: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, BuildID[sha1]=d0fa19146049b8f8b97701a7fc91284f157403ef, not stripped
When I try and run ./my-file:
* Error in `./lano-campipe': realloc(): invalid pointer: 0x0000000000406dc8 *
Aborted (core dumped)
Answer: There is an aarch64 port of valgrind.  If you run your program under that, it will likely tell you precisely what is wrong and where the bug happens.
My guess, based on the address, is that you are passing an addresss (of an array or string?) to realloc that is allocated in the .data (or .rodata) section, and not in the heap.  If you run the program under GDB and enter this command
info symbol 0x0000000000406dc8

it will likely print symbol information for the address.  It is probably necessary to call malloc and copy the data to the heap to fix this bug.


How to visualize an interaction effect from a regression?

Question: I have a regression with a harmonic effect of day of the year, which interacts with other variables. I am not sure how to interpret the coefficients. My model is:
m1 <- lme(lcount ~ AirT + sin(2*pi/360*DOY) + cos(2*pi/360*DOY) +
          AirT*sin(2*pi/360*DOY) + AirT*cos(2*pi/360*DOY) + RainAmt + RainAmt*AirT,
          random = ~1|plot))

I get significant interaction effects of air temperature with the linearized harmonic day of the year (DOY) function. My response variable is the log of animal counts on each day. I want to describe how the effect of air temperature on animal observations changes depending on the day of the year.
Does anyone have suggestions on how I can interpret my beta values and/or how I can visualize the effect? I am using R but am not that skilled. The package I used for analyzing my data is nlme.
EDIT: My primary goals are (1) to describe the response of animals to environmental variables and (2) to predict future activity periods (i.e. when and under what conditions should a research bother trying to catch these animals). So if there is a better way to model this data, I would be interested in hearing it (such as cubic splines - see comments below).
Comment: This will be difficult to interpret indeed unless you constrain the ratio of the third and second coefficients to equal the ratio of the fifth and fourth: otherwise the two harmonic terms are potentially out of phase.  Harmonic representations are not very good representations of seasonal temperature fluctuations, anyway: have you considered using a cubic spline instead?
Comment: @whuber - I have no idea how I'd constrain those ratio's in R. I'm still quite a novice (in R and statistics). I'm not familiar with cubic splines. Do you have any references you'd recommend offhand? My primary goals are (1) to describe the response of animals to environmental variables and (2) to predict future activity periods (i.e. when and under what conditions should a research bother trying to catch these animals). Are cubic splines suitable for these purposes?
Comment: (1) [Search here](http://stats.stackexchange.com/search?q=%2Bcubic+%2Bspline) for information on splines.  (2) Consider using a [generalized linear model](http://cran.r-project.org/web/packages/pscl/vignettes/countreg.pdf) for these data.
Answer: Before you do any more modelling I'd definitely try to get a good visualisation of the data.  Lattice graphics are good at this sort of thing.  One trick might be to cut up the DOY variable into months, seasons, or whatever are  the meaningful divisions in your field.  Then you can do a different plot for each season showing the different relationship between the air temperature and animal count.  The code below does this by breaking DOY automatically into 12 equal parts.
library(lattice)
xyplot(lcount~AirT | cut(DOY, 12), panel=function(x,y){
    panel.xyplot(x, y)
     panel.loess(x, y, span=1)})

You can experiment with different ways of showing your location effect - different coloured characters for different plots, or you can feed it into the formula above (eg
replace the formula above with "lcount~AirT | cut(DOY, 4) + location") and get a different plot for each combination of location and quarter (assuming quartering the year is useful for you - probably isn't so you need some other way of dividing up DOY).
Have a read of the help files for xyplot and lattice.
Do you have Pinheiro and Bates' book on mixed effects models in S-Plus?  Very similar to R and lots of good advice on this sort of issue.  My copy's not here right now.
Comment: Your R command can be shortened as `xyplot(..., type=c("p","smooth"))`. The `span` (which is 2/3 by default) can be directly passed to `panel.loess()` so that your expression is equivalent to `xyplot(lcount~AirT | cut(DOY, 12), type=c("p","smooth"), span=1)`. You can also use `shingle` (or `equal.count`) instead of `cut`.


CMake: Can't link simple project that uses shared zlib wrapper library

Question: In my situation I have two CMake projects (built using MSYS Makefiles if that matters):

One is a shared library that uses Zlib and Libzip to provide zipping functionality to the library user, let's call it mylib.
Another is an app that uses mylib to do some zipping stuff. It doesn't have to worry about Zlib, Libzip or anything. Let's call it app.

I have created a bare-bones example below that demonstrates the bizarre issue I'm having. Simply put, app refuses to link when running make, but only if some Libzip code is left uncommented in mylib (which compiles and links fine, no matter what).
Zlib and Libzip source code is located in C:/Dev/Libs.
lib/CMakeLists.txt
cmake_minimum_required(VERSION 3.7)

# Set project
project(mylib)
set(LIBS_DIR "C:/Dev/Libs")

# Add mylib sources
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src")
file(GLOB_RECURSE SRCS "src/*.cpp")
add_library(mylib SHARED ${SRCS})

# Add Zlib
set(ZLIB_ROOT "${LIBS_DIR}/zlib-1.2.11")
include_directories(${ZLIB_ROOT})
target_include_directories(mylib PUBLIC ${ZLIB_ROOT})
file(GLOB ZLIB_SRCS "${ZLIB_ROOT}/*.c")
add_library(zlib STATIC ${ZLIB_SRCS})

# Add Libzip
set(LIBZIP_ROOT "${LIBS_DIR}/libzip-1.1.3")
include_directories("${LIBZIP_ROOT}/lib")
target_include_directories(mylib PUBLIC "${LIBZIP_ROOT}/lib")
file(GLOB LIBZIP_SRCS "${LIBZIP_ROOT}/lib/*.c")
add_library(libzip STATIC ${LIBZIP_SRCS})

set(LIBS_LINK    zlib libzip)
set(LIBS_INSTALL zlib libzip)
set(LIBS_EXPORT  zlib libzip)
target_link_libraries(mylib PUBLIC ${LIBS_LINK})

# https://rix0r.nl/blog/2015/08/13/cmake-guide/
# Define headers for this library. PUBLIC headers are used for
# compiling the library, and will be added to consumers' build
# paths.
target_include_directories(mylib PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
    $<INSTALL_INTERFACE:include>
    PRIVATE src)

# 'make install' to the correct locations (provided by GNUInstallDirs).
include(GNUInstallDirs)
install(TARGETS mylib ${LIBS_INSTALL} EXPORT mylibConfig
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_CURRENT_BINARY_DIR})  # This is for Windows
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

# This makes the project importable from the install directory
# Put config file in per-project dir (name MUST match), can also
# just go into 'cmake'.
install(EXPORT mylibConfig DESTINATION share/mylib/cmake)

# This makes the project importable from the build directory
export(TARGETS mylib ${LIBS_EXPORT} FILE mylibConfig.cmake)

lib/src/lib.hpp
#pragma once

namespace lib
{
    int func();
}

lib/src/lib.cpp
#include "lib.hpp"

#define ZIP_STATIC
#include "zip.h"

int lib::func()
{
    zip_source_t *src;
    zip_error_t error;
    src = zip_source_buffer_create(0, 1, 0, &error);

    return 10;
}

app/CMakeLists.txt
cmake_minimum_required(VERSION 3.7)

project(app)

# Add app sources
file(GLOB_RECURSE SRCS "src/*.cpp")
add_executable(app ${SRCS})

# Add mylib
set(mylib_DIR "${CMAKE_CURRENT_BINARY_DIR}/../lib")
find_package(mylib REQUIRED)
configure_file("${mylib_DIR}/libmylib.dll" "libmylib.dll" COPYONLY)
target_link_libraries(app mylib)

app/src/app.hpp
#pragma once
#include <lib.hpp>

app/src/app.cpp
#include <iostream>
#include "app.hpp"

int main()
{
    std::cout << lib::func() << std::endl;
    return 0;
}

Output from building mylib
$ make
[ 13%] Built target zlib
[ 98%] Built target libzip
Scanning dependencies of target mylib
[ 99%] Building CXX object CMakeFiles/mylib.dir/src/lib.cpp.obj
[100%] Linking CXX shared library libmylib.dll
[100%] Built target mylib

Output from building app
$ make
Scanning dependencies of target app
[ 50%] Building CXX object CMakeFiles/app.dir/src/app.cpp.obj
[100%] Linking CXX executable app.exe
CMakeFiles/app.dir/objects.a(app.cpp.obj):app.cpp:(.text+0x17): undefined reference to `lib::func()'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [app.exe] Error 1
make[1]: *** [CMakeFiles/app.dir/all] Error 2
make: *** [all] Error 2

If I comment src = zip_source_buffer_create(0, 1, 0, &error); in lib/src/lib.cpp, rebuild and then rebuild app:
$ make
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Dev/Builds/cmaketest/app
[ 50%] Linking CXX executable app.exe
[100%] Built target app

$ ./app
10

Which yields the expected result.
The issue must lie in the CMakeLists of app, since mylib builds fine without any linking errors. Or am I adding zlib/libzip incorrectly in mylib? I need to link to them statically and I couldn't get it to work with find_package.
Thanks!
Comment: I don't build on Windows, but don't dlls require an extra file with the list of exported symbols (memory says mylib.dll.a, but it's been years).  I don't see the `__declspec(dllexport)` stuff that enables those symbols.
Comment: That was it! Cheers!
Answer: Got it working after copying over the .dll.a file as well:
configure_file("${mylib_DIR}/libmylib.dll.a" "libmylib.dll.a" COPYONLY)

and prefixing all my methods in lib with the following:
__declspec(dllexport)


How do I reduce the volume of the metronome on a Yamaha E423 keyboard?

Question: I am using a metronome to get grip on my best timings. Currently its volume is too high, so I want to reduce it. What is the setting for it?
Note: I do not want to reduce the volume of my keyboard music. I only want to reduce the volume of metronome.
I have tried M.Volume settings under Category. But when I turn the wheel, the volume is not reducing.
Answer: I found the answer.
It was MtrVol function.
It was not M.Volume.


Is this an acceptable way to lock a container using C++?

Question: I need to implement (in C++) a thread safe container in such a way that only one thread is ever able to add or remove items from the container. I have done this kind of thing before by sharing a mutex between threads. This leads to a lot of mutex objects being littered throughout my code and makes things very messy and hard to maintain.
I was wondering if there is a neater and more object oriented way to do this. I thought of the following simple class wrapper around the container (semi-pseudo C++ code)
 class LockedList {
    private:
        std::list<MyClass> m_List;

    public:
        MutexObject Mutex;
 };

so that locking could be done in the following way
 LockedList lockableList;     //create instance
 lockableList.Mutex.Lock();    // Lock object

 ... // search and add or remove items

 lockableList.Mutex.Unlock();   // Unlock object

So my question really is to ask if this is a good approach from a design perspective? I know that allowing public access to members is frowned upon from a design perspective, does the above design have any serious flaws in it. If so is there a better way to implement thread safe container objects?
I have read a lot of books on design and C++ in general but there really does seem to be a shortage of literature regarding multithreaded programming and multithreaded software design.
If the above is a poor approach to solving the problem I have could anyone suggest a way to improve it, or point me towards some information that explains good ways to design classes to be thread safe??? Many thanks.
Comment: Who cares about "more object-oriented"? What this needs is "more RAII".
Comment: The posted code is not exception-safe. If the search, add or remove operation throws an exception the mutex is not unlocked. Providing a scoped lock-unlock mechanism is useful to avoid this.
Comment: then shouldn't you be asking your boss if this code is acceptable? Seems like his opinion is what matters
Comment: Why would your boss dictate how you write your code? Why doesn't he write it himself then?
Comment: @mathematician1975 sure, those are fine goals. But they don't necessarily imply "more object-oriented". OOP is just one among several ways to *try* to achieve some of these goals. But it's the goals that are important, not the OOP. If another paradigm allows you to better reach those goals, then the only sane thing to do is say "screw OOP"
Comment: @R.MartinhoFernandes My boss does - hence the question
Comment: Presumably you really mean "...one thread *at a time* is ever able to add or remove items..."? If only one thread will *ever* add/remove items, the correct approach is to simply not give other threads access rather than using a mutex.
Comment: @JerryCoffin Yes I did mean that - sorry.
Comment: @jalf In truth it is not just my boss. I would like to find a nice way to hide the mutex objects so my code is more maintainable
Comment: @R.MartinhoFernandes thanks for the RAII suggestion.
Comment: @jalf I take your point. Perhaps I should have worded the question in a different way. My code works fine as it is, I was just looking for a little more encapsulation and the advice of people who know much more about this than I do.
Comment: @R.MartinhoFernandes: To me `more object-oriented` is a manager speak for `more RAII`. I see them as synonyms (when working in C++).
Answer: I would do something like this to make it more exception-safe by using RAII.
class LockedList {
private:
    std::list<MyClass> m_List;
    MutexObject Mutex;
    friend class LockableListLock;
};

class LockableListLock {
private:
    LockedList& list_;
public:
    LockableListLock(LockedList& list) : list_(list) { list.Mutex.Lock(); }
    ~LockableListLock(){ list.Mutex.Unlock(); }
}

You would use it like this
LockableList list;
{
    LockableListLock lock(list); // The list is now locked.

    // do stuff to the list

} // The list is automatically unlocked when lock goes out of scope.

You could also make the class force you to lock it before doing anything with it by adding wrappers around the interface for std::list in LockableListLock so instead of accessing the list through the LockedList class, you would access the list through the LockableListLock class. For instance, you would make this wrapper around std::list::begin()
std::list::iterator LockableListLock::begin() {
    return list_.m_List.begin();
}

and then use it like this
LockableList list;
LockableListLock lock(list);
// list.begin();   //This is a compiler error so you can't
                   //access the list without locking it
lock.begin(); // This gets you the beginning of the list

Comment: Rather than make a wrapper for each method you want to use you can expose the list via `operator->()` in LockableListLock. This allows users to call methods on it but never get a reference or pointer for storage. `std::list* LockableListLock::operator->() {return &list_.m_List;}`
Answer: I would rather design a resourece owner that locks a mutex and returns an object that can be used by the thread. Once the thread has finished with it and stops using the object the resource is automatically returned to its owner and the lock released.
template<typename Resource>
class ResourceOwner
{
      Lock         lock;
      Resource     resource;

      public:
         ResourceHolder<Resource>  getExclusiveAccess()
         {
              // Let the ResourceHolder lock and unlock the lock
              // So while a thread holds a copy of this object only it
              // can access the resource. Once the thread releases all
              // copies then the lock is released allowing another
              // thread to call getExclusiveAccess().
              //
              // Make it behave like a form of smart pointer
              //    1) So you can pass it around.
              //    2) So all properties of the resource are provided via ->
              //    3) So the lock is automatically released when the thread
              //       releases the object.

              return ResourceHolder<Resource>(lock, resource);
         }
};

The resource holder (not thought hard so this can be improved)
template<typename Resource>
class ResourceHolder<
{
    // Use a shared_ptr to hold the scopped lock
    // When first created will lock the lock. When the shared_ptr
    // destroyes the scopped lock (after all copies are gone)
    // this will unlock the lock thus allowding other to use
    // getExclusiveAccess() on the owner
    std::shared_ptr<scopped_lock>    locker;
    Resource&                        resource;   // local reference on the resource.

    public:
        ResourceHolder(Lock& lock, Resource& r)
            : locker(new scopped_lock(lock))
            , resource(r)
        {}

        // Access to the resource via the -> operator
        // Thus allowing you to use all normal functionality of
        // the resource.
        Resource* operator->() {return &resource;}
};

Now a lockable list is:
ResourceOwner<list<int>>  lockedList;

void threadedCode()
{
    ResourceHolder<list<int>>  list = lockedList.getExclusiveAccess();

    list->push_back(1);
}
// When list goes out of scope here.
// It is destroyed and the the member locker will unlock `lock`
// in its destructor thus allowing the next thread to call getExclusiveAccess()

Comment: I think I'd rename it to something like `GetExclusiveAccess`. At least to me, "UnsharedAccess" seems less descriptive of the intent. Nice design though.
Comment: @JerryCoffin: That does make more sense. Done (though I lower-cased the first g).
Comment: @LokiAstari Thanks for that. A few answers here have been a great help but I think I will go with your design.
Comment: Do not dismiss @Dirk Holsopple idea. With the slight alteration I suggested in the comment it becomes as flexible as this. His also has the added advantage of being more efficient (as you don't need a shared pointer). Actually after re-reading it I prefer his solution (if you add -> and templatize it and make it non copy-able (you can not assign so why why make it copyable) and clean it up). You will still be allowed to pass it by reference, but a thread will no longer be able to accidentally keep a copy alive.
Answer: Okay, I'll state a little more directly what others have already implied: at least part, and quite possibly all, of this design is probably not what you want. At the very least, you want RAII-style locking.
I'd also make the locked (or whatever you prefer to call it) a template, so you can decouple the locking from the container itself.
// C++ like pesudo-code. Not intended to compile as-is.
struct mutex {
    void lock() { /* ... */ }
    void unlock() { /* ... */ }
};

struct lock {
    lock(mutex &m) { m.lock(); }
    ~lock(mutex &m) { m.unlock(); }
};

template <class container>
class locked {
    typedef container::value_type value_type;
    typedef container::reference_type reference_type;
    // ...

    container c;
    mutex m;
public:
    void push_back(reference_type const t) {
        lock l(m);
        c.push_back(t);
    }

    void push_front(reference_type const t) {
        lock l(m);
        c.push_front(t);
    }

    // etc.
};

This makes the code fairly easy to write and (for at least some cases) still get correct behavior -- e.g., where your single-threaded code might look like:
std::vector<int> x;

x.push_back(y);

...your thread-safe code would look like:
locked<std::vector<int> > x;

x.push_back(y);

Assuming you provide the usual begin(), end(), push_front, push_back, etc., your locked<container> will still be usable like a normal container, so it works with standard algorithms, iterators, etc.
Answer: The problem with this approach is that it makes LockedList non-copyable.  For details on this snag, please look at this question:
Designing a thread-safe copyable class
I have tried various things over the years, and a mutex declared beside the the container declaration always turns out to be the simplest way to go ( once all the bugs have been fixed after naively implementing other methods ).
You do not need to 'litter' your code with mutexes.  You just need one mutex, declared beside the container it guards.
Answer: It's hard to say that the coarse grain locking is a bad design decision.  We'd need to know about the system that the code lives in to talk about that.  It is a good starting point if you don't know that it won't work however.  Do the simplest thing that could possibly work first.
You could improve that code by making it less likely to fail if you scope without unlocking though.
struct ScopedLocker {
  ScopedLocker(MutexObject &mo_) : mo(mo_) { mo.Lock(); }
  ~ScopedLocker() { mo.Unlock(); }

  MutexObject &mo;
};

You could also hide the implementation from users.
class LockedList {
  private:
    std::list<MyClass> m_List;
    MutexObject Mutex;

  public:
    struct ScopedLocker {
       ScopedLocker(LockedList &ll);
       ~ScopedLocker();
    };
};

Then you just pass the locked list to it without them having to worry about details of the MutexObject.
You can also have the list handle all the locking internally, which is alright in some cases.  The design issue is iteration.  If the list locks internally, then operations like this are much worse than letting the user of the list decide when to lock.
void foo(LockedList &list) {
  for (size_t i = 0; i < 100000000; i++) {
    list.push_back(i);
  }
}

Generally speaking, it's a hard topic to give advice on because of problems like this.  More often than not, it's more about how you use an object.  There are a lot of leaky abstractions when you try and write code that solves multi-processor programming.  That is why you see more toolkits that let people compose the solution that meets their needs.
There are books that discuss multi-processor programming, though they are few.  With all the new C++11 features coming out, there should be more literature coming within the next few years.
Answer: I came up with this (which I'm sure can be improved to take more than two arguments):
template<class T1, class T2>
class combine : public T1, public T2
{
public:

    /// We always need a virtual destructor.
    virtual ~combine() { }
};

This allows you to do:
    // Combine an std::mutex and std::map<std::string, std::string> into
    // a single instance.
    combine<std::mutex, std::map<std::string, std::string>> mapWithMutex;

    // Lock the map within scope to modify the map in a thread-safe way.
    {
        // Lock the map.
        std::lock_guard<std::mutex> locked(mapWithMutex);

        // Modify the map.
        mapWithMutex["Person 1"] = "Jack";
        mapWithMutex["Person 2"] = "Jill";
    }

If you wish to use an std::recursive_mutex and an std::set, that would also work.


Proving a set is bounded and its supremum and infimum

Question: Question: Let $X = \{\frac{n-1}{n+1}:n\in\Bbb{N}^{>0}\}$

Is $X$ bounded above?

If yes, what is Sup$X$?

Is $X$ bounded below?

If yes, what is Inf$X$?

My attempt:
I know that $X$ is bounded, such that Sup$X=1$ and Inf$X=0$, but my uni lecturer wants us to practise proving our answers.
So I need to show that:

$\exists B\in\Bbb{R}: |x_n|\le B,\forall n\in\Bbb{N}^{>0}$ (Bounded)

Or show both:

$\exists B\in\Bbb{R}: x_n\le B,\forall n\in\Bbb{N}^{>0}$ (Bounded above)
$\exists B\in\Bbb{R}: B \le x_n,\forall n\in\Bbb{N}^{>0}$ (Bounded below)

I don't know how to approach this though, and am not sure how to prove Sup$X=1$ and Inf$X=0$.
Any help would be greatly appreciated.
Answer: I'll give you some pointers.
The easiest part is to show that $\inf X=0$: first show that in fact $0\in X$, and then show that $x\ge 0$ for each $x\in X$.
It's also not hard to show that $x<1$ for each $x\in X$, so $X$ is bounded above by $1$. Then note that $1-\frac{n-1}{n+1}=\frac{2}{n+1}$ for each $n\in\Bbb N^+$, and use this to show that if $y<1$, there is an $n\in\Bbb N^+$ such that $\frac{n-1}{n+1}>y$; this will show that $\sup X=1$ (why?).
Answer: Well, a few things:
You know that $0\le n-1 < n+1$ so $0\le \frac {n-1}{n+1} < 1$.
So that shows there $\exists B = 1$ so that $x_n \le B=1$ for all $n$.
And there $\exists B = 0$ so that $0 = B \le x_n$ for $n$.
So that shows it is bounded above and below.
....
Also $x_1 = \frac {1-1}{1+1} = \frac 0 2 = 0 \ge 0$. So the set $X$ does achieve $0$ as a value.
If a set achieves a lower bound as a value that lower bound must be the $\inf X$.  Why?  Because for any $y > 0$ then $x_1 < y$ so $y$ can't be a lower bound and as $0$ is a lower bound and nothing larger can be, then $0$ is $\inf X$.
......
So that was $\frac 34$ of the problem with no actual work.
The final thing though is that $X$ never does acheive an $x_n = 1$ so to prove $1$ is $\sup A$ will require some work.
Prove that if $y < 1$ then there is some $x_n$ so that $y < x_n \le 1$.
That would mean that any $y < 1$ can't be an upper bound, so the upper bound $1$ must be the least upper bound.
So I'll leave to you.  Show that for any $y < 1$ there will be an $x_n \in X$ so that $y < x_n \le 1$.


How can I find the standard deviation in categorical distribution

Question: How can I find the standard deviation in categorical distribution, where the elements have non-numerical attributes (e.g. colors)?
For example, I have a bag of marbles with $n$ colors. There's an infinite number of marbles in the bag, and are biased towards a certain color with a probability of $\frac{x}n, x>1$. From the bag, I pick $m$ marbles and get the probability distribution with respect to their colors.
From this distribution, by picking $q$ colors with the highest probabilities, I want to convince others with a confidence interval $k$% (e.g. 95%), that one of the colors I chose is the one that the marbles are biased towards.
In this scenario, what are some of the analysis techniques that can be used to find $q$, given $x,n,k,$ and $m$?
Answer: There is no standard deviation of a categorical variable - it makes no sense, just as the mean makes no sense.  E.g. in your example, what is the "average color"?
But there are ways to estimate the error of a binomial or multinomial proportion. It isn't clear which you want, since your title seems to ask for the latter while your text seems to ask for the former. Even for the binomial proportion, it's trickier than many people think.
The classic formula for a 95% CI for a binomial proportion is
$CI=\hat{p} \pm 1.96\sqrt{\frac{\hat{p}(1-\hat{p})}{n}}$
but this may not be best. See e.g Brown, Cai & DasGupta
Comment: Note that "exact" confidence intervals are not very accurate (see Agresti's work).  I don't know of a method for simultaneous confidence intervals, but for individual intervals Wilson's confidence interval is more accurate than "exact" methods.  It can be computed in R with the `Hmisc` package's `binconf` function.
Comment: Hi Peter, thanks so much for your guidance in this matter. I think multinomial proportion is what I was looking for (many discrete choices with different probabilities). I found a recent journal article related to finding the exact confidence coefficients for multinomial proportion [here](http://www.stat.nctu.edu.tw/~hwang/16_JMVA2008.pdf). I think this would be the one to use.
Comment: Indeed. Agresti & Coull is an excellent entry into this literature.


Using KMeans++ Computing Weighted Probability for KMeans Initialization

Question: There are times where I get less than K clusters in a K-Means algorithm implemented in Java so I searched for a solution. One thread suggested using KMeans++ for initialization but I am stucked on how to implement the algorithm  (in Wiki) most especially the 3rd step. I am wondering how to implement in on a lattice where I am doing the K-Means algorithm.
This is the exact algorithm.

Choose one center uniformly at random from among the data points.
For each data point x, compute D(x), the distance between x and the nearest center that has already been chosen.
Choose one new data point at random as a new center, using a weighted probability distribution where a point x is chosen with probability proportional to D(x)^2.
Repeat Steps 2 and 3 until k centers have been chosen.
Now that the initial centers have been chosen, proceed using standard k-means clustering.

I am a bit confused on how to implement it most especially step 3 in choosing with probability proportional to D(x)^2.
Answer: It is simple to implement the step 3. Assume that you have an array $D2$ that contains the squared of the distances computed in step 2. Now, we want to choose one of the points based on the weighted probability distribution according to $D2$. To do that, first normalize $D2$, that is, divide each element by the sum of $D2$. Then, generate a uniform random number $r$ in $[0,1]$. Then, choose the first element in the list that the sum of the values in the normalized $D2$ up to that element is greater than or equal to $r$. For example, if the normalized $D2$ is $[0.2,0.1,0.4,0.3]$ and $r$ is 0.55, the third element is selected.
Comment: how about if it is on the search of third centroid? In which of the 2 previous centroid shouls I get the D(x) or should I get it from two centroids? If from two previous centroids, how should I get it from two centroids?
Comment: Where is D(x)^2 in the algorithm?
Comment: @James-AndrewSarmiento As mentioned, array $D2$ contains D(x)^2.
Comment: @James-AndrewSarmiento Your first comment is vague.
Comment: Thank you. I mean, how do you compute D(x)'s when on the third and so on centroids? Should it be compared to the one chosen in Step 1?
Comment: @James-AndrewSarmiento D(x) is the distance between x and the nearest center. In Step 1, you randomly choose a centroid. Then, after each repetition of Steps 2 and 3, you get a new center. This way, the number of centers are increased and you have more centers to compare with.
Comment: Yeah. But how do I compare something to multiple centers/centroids?
Comment: @James-AndrewSarmiento Compute the euclidean distance between $x$ and all centroids and choose the centroid with the lowest distance.


Evaluate the following

Question: I imagine that this will not provde a significant challenge for many people, but I can't quite get my head around it. It doesn't help that I'm not entirely sure what the question is asking for.
If $x + \frac {1}x = 8$ , evaluate $x^4 + \frac {1}{x^4}$
Any help would be very much appreciated.
I apologise if the tag is not appropriate, but I was having trouble finding a suitable one.
Comment: similar: http://www.edugoog.com/details/196-194-192-190-if-x-1-x-4-then-x-4-1-x-4-is-equal-to-basic-operation-factorization.html
Answer: $x^4+\frac 1{x^4}=(x^2+\frac 1{x^2})^2-2=((x+\frac 1x)^2-2)^2-2$
Now put the value.
Answer: $$x+\frac 1x=8 \iff x^2 + 1 = 8x\ \wedge\ x \ne 0$$
Does that help?
Comment: Squaring is better...
Comment: I guess so.  This isn't exactly hard though.

Evaluate the following


How to get the managed_file progress bar working in a custom theme?

Question: I'm trying to get the managed_file progress bar working in a custom theme.  Right now, when I start an upload, the empty progress bar widget appears in the form, but it doesn't visually update while the file is being uploaded. If I switch my site from my own theme to Bartik, the bar works properly, so it's evidently a theming problem.  (This also confirms that I've got all the uploadprogress stuff properly installed.)
I've found theme_progress_bar, and tried adding it to my theme's template.php file, renaming it to MYTHEME_progress_bar, but it didn't work.  Any advice on how to get it to work?  Thanks!
Answer: Found, I think: My theme was based on Bootstrap 2, and I was getting bit by a problem of Drupal and Bootstrap disagreeing on what sort of thing the "Upload" button should be:
https://drupal.org/node/2155419
Since I'm still on Bootstrap 2 (I think) I wasn't having the problem with the input fields being out of order, but I did need to deal with the behavior assignments.  In addition to the fix outlined in the cited page, I also needed to run the assignments on page load / document ready to get things properly assigned.


listbox keypress event in server side web form

Question: I have a list box (GroupListBox) and I want to catch the enter key press event on this list box in a C# web form. I have tried many methods but no one is working for me.
Answer: If you want to do this in server side, You should use __doPostBack to get a postback.
Your listbox-
<asp:ListBox ID="ListBox1" runat="server" onkeydown="enterKeyPressed(event);">
            <asp:ListItem>one</asp:ListItem>
            <asp:ListItem>two</asp:ListItem>
            <asp:ListItem>Three</asp:ListItem>
 </asp:ListBox>

Javascript function-

In page's page load event-
protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.GetPostBackEventReference(this, string.Empty);
        if (Request.Params["__EVENTTARGET"] != null)
            if (Request.Params["__EVENTTARGET"] == "ShowMessage")
            {
                Label1.Text = "Enter is pressed";
            }
 }

Comment: He mentioned "server-side" in his question, didn't he?


Diferença entre var_dump e print_r

Question: Tenho uma dúvida simples e objetiva: Sempre nas minhas pesquisas por aí, noto que alguns programadores usam var_dump() e outros usam print_r (como eu).

Qual a diferença entre print_r() e var_dump(), sendo que ambos
  trazem "praticamente" o mesmo resultado?
Answer: Basicamente as diferenças entre print_r()e var_dump() são que o segundo além de exibir o valor/estrutura da variável mostra seu tipo e o tamanho no caso de strings. Não retornar valor da expressão/variável nesse caso var_export() é recomendado.
var_dump()
$arr = array('a', 'b', 'c', 'teste');
var_dump($arr);

Saida:
array (size=4)
  0 => string 'a' (length=1)
  1 => string 'b' (length=1)
  2 => string 'c' (length=1)
  3 => string 'teste' (length=5)

print_r()
$arr = array('a', 'b', 'c', 'teste');
print_r($arr);

Saida:
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => teste
)

Diferença entre var_dump e print_r


Interpret Wilcoxon-Mann-Whitney Result in R

Question: I am doing a Wilcoxon rank sum test (aka Wilcoxon-Mann-Whitney) with R. There are two variables "ScorePerView", that is metric and "HasCodeElement", that is binary.
So the syntax is like that

wilcox.test(ScorePerView ~ HasCodeElement, data=Datenmatrix, paired=FALSE, alternative='less')

When the result is significant (p-value < 0.005), then one distribution is lower or equal to the other.
But which one is lower?
Is the one where HasCodeElement is 0 is lower (or equal) then the one where HasCodeElement is 1.
Or is it the other way where HasCodeElement is 1 is lower (or equal) then the one where HasCodeElement is 0.
How can I interpret the R output on an one tailed test?
Here is my result with the syntax above.

Found the answer here:
How do I interpret the Mann-Whitney U when using R's formula interface
Also marked the Question as duplicated.
Comment: Thanks for going to the effort involved in finding the answer yourself and marking the question as a duplicate; this helps to make our site work better.
Answer: I don't think you should use the formula interface with ~. If you look at the syntax of the function wilcox.test here you will see that you can also separate your two samples as x and y and use wilcox.test(x,y,...). This will remove any ambiguity. If you use wilcox.test(x,y,paired=FALSE,alternative="less"), and the p-value is significant, then it means that x and y come from different populations, and that y comes from a population with larger values than x.
Comment: My problem is totally different. I have a metric and a binary variable. And they are not paired. It is like this: http://www.r-tutor.com/elementary-statistics/non-parametric-methods/mann-whitney-wilcoxon-test. But I also want to know the direction.
Comment: I corrected my answer at the same time your wrote your comment I guess. And I was proposing to split `x` into `x` and `y` based on your binary factor


Poincare-type inequality on ball.

Question: Let $B_r$ be an open ball in $\Bbb R^d$ with radius $r$. Let $u\in W^{1,p}(B_r)$ and define
$
\bar u=\frac 1{|B_r|}\int u\ dx,
$
so that by Poincare inequality we have
$$
||u-\bar u||_{L^p(B_r)} \le C||Du ||_{L^p(B_r)}\quad;\quad C=C(r,p).
$$ Actually, $C(r,p)=C(p)\cdot r$ by dilating $B_r$ to $B_1$ and change of variables.
Now, by Sobolev embedding we have $W^{1,p}(\Omega) \hookrightarrow L^q(\Omega)$ for $q\in [1,\frac{dp}{d-p}]$, i.e.
$$
||u||_{L^q} \le C'||u||_{W^{1,p}}\quad;\quad C'=C'(\Omega,p,q,d).
$$
Combining these we can easily get
$$
||u-\bar u||_{L^q(B_r)} \le C'' ||Du||_{L^p(B_r)}
$$
for some constant $C''$ depending on $r,p,q,d$.

However, I've seen a version that says
  $$
\left(\frac 1{|B_r|}\int |u-\bar u|^q \right)^{1/q} \le Cr \left(\frac 1{|B_r|}\int |Du|^p \right)^{1/p}
$$
  where $C=C(d,p,q)$ does not depend on $r$.

Is that an easy consequence of the above or does it require another approach entirely? I tried scaling argument but the term $||u||_p$ and $||Du||_p$ in $||u||_{W^{1,p}}$ scale differently. I seem to somewhat stuck, could anyone please help?
Comment: Yes, it is a consequence (by scaling), just like in the case $q=p$. Why don't you write it down in detail?
Answer: The inequality you want is only true for $q=\frac{dp}{d-p}$.
Assume first that $g\in W^{1,p}(B_1)$. Then you can extend $g$ to a function $G\in W^{1,p}(\mathbb{R}^d)$ with
$$\Vert G\Vert_{W^{1,p}(\mathbb{R}^d)}\le c \Vert g\Vert_{W^{1,p}(B_1)}.$$
Next apply Sobolev embedding to find
$$\Vert g\Vert_{L^{p^*}(B_1)}\le\Vert G\Vert_{L^{p^*}(\mathbb{R}^d)}\le c \Vert \nabla G\Vert_{L^{p}(\mathbb{R}^d)}\le C(\Vert \nabla g\Vert_{L^{p}(B_1)}+\Vert g\Vert_{L^{p}(B_1)}).$$
Now you apply this inequality to $u-\bar u$ to get
$$\Vert u-\bar u\Vert_{L^{p^*}(B_1)}\le C(\Vert \nabla u\Vert_{L^{p}(B_1)}+\Vert u-\bar u\Vert_{L^{p}(B_1)})
\le C(\Vert \nabla u\Vert_{L^{p}(B_1)},$$
where in the last inequality you apply Poincare's inequality (the one you wrote at the beginning).
Now you do a rescaling and use the fact that $p^*=\frac{dp}{d-p}$.

Poincare-type inequality on ball.


Error: Parse Error Issue whike Making get request using any library

Question: I'm Making get request with "Content-type" and "Autorization" headers only, But getting
<code>{ Error: Parse Error
    at TLSSocket.socketOnData (_http_client.js:362:20)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at TLSSocket.Readable.push (_stream_readable.js:134:10)
    at TLSWrap.onread (net.js:551:20) bytesParsed: 152, code: 'HPE_UNEXPECTED_CONTENT_LENGTH' }
</code>
error.
I have tried many library and not sending any other headers.
<code> var args = {
                headers: {
                    'Content-Type': 'application/json',
                    'accept': 'application/json',
                    'authorization' : "bearer " + accessToken
                }
            };
        self._client.get("https://api.sipgate.com/v1/groups", args, function (data, response) {
            if (response.statusMessage == 'OK'){
                if (data != null && data != undefined){
                    if (data.items != null && data.items != undefined){
                        if (data.items.length > 0){
                            deffered.resolve(data.items);
                        } else {
                            deffered.reject("data item length zero.");
                        }
                    } else {
                        deffered.reject("data item undefined.");
                    }
                } else {
                    deffered.reject("data undefined.");
                }
            } else {
                deffered.reject("Response Status not ok.");
            }
        });
</code>
With POSTMAN the request is successfully fetching the result.
Answer: UPDATE: I alerted the sipgate i/o support about this problem. They released a fix today. I already tested it and the problem is solved.
From my research the problem is that sipgate is using transfer-encoding chunked, but includes a content-length header. You can see the explanation here


How to convert data into timeseries for column groups

Question: I have data with timestamps. Users do tasks, and the timestamp is recorded. Each user is identified by a 'uid'. I want to convert this data into 10-minute granular time series, but for each user separately. So, timestamp goes in chronological order for uid=1 separately, then for uid=2 and so on.
From:
timestamp          uid    var
2020-01-01 10:00    1     10
2020-01-01 10:04    2     20
2020-01-01 20:02    2     15
2020-01-01 21:20    1     10
..
2020-01-15 23:12    1     5

To:
timestamp           uid   var
2020-01-01 10:00     1    10
2020-01-01 10:10     1    NaN
2020-01-01 10:20     1    NaN
...
2020-01-15 23:10     1    5
2020-01-01 10:00     2    20
2020-01-01 10:10     2    NaN
2020-01-01 10:20     2    NaN
...

Answer: grouped by uid column and resample 10T
import numpy as np

(df.groupby('uid')
 .resample(rule='10T')['var'].sum()
 .reset_index(level=0)
 .replace({0: np.NaN}))

Comment: i don know why are you asking additional question without `Accepted` when  question has been resolved. use `asfreq` with `method='ffill'`
Comment: thanks! do you have any idea how I can forward fill the missing values for 1 hour (6 rows) after each 'var' column entry?
Comment: need example for answer
Comment: I updated my post to give you an explanation.


Calculating radius of circle through chords

Question: It looks like a simple question, but for some reason I just can't figure out how to do it: I am given the following circle (see picture) and need to calculate the radius. I know how this works when you know the chord height, but not when you are given line segments as shown.
Thanks in advance.
Comment: I'm assuming the two segments are perpendicular.
Comment: @BrianTung sorry it isn't impossible.
Comment: Sorry @BrianTung should have mentioned that indeed the two line segments are perpendicular.
Answer: Use the  Law of sines

Let $R$ the circumradius of the triangle $ABC$
\begin{align*}
\frac{|AC|}{\sin B}&=2R\\[4pt]
\frac{\sqrt{2^2+4^2}}{\frac2{\sqrt{2^2+16^2}}}&=2R\\[4pt]
\frac{\sqrt{20}\sqrt{260}}{2}&=2R
\end{align*}
Then $R=5\sqrt{13}$.
Answer: Basic approach. Draw two right triangles, and equate their hypotenuses to the radius:

Use the Pythagorean theorem.  Drawing is approximately but not perfectly to scale.
Answer: Consider the function
$$y=\sqrt{r^2-(x-10)^2}+b$$
This defines the upper arc of a circle centred on $(10,b)$ with radius $r$. We want it to touch $(0,0)$ and $(0,20)$, and setting the equation up at either of these points yields the same result:
$$0=\sqrt{r^2-100}+b$$
$$b^2=r^2-100\tag1$$
Now we are given that $(16,2)$ is on the arc:
$$2=\sqrt{r^2-36}+b$$
$$(2-b)^2=4-4b+b^2=r^2-36\tag2$$
Subtract $(1)$ from $(2)$:
$$4-4b=64\qquad b=-15$$
Now the radius $r$ comes easily:
$$r=\sqrt{b^2+100}=5\sqrt{13}$$
Answer: It is a simple pythagorean theorem:


Error with Trigger in MySQL

Question: I'm trying to write a trigger with mysql but it gives me several error. The last one has no description (MySQL says only Error!) and so i'm here to ask for your help
delimiter |
CREATE TRIGGER SellStockTrigger
AFTER INSERT ON UtenteHaAzione
FOR EACH ROW

BEGIN
DECLARE BUD DOUBLE;
DECLARE SOMMA DOUBLE;
SET BUD=(SELECT BUDGET
FROM UTENTE
WHERE CODUTENTE = NEW.CODUTENTE);

SET SOMMA= NEW.COSTOAZIONE*NEW.QTAAZIONE;
SET BUD=BUD-SOMMA;
IF (BUD<0)
 raise_application_error (6002, 'budget exceeded');
ELSE
UPDATE UTENTE
WHERE CODUTENTE=NEW.CODUTENTE
SET BUDGET=BUD;
END IF;
END;
delimiter ;

This is my database structure.
Utente_Table
UtenteHaAzione_Table
Unfortunately i used to write trigger in Oracle Databases, so i'm sure that i'm missing something!
Thanks for your help
Comment: first thing first change `END;` to `END; |`
Answer: 2 things -
First after the end you need the delim something as
END; |
delimiter ;

Next the update query is wrong and should be as
UPDATE UTENTE
SET BUDGET=BUD
WHERE CODUTENTE=NEW.CODUTENTE
;

Comment: Thanks! But I still have an error for "raise_application_error (6002, 'budget exceeded');". What is MySQL syntax for this oracle expression?
Comment: hmm are you sure that the BUD is +ve value ? you can create a test table and insert the value of BUD from trigger to see what value its getting.
Comment: i think that the problem is the method raise_application_error...like if it does not exists in MySQL syntax...
Comment: yes raise_aplication_error is not there in mysql but in mysql you can use signal http://dev.mysql.com/doc/refman/5.5/en/signal.html#signal-condition-information-items

Error with Trigger in MySQL


How can one restore an old guitar? (a 1950s, Soviet made acoustic)

Question: Today I was at my grandparents' house and found a really old guitar. It's kinda small (I don't know why), but it also has 7 strings. Another strange thing for me which I have never seen - the nut is wooden and it's not detachable, like modern ones have (so it's part of the head itself).
The tuning pegs are really hard to rotate; I think that's because the strings are very old. The body itself has some scratches, which I like, because it looks really old and vintage. The guitar also has some sort of strange truss rod, you can rotate it from the back of the fretboard where it meets the body, and I don't know why but it feels very dirty and sticky.
Main problems are tuning pegs, strings, and it needs little bit of truss adjustment.
The guitar is old (it has a date, 12 mar 1955) and I'm scared to do anything because I think that it could break.
What would you recommend?
Comment: Where do you live? What nation? Are you in a major city, or a remote rural area? You need to find a professional luthier who restores old guitars, take the guitar to them for an inspection, and obtain an estimate of the cost of restoration. Then you need to do further research and obtain other estimates from other luthiers.
Comment: I live in georgia, guitar itself was made in leningrad. I live in tbilisi, the capital but we dont have many guitar shops and less restorators. So that not gonna work, it feels very sturdy and bridge is fixed in place too,i will change string but i wont touch truss rod, because im afraid, but it's at good height for slide guitar, maybe i will use it for that
Comment: 7-string guitars are known here as "Russian", which would support the fact that it was made in Leningrad. Could you at least post a photo or two of the guitar (particularly of the most strange/damaged parts)?
Comment: Then you should look for professional musicians who play the traditional Georgian 7-string guitar, show them your guitar, and ask for advice about restoring it.
Comment: Greetings from the state of Georgia, United States of America, where I live. Welcome to Music: Practice & Theory.
Comment: Don't apologise for your use of English, BrsGamer. Its actually better than you think
Answer: It will be a Russian guitar. The extra string is a B under the others.One of the original 7-stringers that are popular now! I was in Leningrad in the sixties, and bought one. £2.50 from memory (in roubles!). The action was, and still is awful. The strings needed to be lighter gauge, to stop the action being too bad.The truss rod is adjustable from the heel where the neck and body meet, using a square socket.
The bad news is - it's probably not going to be a great player. But - it's worth renovating just for posterity. The tuners will benefit from some release oil - duck oil, WD40. With the strings off/loose, do the same with the truss rod, and work the screw.The nut should be fixed, there's no point in it not being, but it's no big deal, as long as it stays still.I don't understand your statement that says most nuts are detachable.Tap the body all over. It shouldn't 'clunk'. If it does, the wood is split, and it'll end up on the wall as a showpiece, like mine.
If it's dated 1955, it'll be a bit more than 30 yrs old!! Good luck!
Comment: Thanks for advice! I don't have problems with nut. Just it's little bit strange to me because i haven't seen wooden nut yet. What i meam by detachable is that oj most of the guitars you can change it but on this one, it's part of the fretboard
Comment: correct me if I'm wrong but I thought that Russian 7 string guitars were tuned in thirds; EGBEGBE rather than a low B string
Comment: I was wrong, it is tuned in a tight (thirds) tuning but, according to wikipedia, the most common tuning is in fact Open G, as in DGBDGBD https://en.wikipedia.org/wiki/Russian_guitar But yes the real point is that it's not just extended range but a tighter voicing that the 7 string traditionally facilitates.  I don't suppose there's any reason that you *couldn't* tune it with a Low B instead though, or indeed tune it Brazilian style (low C).


Log-Likelihood Ratio Test: Difference of Equations

Question: I have noticed that the sign at the front of the log-likelihood ratio formula changes depending on what source I have been looking at.
Below are the two formula I have seen, exactly as they were written (in case the notation is offering something I am missing):
$LLR = -2 ( LL_{\rm Simple} - LL_{\rm General})$
(Source)
or
$LLR = 2(LL_{\rm new} - LL_{\rm baseline}$)
(Source: Discovering Statistics Using SPSS, By Andy Field)
I was wondering if someone could tell me the significance of the sign at the front of the model? My suspicion is that it is something to do with whether you are introducing or removing parameters.
Thanks.
Answer: The real answer is so short as to essentially require only a comment, but I hate to leave more questions without an answer when there's an obvious one.
There's no mystery here, this is simple arithmetic. When you swap the order of the terms inside the parentheses, you invert the sign. The two forms are algebraically identical.
When the more general model is second, the sign in front will be negative, as in your first equation. If you swap it so that it is first, then you have multiplied the terms inside the parentheses in the first equation by -1, so there's a compensating negative sign comes out the front to still leave the whole thing positive.
\begin{eqnarray}
LLR &=& -2\, ( LL_{Simple} - LL_{General})\\
&=& -2 \times(-1\times-1)\times\, ( LL_{Simple} - LL_{General})\\
&=& (-2 \times -1)\times\, ( (-1\times) LL_{Simple} - (-1\times) LL_{General})\\
&=& 2\, ( - LL_{Simple} + LL_{General})\\
&=& 2\, (LL_{General} - LL_{Simple})
\end{eqnarray}
Note that in the second equation you gave, the baseline model is the simpler one, so it's of the form at the end of my (trivial) derivation above.

I should mention that I don't usually remember* the right order for the first form (or equivalently, I don't remember which one goes on the numerator in the likelihood ratio), but it's trivial to work out, since only the likelihood for the more general model can be higher, and the form of the log-LR we want will be positive. Since there's a -2 out the front, the more general model goes second (or on the denominator in the ordinary LR).
* nor will I be likely to ever expend any effort memorizing it, since it's easy to figure out what is required when I need to. I actively avoid allocating mental effort to remembering things I know can figure out as I go. It'll probably eventually get committed to memory on its own, if I need it often enough to make a difference.


Remove invalid/incorrectly placed tags from html string

Question: I'm wondering if there is a good (or good enough) way to remove invalid or incorrectly placed HTML tags from an HTML string in C#?
Example 1:

 should be changed to

Example 2:

 should be changed to

Basically the transformed html string should be W3C validated markup. I understand that this may be a bit difficult to do, perhaps there is a library that does the job well?

Thanks!
Answer: I ended up fixing the root issue that generated an invalid HTML string. In such a scenario, it is exceedingly better to fix the main problem - if possible - than the symptoms.
Answer: I'd recommend using HTMLTidy.
Since you're using C#, there's the tidy.net project.  I think there are dlls that you can just reference and use in your C# code.
Or, you can just use the command line stuff for HTMLTidy.
Comment: Thanks for the response! I found the .NET wrapper for Tidy here: https://github.com/markbeaton/TidyManaged. The up-to-date repository of this library is found here: https://github.com/htacg/tidy-html5. I remember ending up solving the root issue that generated the invalid HTML string rather than cleaning it up in the end :-)


Good Java HTML package?

Question: I need some utility class to use in order to write some results to a html file. I did this manually and it works but I'm pretty sure there has to be some free existing package out there that is more reliable, easy to use and formats the html in a nice way. Any suggestions?
I don't need a HTML parser, but something to generate good-looking HTML.
Comment: Does it have to generate good looking HTML? Couldn't it generate good-functioning HTML and if you have to, run it through a beautifier? :-)
Comment: I'd prefer doing as little work as possible...
Answer: I've been using Google Closer templates.  Some other (and maybe more commonly used) suggestions are the StringTemplate library.  Freemarker is another option as well.
Its worth mentioning that all of them provide basic logical constructs (if/else/foreach) within your template files, so its up to you to decide to use them or not.  This is the source of a lot of criticisms for these frameworks because it doesn't encourage the decoupling of controllers and views that MVC pattern calls for.

Good Java HTML package?


I cannot access drupal.org

Question: I haven't been able to access drupal.org for the past few days. When using Chrome I get: 'This webpage is not available'.  I have no problems with accessing any other site.
Oddly enough when I use a proxy I have no problem. I've cleared my cache in both IE and chrome but still no luck. I don't believe my IP address has been blacklisted so beyond that I don't know. This is true when I try on my phone too so perhaps it is IP address related?
Comment: Should have asked this is the first place:  What does: "I haven't been able to access drupal.org" mean?  Do you get "access denied", do you get "unroutable addess", do you get the WSOD, or is the nature of non-access something else?
Comment: I believe your question should moved to another SE website; not sure which one exactly though. This is probably a problem with your ISP - running a traceroute command should give you some hints.
Comment: Free Radical - when using Chrome I get 'This webpage is not available' - As for taking this elsewhere I thought that drupal.stackexchange could help since it's specific to Drupal - I have no problems with any other site.
Comment: It happened to me too, when I was in the USA: I could just access drupal.org from a proxy.
Comment: If the problem is about Chrome, then it is off-topic here.
Comment: I don't think it's about Chrome. I can't access it via IE or safari either. You seem determined to make this off-topic. The only site I have a problem with is drupal.org
Answer: For what it is worth here is a link to Chromebook help for "This webpage is not available".
Assuming you've typed in the right URL, it looks like Chrome will give you this error message if it thinks the site takes too long to respond.  The help page also suggests:

Clear your cookies.
Adjust your proxy settings

You could also try to use another browser to access the site (both Firefox and MSIE work OK for me).
Comment: V interesting - looks like you have to be logged in to get a straight up "Access Denied" for the whitelist page, otherwise you get the login form. Actually I guess that's essentially the same thing
Comment: Interesting indeed - if I can't get into Drupal.org how can i get into http://drupal.org/httpbl/whitelist
Comment: ....oops hit enter too fast. Anyway, I did get in using a proxy and when I entered http://drupal.org/httpbl/whitelist I did get Access Denied which means I'm NOT blacklisted. So I'm back to square one. It would seem that it's IP related?

I cannot access drupal.org


Combinatorics and possibly stars and bars

Question: I can across this question:
Dillian has 5 pieces of paper, each with a different math problem. In how many ways can he give these problems to his 10 friends where each friend can receive more than one problem?
I am a little confused because I do not think the answer is simply $10^5$. What am I doing wrong?
Comment: It looks like $10^5$ to me: for each paper he has $10$ choice of friend to whom to give it, and there are no restrictions.
Answer: How many ways can he give the 1st piece of paper to one of his friends? 10.
How many ways can he give the 2nd piece of paper to one of his friends? 10.
...
How many ways can he give the 5th piece of paper to one of his friends? 10.
Each of these decisions is independent of each of the other.
So there are $10 \times 10 \times \dotsb \times 10$ ways of distributing his problems.
Answer: He can give each problem to his friends in 10 different ways and since there are 5 problems the answer is $10^5$.
Comment: Yes that is right.Apologies


Showing bound on operator $||Tx|| \ge \frac{||x||}{||(T')^{-1}||}$

Question: T is an bounded linear operator on a Banach space, X, and I'm given that it's adjoint $T'$ is invertible, I'm trying to show: $||Tx|| \ge \frac{||x||}{||(T')^{-1}||}$. I feel like this should be simple but I'm finding it very frustrating!
My ideas so far: $T'$ is invertible so we have that for any $x \in X$, $(T')^{-1} T g (x) = g(x)$ and then taking norms of this, and using the fact everything is bounded we find: $||g(x)|| \le ||(T')^{-1}|| ||T|| ||g|| ||x||$. I'm having trouble then deducing the inequality from this even though I feel it should follow?
Thaanks
Answer: Fix $x\in X$. If $f\in X'$ then $|f(x)|=|[(T')^{-1}T'f](x)|=|(T')^{-1}f(Tx)|\leq\|(T')^{-1}\|\|f\|\|Tx\|$. Hence
$$\|Tx\|\geq\frac{|f(x)|}{\|f\|}\cdot\frac1{\|(T')^{-1}\|}.$$
By a corollary to the Hahn-Banach theorem, there exists $f\in X'$ with $\|f\|=1$ and $f(x)=\|x\|$, completing the proof. This method manages to avoid any spectral theory.
Answer: Since $T'$ is invertible, $\operatorname{spec}(T')=\operatorname{spec}(T)$ is bounded away from zero, and we can define:
$$\lambda^- = \min_{\lambda\in\operatorname{spec}(T)}|\lambda|.$$
Obviously we have:
$$ \| T x\| \geq \lambda^{-}\|x\|,$$
hence it is sufficient to check that:
$$ \lambda^{-} = \frac{1}{\|T'^{-1}\|} $$
that follows from:
$$ \|T^{-1}\| = \max_{\lambda\in\operatorname{spec}{T'^{-1}}}|\lambda| =\frac{1}{\min_{\lambda\in\operatorname{spec}{T'}}|\lambda|}=\frac{1}{\lambda^-}.$$


How to solo over chord changes on guitar?

Question: To give some context, I am able to easily solo over a backing track based on the key of the track - all over the guitar's fretboard using most of the advanced guitar techniques and scale runs.
My solo compositions over chord changes sound neat through some trial and error but I want to improve on improvisation. The improv playing is mostly by ear but I'm looking for guidance as to how I should go to the next step of consciously playing chord-tones/arpeggio-notes to sound better.
If someone can shed light on what is the best (i.e. a digestible) way that you found during your learning to get started on this path? Cage system approach? Learning triads? Arpeggio patterns for each chord? Switching scale to target chord? Something else?
For purposes of discussion, let's stick to C major scale and I–V–vi–IV progression (C–G–Am–F).
I'm looking to get started. I really appreciate any insight into this topic.
Answer: Learn your scales and arpeggios

Cage system approach? Learning triads? Arpeggio patterns for each chord? Switching scale to target chord? Something else?

Yes, all of them. I wouldn't worry about CAGED vs another fingering system necessarily. There are pros and cons to each but most people choose either CAGED or "3 notes per string". You can try both eventually. For now just pick a system that works for you and start learning your scales. But knowing your major scales really well is probably your first priority.
Also pentatonic scales—if you don't know them already—are really helpful especially for blues, rock and pop. I'd actually probably start there if you haven't already.
Then move on to both triads and 7th chord arpeggios within whatever system you've chosen. So given a C major scale: C, D, E, F, G, A, B, you should be able to start from a position in the major scales you've learned and play the arpeggios that are diatonic to that scale (CMaj7, Dm7, Em7, FMaj7, G7, Am7, Bm7b5). So C (C, E, G), Dm (D, F, A), Em (E, G, B), etc. Or CMaj7 (C, E, G, B), Dm7 (D, F, A, C), etc.
Eventually you'll want to know melodic minor, harmonic minor, and more. But you can get really far with just major scales and arpeggios.
Don't worry about learning it all before you start improvising. Start with one scale pattern or whatever and once you get it under your fingers immediately start improvising with it. Then pick up another pattern or scale or arpeggio the next day/week/whatever.
Know what notes you're playing
When you play these things make a conscious effort to sing, say, or think the note that you're playing as well as the degree of the scale and/or chord it is. Because of the way fretboard shapes are so easily movable/transposable, guitarists have a tendency to rely on only shapes.
But knowing what notes you're playing and even better how it relates to the underlying chord/scale is important to your goal of improvising. Within a given chord it makes it easier to work with tension and release. And when connecting chords it makes much easier to map that path if you know what chord tone you're on and which one you're headed to on the next chord.
Don't get stuck just practicing
Play these scales and arpeggios but don't get stuck on practicing them in isolation. Quickly move on to using them over chords melodically.
Also while it's helpful to do a bit of isolation work to hear how various scales and arpeggios sound over a particular chord, don't get stuck doing that either. Quickly move on to playing over whole progressions and whole tunes.
While you should do some daily work on learning the fretboard and theory, the bulk of your practice time should be playing music because otherwise what's the point of any of it.
Connect the changes well
When you practice over changes one thing you'll want to put some focus on is connecting from one chord to the next. It usually doesn't sound good when you suddenly jump from one idea or scale or whatever when the chord changes. "You want to play through the changes and not over them" (I wish I knew who to attribute this quote to, I think maybe it was Jimmy Bruno). "Target notes" and "guide tones" are something to look into.
Also try using sequences in your lines. So when you play a motif or idea over one chord, try transposing and restating that idea again over the next chord. You can keep the melodic the idea the same but slightly vary the rhythm and articulation to make it feel a bit different.
Slow it down
It may be helpful to slow down to a rubato (free of tempo) so that you can really think about what you're playing and how to connect to the next thing. Go as slow as you need to so that you can think about your pathway through the changes. Do count though. So maybe restrict yourself to playing 8th notes and count 1 & 2, etc in your head so you know when the chord change is coming up but don't worry about doing it at tempo.
Compose lines, too
And ultimately your goal is improvisation, but that doesn't mean you have to be doing that all the time. Try composing some lines and then playing them. Even if you don't want to do that for an actual performance, it's helpful for trying out new techniques and analyzing what's possible without having to worry about actually playing it correctly yet. You mentioned you've done this, but I think you should try more later once you get some more of the theory in your head. It's especially helpful for things like connecting changes (as is the rubato technique).
Steal
Listen to other solos you like, figure them out, and then analyze them. It many not feel creative or in the spirit of improvising but it's something everybody does and important part of learning especially when it comes to the idiomatic differences between genre. The things I've mentioned so far apply to all genres but if you want a jazz, or metal, or blues sound then you need to listen to those types of music and figure out how those lines are made up. You don't need to play them exactly like the recording or even use them in your own performances at all. But the process of transcribing and dissecting them will teach you a lot.
Comment: Very sound advice. I like the way you've organized your response. Very easy to digest. I already know pentatonic scales, but I haven't tried every system there is - maybe, as you say, I need to try all and only then I will know what works for me. Also, relating to the "connect changes well" studying basic 'approach notes' technique has been helpful to me. And your last point is spot on. Studying my guitar heroes is probably the best exercise for me once I know my scales and chords well. Thank you!!
Answer: I learned to solo improvise using blues tracks, not necessarily backing tracks, buy back in the day you used to be able to get some great blues compilation albums from garage forecourts for a couple of quid.
Not only did i get some good practice in, as they key and artist changed throughout the albums, but I also discovered some great artists.
And practiced for hours using mainly pentatonic scales.
Great fun to practice with.
Answer: Have a listen to Mr Neil Young, some times one note will do. Played with felling and passion. BEND IT, HAMMER IT, HOLD IT, PLAY IT LOUD, PLAY IT MUTED,VIBRATO, STACCATO, This man does not play by the rules, scales and note patterns are good for the purest but what is music ? Written music is a tool we learn to try and reproduce the writers work, You will learn more by the felling you get from an off note that you worked on to make fit than any level of formal training and practice, remember you are most likely only a semitone away


Confidence interval for Population Attributable Fraction with several strata

Question: I have used aggregated data to create a table of person-years (pys) and deaths by social class, age and sex.
If we consider social class to be a modifiable factor, we can calculate the number of 'expected' deaths in a situation where the low class group has the same mortality rate as the high class group. The difference is the number of attributable deaths. In the example below this is 38, and the Population Attributable Fraction for social class is 38 / 182 = 21%.
<code>+-------+-------+--------+------+--------+--------+----------+--------------+
| Class |  Age  |  Sex   | Pys  | Deaths |  Rate  | Expected | Attributable |
+-------+-------+--------+------+--------+--------+----------+--------------+
| High  | Young | Male   |  100 |     10 |    0.1 |       10 |            0 |
| High  | Young | Female |  120 |     12 |    0.1 |       12 |            0 |
| High  | Old   | Male   |   40 |      8 |    0.2 |        8 |            0 |
| High  | Old   | Female |   80 |     12 |   0.15 |       12 |            0 |
+-------+-------+--------+------+--------+--------+----------+--------------+
| Low   | Young | Male   |  200 |     30 |   0.15 |       20 |           10 |
| Low   | Young | Female |  200 |     30 |   0.15 |       20 |           10 |
| Low   | Old   | Male   |  160 |     40 |   0.25 |       32 |            8 |
| Low   | Old   | Female |  200 |     40 |    0.2 |       30 |           10 |
+-------+-------+--------+------+--------+--------+----------+--------------+
| ALL   | ALL   | BOTH   | 1100 |    182 | 0.1655 |      144 |           38 |
+-------+-------+--------+------+--------+--------+----------+--------------+
</code>
Do you know how I would calculate a confidence interval for this fraction? It seems straightforward to calculate a PAF and a confidence interval for a 2x2 matrix (e.g. if we were just looking at rows 1 and 5 of the table), but I'm not sure how to approach the problem with a number of different strata.
Answer: I've done a bit of reading and couldn't find a formula. I did find an article looking at estimation of PAF in a prospective study - https://academic.oup.com/aje/article/171/7/837/85797 - which I didn't completely understand but I think you need individual-level data for the regression model-based estimation of relative risks.
I therefore used a Monte Carlo method to estimate confidence intervals, and created a general function:
<code>#--------------------------------------------
# Monte Carlo estimation of 95% CIs for a PAF
#--------------------------------------------

# dat is a data frame
# mod_var is the modifiable variable (string)
# ref is the reference category of the modifiable variable (string)
# adj_vars is a vector a non-modifiable variables (e.g. age, sex)
# time is the time variable (e.g. person-years)
# event is the events variable (e.g. deaths)
# N is the number of Monte Carlo simulations (integer)
# point.estimate (MEAN or MEDIAN) is the method for reporting the point estimate

mc_paf <- function(dat, mod_var, ref, adj_vars, time = 'pys', event = 'deaths', N = 10000, point.estimate = 'MEAN') {

  # deaths and mortality rates
  rd <- sapply(dat[,event], function(x) rpois(N, x))
  rd_rate <- t(t(rd) / dat[,time])

  # which are the relevant reference groups?
  refn <- dat[,c(mod_var, adj_vars)]
  refn$nc1 <- seq_len(nrow(refn))
  refn_ref <- refn[refn[,mod_var] == ref,]
  refn_ref[,mod_var] <- NULL
  names(refn_ref) <- c(adj_vars, 'nc2')
  refn <- merge(refn, refn_ref, by = adj_vars)
  refn <- refn$nc2[order(refn$nc1)]

  # expected
  expected <- t(t(rd_rate[,refn]) * dat[,time])

  # attributable
  attributable <- rd - expected
  attributable <- attributable[,dat[,mod_var] != ref] # remove those in reference category
  attributable <- rowSums(attributable)

  # PAF and results
  pafs <- attributable / rowSums(rd)
  point <- if (point.estimate == 'MEAN') mean(pafs) else median(pafs)
  c(point, quantile(pafs, c(0.025, 0.975)))

}

#------------------------
# Example (from question)
#------------------------

dat <- data.frame(
  class = c(rep('high', 4), rep('low', 4)),
  age = rep(rep(c('young', 'old'), each = 2), 2),
  sex = rep(c('male', 'female'), 4),
  pys = c(100, 120, 40, 80, 200, 200, 160, 200),
  deaths = c(10, 12, 8, 12, 30, 30, 40, 40),
  stringsAsFactors = F
)

# Monte-Carlo confidence intervals with 1M simulations

mc_paf(dat, N = 1000000, mod_var = 'class', ref = 'high', adj_vars = c('age', 'sex'), time = 'pys', event = 'deaths')
# PAF = 0.21 (95% CI 0-0.42)

# Manual calculation of PAF

dat$rate <- dat$deaths / dat$pys
dat$expected <- dat$pys * rep(dat$rate[1:4], 2)
dat$att <- dat$deaths - dat$expected
sum(dat$att) / sum(dat$deaths)

#----------------------------------
# Second example (with more levels)
#----------------------------------

dat2 <- structure(list(smoke = c("current", "current", "current", "ex",
                         "ex", "ex", "never", "never", "never", "current", "current",
                         "current", "ex", "ex", "ex", "never", "never", "never"), age = c("50-59",
                                                                                "60-69", "70-79", "50-59", "60-69", "70-79", "50-59", "60-69",
                                                                                "70-79", "50-59", "60-69", "70-79", "50-59", "60-69", "70-79",
                                                                                "50-59", "60-69", "70-79"), sex = c("male", "male", "male", "male",
                                                                                "male", "male", "male", "male", "male", "female", "female", "female",
                                                                                "female", "female", "female", "female", "female", "female"),
               pys = c(1578L, 1448L, 1345L, 2529L, 2340L, 2156L, 945L, 921L,
                       900L, 1785L, 1645L, 1602L, 1932L, 1777L, 1711L, 1445L, 1390L,
                       1388L), deaths = c(269L, 281L, 366L, 242L, 273L, 331L, 46L,
                                          77L, 84L, 256L, 289L, 363L, 147L, 178L, 237L, 99L, 80L, 134L
                       )), class = "data.frame", row.names = c(NA, -18L))

mc_paf(dat2, N = 1000000, mod_var = 'smoke', ref = 'never', adj_var = c('age', 'sex'))
# PAF = 0.43 (95% CI 0.38-0.48)

# Manual calculation of PAF

dat2$rate <- dat2$deaths / dat2$pys
dat2_ref <- dat2[dat2$smoke == 'never',]
dat2_ref[,c('pys', 'deaths', 'smoke')] <- NULL
names(dat2_ref)[3] <- 'ref_rate'
dat2 <- merge(dat2, dat2_ref)
dat2$expected <- dat2$pys * dat2$ref_rate
dat2$att <- dat2$deaths - dat2$expected
sum(dat2$att) / sum(dat2$deaths)
</code>


SVG - Move a circle along a path of an animated straight line

Question: I am trying to do a simple animation of a circle that moves along with a line that is also being animated.
My current method for moving the circle is using  and just matching the to and from coordinates with the start and end coordinates of the line, it's a manual and time consuming process.
I was also reading that the SMIL animation is deprecated with little or no support in the future. https://developer.mozilla.org/en-US/docs/Web/SVG/SVG_animation_with_SMIL
Can anyone suggest a more efficient way of doing this?

.line {
  stroke: #bfbfbf;
  stroke-width: 1;
  fill: none;
  animation: drawline 2s linear forwards;
  -moz-animation: drawline 2s linear forwards;
  -webkit-animation: drawline 2s linear forwards;
}

@keyframes drawline {
  from {
    stroke-dasharray: 0 400;
    stroke-dashoffset: 0;
  }
  to {
    stroke-dasharray: 400 400;
    stroke-dashoffset: 0;
  }
}
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 521.156 545.59">

  <line class="line" id="audit-line" fill="#000" stroke="#bfbfbf" x1="154" y1="238" x2="214" y2="30"></line>

  <circle id="audit-circle" r="18" cx="158" cy="238" stroke="#7ac142" fill="#7ac142" />
  <text id="audit-text" font-family="Arial" font-size="13">A</text>
  <text id="audit" class="fade-in delay-2" x="245" y="35" font-size="13" font-family="Arial">Text</text>
  <animate
    xlink:href="#audit-circle"
    attributeName="cx"
    to="214"
    dur="1s"
    fill="freeze" />
  <animate
    xlink:href="#audit-circle"
    attributename="cy"
    to="30"
    dur="1s"
    fill="freeze"
  />
  <animate
    xlink:href="#audit-text"
    attributeName="x"
    from="155"
    to="210"
    dur="1s"
    fill="freeze" />
  <animate
    xlink:href="#audit-text"
    attributename="y"
    from="240"
    to="35"
    dur="1s"
    fill="freeze"
  />

</svg>
Answer: You can do it a little more efficiently. For example, you don't need to animate the circle and its text independently.  Also, IMO you are making things complicated for yourself by mixing CSS and SMIL animation.

.line {
  stroke: #bfbfbf;
  stroke-width: 1;
  fill: none;
}
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 521.156 545.59">

  <line class="line" id="audit-line" fill="#000" stroke="#bfbfbf" x1="154" y1="238" x2="154" y2="238"></line>

  <g id="audit-circle">
    <circle r="18" cx="158" cy="238" stroke="#7ac142" fill="#7ac142" />
    <text x="154" y="242" font-family="Arial" font-size="13">A</text>
  </g>

  <text id="audit" class="fade-in delay-2" x="245" y="35" font-size="13" font-family="Arial">Text</text>

  <animateTransform
    xlink:href="#audit-circle"
    attributeName="transform"
    type="translate" from="0,0" to="56,-208" dur="1s"
                    additive="replace" fill="freeze"/>
  <animate
    xlink:href="#audit-line"
    attributeName="x2"
    from="154"
    to="214"
    dur="1s"
    fill="freeze" />
  <animate
    xlink:href="#audit-line"
    attributename="y2"
    from="238"
    to="30"
    dur="1s"
    fill="freeze"
  />

</svg>


A drain gradually gets clogged...

Question: Been staring at this for a while. Having trouble getting started. It seems simple enough but it's kicking my ass. I'm sure I'm missing a simple method.

In an experiment water pours through the drain that gets gradually clogged. In the first hour 1 gallon of water flowed through, and in every subsequent hour the amount of water pouring through the drain is equal to 2/3 of the amount that flowed through in the previous hour.
a) How much water poured through the drain in the second hour? What is the total amount of water that flowed through the drain in the first 2 hours?
b) How much water pours through the drain in the first 10 hours?
c) How much water would flow through the drain if the experiment continued indefinitely?
Comment: What's keeping you from answering (a)? You don't eve have to compute anthying (ok, you have to perform *one* addition) for that...
Comment: You're right, but seeing as (b) is an extension of (a), I want to understand how to solve it using whatever method (exponential differential equation?) that is supposed to be used.
Answer: I will actually tackle this question with discrete math/sequences rather than calculus. Let $x_k$ denote the amount of water drained in the $k^\textrm{th}$ hour. Since the amount that flows through the drain is $\frac{2}{3}$ the previous hour, {$x_k$} forms a geometric progression.
Q(b) can be solved by using the formula for the sum of a geometric series $\displaystyle \frac{a(1-r^n)}{1-r}$ where $a$ is the first term, $r$ the common ratio and $n$ the number of terms. Q(c) is then the sum to infinity.
We can probably approach it through setting up differential equations too but unless I'm overlooking something the discrete version will work the same and is definitely simpler.
Comment: You *are* using calculus! How do you think the formula for summing the infinite series $\sum_{i=0}^\infty a_n^i$ is derived, if not with calculus?
Comment: Thanks. This is the kind of thing I was looking for. I'd been looking in the wrong direction!

A drain gradually gets clogged...