Effective way of rendering a lot of quads (LibGDX/OpenGL ES)

Question: I want to ask you what is the most effective or atleast sufficient way to render a lot of quads. I want to render a lot of quads (atleast 150x150) aligned in grid. The quads will not have any textures, just solid background (green,black) but colors can be changed during a runtime. Thanks for your answers.
Comment: Could you give some more details about how you will use these quads? Will they ever move (as a grid or as single objects)?
Comment: They will not move, position will be static, only the color will change.
Answer: You can always abstract it to some sort of editable mesh object which can keep a list of indexes for the points to be drawn, a list of vertices which are drawn in order of the indexes, and then the colour for each vertex. You can then add to this dynamically at run time, and then prepare the mesh everytime you change it, which will bind everything to a VBO, and then you can simply draw it. Everything else is just implementation detail, but if you have a decent knowledge of OpenGL VAO's and VBO's should be fairly trivial. But otherwise I can help with this if you need.
Update(As there isn't enough space in the comment):
If I recall correctly, drawElements is the way to go. Don't draw each object separately, if you do that, you might as well use immediate mode. glDrawElements basically batches all of the information to your graphics card at once where it is all done as much in parallel as possible. If you draw one thing at a time, you won't be taking full advantage of the power of the GPU.
After your comment, this is where you need to make some design decisions based on what you need to do. So you want to translate individual meshes, and then eventually we want to build up a giant buffer which allows us to draw everything at once. So the first step would be to determine how much of the mesh will be moving.
Lets say we have 2 blocks that move, but the rest are stationary, we can create a separate editable mesh from these 2 blocks, and create our giant mesh from the remaining blocks. Another thing to check would be, how often is our mesh going to move? If it moves maybe once every minute or so, I would say its not such a big issue to recreate the entire mesh and bind the data again(assuming the mesh isn't ridiculously large, in which case you can create submeshes and only update those which need updating).
On the point in my previous paragraph, what are you going to be using the translated vertices for? If you are only using it for visual purposes, you can get away with creating a model matrix for your transforms for each sub-mesh that needs to be translated. However if you are going to be using it for things like collision detection you will need to store this information so that it can be used elsewhere. However you can still get away with just using the transform matrix instead of updating the VBO.
Hope this helps in some way! :)
Comment: So should i create some object which holds vertices, color and indices, and then i create one big buffer which will contain vertices, colors etc fro m all my objects? or draw each object separately? I also want to ask you what is more effective? glDrawElements or glDrawArrays? for this purpose of course. And what if i create only one array of vertices and my object will have Vec position, then if I do translation for each object is it waste of performance? (sorry for bad english)
Comment: Thanks for your answer, it's very helpful :) SO Basically I will create object representating quad with pos,color,indices then create one big VBO with pos + colors of all objects + one big array with all indices and draw it in one batch, am I right? Isn't here some more efficient? Because all objects will be same size, only position and color can be different and position will be set only once (at initialization) and then object can change only color. I read something about instancing but then I found that it probably is not available for OpenGL ES < 3. Thanks for your advice & tips.
Comment: Yeah, so with the VBO everything will be drawn in one batch, that is the advantage of using a VBO for binding all of your vertex data. It basically hands everything over to the GPU and it draws it all in one batch. On the note of display lists, you could look at that yes, but I'm not familiar with ES and if the API is the same as the normal API. But yes, you've got the idea. Let me know if you get it working, and maybe post a link to some screenshots or something :)
Comment: Thanks Daniel :) I created some class representing Quad (holds vertices, indices), then create Libgdx's object called Mesh, set Vertex attrib, create one big array with all vertices, indices and also bind it to Mesh, then render that mesh. Speed was quite good so I think it will be sufficient for my purposes. One more thanks for your willingness :)
Comment: But what if I want to change color of any specific Quad? Will I have to recreate whole buffer?
Comment: https://www.opengl.org/wiki/Vertex_Specification_Best_Practices this page provides some decent info on best practices. But basically it boils down to how your data should be organized. I'm assuming all the squares will be changing colour, so you can bind the colour to your Buffer on your VBO as well, but you can bind it to a separate pointer, this means if you want to update colours on your mesh(or any other value for that matter) you can only update that attribute specifically.
Comment: Yea, I Will accept, but I don't have enough reputation for VOTE-UP :( :D


Why is my program skipping the 2nd scanf line?

Question: I had this problem for a while. when I try to run my program, It will run but it will skip the 2nd scanf which is the nudoor, the part where my program ask for number of doors.
If anyone could help. Here's the screenshot of my program running DOOR SKIP

int main ()
{
    float length, height, width;
    height = 7.5;

    int nudoor, nuwin, gall, door, win, doorsol, winsol, gallsol, gallsolu;
    gall = 450;
    door = 18;
    win = 9;

    printf("Width of Room: ");
    scanf("%.2f",&width);
    printf("Number of doors: ");
    scanf("%d",&nudoor);
    printf("Number of windows: ");
    scanf("%d",&nuwin);
    printf("Length of room: ");
    scanf("%.2f",&length);

    doorsol = nudoor * door;
    winsol = nuwin * win;
    gallsol = doorsol + winsol + length + width + height;
    gallsolu = gallsol/gall;

if (gall >= gallsol)
    printf("Gallons of Paint needed: 0");
else
    printf("Gallons of Paint needed:%d",gallsolu);

return 0;
}

Comment: Compile the code with warnings enabled, and treat warnings as errors. And check the return value of your `scanf` calls. They should all return 1 in your case, if they manage to parse the input correctly.
Comment: `%.2f` is not a valid conversion specification. The behavior of your program is undefined after it calls `scanf` with that. Change it to `%f`. Also, in `gallsolu = gallsol/gall;`, you may want `-` rather than `/`.
Comment: I want to divide the gallsol and gall not subtract them.  also thanks %f worked.
Answer: Such a format string as shown below
scanf("%.2f",&width);

is incorrect. There is no precision part .2 of a format string for scanf (opposite to printf). So remove it everywhere where it is used to read float numbers.


Is it better to practice easier or harder pieces?

Question: If you are a newbie classical guitar player, is it better to practice on easier or harder pieces?
Here is an example of a hard piece and of an easier one. Assume that either piece is practiced slowly.
If you want to answer "it depends" can you then tell me what it depends on?
Edit: I forgot to be clear about that I'm thinking about an "everything else being identical" scenario. Imagine frustration for not being able to play difficult pieces and gratification for being able to play easier pieces not being a factor.
Answer: You should do both.
The reason is that there are several things one has to learn at the same time when working to improve on an instrument. It's important to learn musicianship and new techniques, and it's at least as important to learn ergonomics, proper posture and form, etc.
You should get some fairly simple, short pieces loaded into your muscle memory that you can play without thinking about them and use them for warm-up. When you start a practice session, play through some of those for 5 or 10 minutes while focusing on relaxation, posture, form, tone, and any weaknesses you might have. These are also good pieces for playing with a metronome to work on timing.
Other great uses for simple pieces that you can play easily are learning difficult key signatures by transposing them into those keys and relearning them, as well as changing the feel and relearning them. For instance, I have a Bach minuet that I've been playing for more than 30 years that is quite simple for me, so I play it straight sometimes just to relax, other times I change the key or play it with swing (NB pianists: if you haven't "jazzed up" a Bach or Mozart minuet before, you don't know what you're missing) or play it entirely staccato or with exaggerated legato, etc.
Of course, you're not going to keep learning if you only play simple pieces that you know by heart over and over again, so you certainly have to challenge yourself. Piece selection is one area where teachers provide a lot of value. Until you start working on a piece, it's hard to know whether it is an appropriate challenge or way beyond your abilities. I've been overly ambitious with learning pieces way beyond my abilities and gotten away with it, but it is dangerous in a few areas.
Working on excessively hard pieces can encourage you to try to learn techniques that can cause injury if repeated incorrectly and frequently. Also, working on overly difficult music can sap your morale and take away from the joy of music.
If you insist on doing piece selection on your own, I would look for an anthology or series of books where the pieces get more and more difficult. I'm thinking like the Suzuki method books (I'm not sure if they make them for guitar). With a resource like that, you can find approximately where you are in the difficulty spectrum and start working on pieces that are just past that.
Short of that, you should look for as much literature as you can find (http://imslp.org is a resource) and play the first several measures and see if you can judge how much it interests you and how much it challenges you. You want to find pieces where you can see how to play almost the whole piece, with one or two challenging areas for you. Using classical literature, you might look for pieces called etudes, since they are specifically written to be educational.
I think most people can teach themselves music, but it takes much longer and exposes one to many pitfalls compared to having a teacher. Only you can decide what route is best for you and your wallet, but if you haven't already learned one instrument in a structured way, trying to learn an instrument while also trying to learn how to learn an instrument is an overwhelming challenge.
Comment: Yep.  I'd just add- and this applies to all instruments- that working on excessively hard pieces can lead to learning sloppy technique, which sounds bad and must be unlearned with much grinding of teeth (I speak from sad experience).
Answer: It also depends on one's definition of practice.
If it means sight-reading, which in the initial stages covers any attempt to play a piece, then once it's sight read, it's done, hard or easy.
If it means learning to play a piece properly, it opens cans of worms. Practising an easy piece for one's own enjoyment and to have a finished performance piece eventually will obviously take less time, but if it's for exam purposes, it takes on a new complexion. If it's to re-interpret it, it still needs 'practice', but from a different perspective.
'Practising' a more difficult piece will take longer, and more effort, and more frustration, possibly leading to scrapping it 'until I'm a better player'. And, again, what's the end product for?
Given choice, as a teacher and performer, I'd say choose easier pieces, as in the same time frame one could get through an awful lot more, and diversify with style, genre, et al. Again, it's very dependent on what the final goal actually is. Rather subjective...
As an aside,the site title includes the words theory and practice. There was I feel some confusion a short time ago, as to what actually constitutes theory, and now, I'm starting to try to qualify the term practice. Help!!
Comment: My definition of practice is acquiring skills in the instrument I'm playing. My definition of optimal practice is acquiring as much skill/unit of time invested as possible.
Answer: It depends on your level of perception and your threshold of frustration and your sense of accomplishment.  And on the respective levels of your teacher.
And of course it depends on whether you want to entertain anybody but yourself in any predictable amount of time.
Answer: For sure both! Nothing like being thrown in a swimming pool and forced to learn how to swim. :) That's how I did it and it was very painstaking at first because I had to literally look up every note on a reference page like this one
as I was learning a piece. And the piece was Antonio Laur's Venezuelan Waltz # 2. :\ However, don't take those easy pieces for granted, they can help a great deal, especially while learning to sight reading. :)
Picture comes from Frederick Noad's "Solo Guitar Playing Book 1".


Open files via links

Question: Is it possible to open a file on local drive via html? I tried things as:
link for a file in the same map as the script. This works, but it downloads the file only (i use a mdb-file so it won't open in the webbrowser like an picture).
When i link to i.e. link or just without file:// nothing happens. Is it possible to link correctly to the files to open them and not just download them?
Thanks in advance!
Comment: If it were, it'd give me access to a whole host of information on your local drive that you wouldn't want me to see, so "No, it isn't"
Comment: Not possible, unless the linking file is placed at the local disk. If you want to request a file from the user, for a form submission, use ``.
Comment: It's possible, but with restrictions.
Answer: It is possible if it's on your machine.
What you have to do is register a protocol with the file extension you want to open.
In your case, .mdb would be MS Access database file.
If you want to click on a link in your browser and open the file in MS Access then you have to do the following:
Open File

Then you have to register "your_protocol" protocol with your computer.
REGEDIT4

[HKEY_CLASSES_ROOT\your_protocol]
@="URL:your_protocol Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\your_protocol\shell]

[HKEY_CLASSES_ROOT\your_protocol\shell\open]

[HKEY_CLASSES_ROOT\your_protocol\shell\open\command]
@="\"C:\\Program Files\\Application\\program.exe\" \"%1\""

Replace "your_protocol" with arbitrary name of your choosing, edit the registry and link your files like in the example.
Answer: No. 'Open' vs 'download' concerns the Content-Disposition HTTP header, returned by the server. When you open a local file, it is not coming from a web server, so you cannot change the headers / they do not exist.
Comment: Might there be another option to make it possible to open the file? or  at least get to the file by opening the folder where its located or?
Comment: My advice is don't do this at all. It is a bad idea to link to local files from a web site, end of story. How do you know that the file exists? How do you know where it is? The fact that this isn't possible to do hints at the fact that it isn't a good idea.
Comment: I got it working with the options of my webbrowser.. i.e. when u download files in chrome u have the option to always direct open the files from a specific extension. Because of the secured shell only this .bat file is ever downloaded. This means this batch file can execute the local file.
Comment: Well... I hope this site isn't public.
Comment: It's a local shell with only links available from specific sites.. A lot is locked so it won't be any problem.
Answer: If the referring HTML document is on local drive, relative URLs will refer to local files, so e.g. link would refer to a file in the same folder. However, browsers will then get to HTTP headers that would specify the media type (such as text/html or image/gif), so they will need to apply some heuristics in guessing what to do with it, or to apply bindings in the system, or just offer a "Save As" dialog (the only thing a browser can really do without knowing or guessing the media type).
Typically, however, this will work as far as you use commonly know filename suffixes like .html or .gif.
If the referring document is on a server, things are different, but you can in theory refer to local files using the file: scheme , but its effect is completely system-dependent. The example link is syntactically malformed (though some browsers may accept it), since the backslash "\" is not allowed in a URL. Using link could in principle work, and it did work on early days of the Web, in case the user's system happened to have a file that can be accessed with the pathname c:/file.ext (possibly mapped to c:\file.ext by the system).
But browsers have generally stopped supporting the file: scheme, presumably for assumed security reasons. Of course no information is directly sent to any server when a file: URL is used, but people thought it might be an indirect security threat. Moreover, file: URLs were of very limited usefulness from the beginning. (They were sometimes used in local networks to link to files on local network servers that do no act as HTTP servers.)

Open files via links


Wait for response from the Serial Port and then send next data

Question: I am reading data in bytes from .bin file and split the whole byte data into 16-16 bytes frames, so I want to 16 bytes frame one by one and wait until the first frame finished its cycle.
Callback method of SerialPort class:
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{

    // Read data from serial port:
    byte[] buffer = new byte[serialPort.BytesToRead];
    serialPort.Read(buffer, 0, buffer.Length);
    StringBuilder sb = new StringBuilder();
    List<string> response = new List<string>();
    for (int i = 0; i < buffer.Length; i++)
    {
        string currentByte = string.Format("{0:X2}", buffer[i]);
        response.Add(currentByte);

        sb.AppendFormat("{0:X2}", buffer[i]);
    }

    string responesCode = response[1].ToString();
    if (responesCode == "44")
    {
        // Wait until the first response is not received
        foreach (var packet in packetList.Skip(1))
        {
            // This method which sending the the data
            this.ReadByteDataFromFile(packet);
        }
    }
}

FdBrowseFile_Click button click:
private void FdBrowseFile_Click(object sender, RoutedEventArgs e)
{
    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
    Nullable<bool> result = dlg.ShowDialog();
    if (result == true)
    {
        byte[] fileBytes = File.ReadAllBytes(filename);

        foreach (byte[] copySlice in fileBytes.Slices(16))
        {
            var splitedByteArray = copySlice;
            if (splitedByteArray.Length != 16)
            {
                byte[] padd = new byte[16];
                var startAt = 0;
                Array.Copy(splitedByteArray, 0, padd, startAt, splitedByteArray.Length);
                packetList.Add(padd);
            }
            else
            {
                packetList.Add(splitedByteArray);
            }
        }
        ReadByteDataFromFile(packetList[0]);
    }
}

ReadByteDataFromFile method:
public void ReadByteDataFromFile(byte[] packet) {
 try {
  byte[] mBuffer = new byte[24];
  byte[] payload = new byte[16];
  int i = 0;
  foreach(var bytes in packet) {
   payload[i++] = bytes;
  }
  CheckSumHelper checkSumHelper = new CheckSumHelper();
  var ckSum = checkSumHelper.GetCheckSum(payload);
  mBuffer[0] = 0x02;
  mBuffer[1] = 0x10;
  mBuffer[2] = CheckSumHelper.GetBytesFromDecimal(packet[0]);
  mBuffer[3] = CheckSumHelper.GetBytesFromDecimal(packet[1]);
  mBuffer[4] = CheckSumHelper.GetBytesFromDecimal(packet[2]);
  mBuffer[5] = CheckSumHelper.GetBytesFromDecimal(packet[3]);
  mBuffer[6] = CheckSumHelper.GetBytesFromDecimal(packet[4]);
  mBuffer[7] = CheckSumHelper.GetBytesFromDecimal(packet[5]);
  mBuffer[8] = CheckSumHelper.GetBytesFromDecimal(packet[6]);
  mBuffer[9] = CheckSumHelper.GetBytesFromDecimal(packet[7]);
  mBuffer[10] = CheckSumHelper.GetBytesFromDecimal(packet[8]);
  mBuffer[11] = CheckSumHelper.GetBytesFromDecimal(packet[9]);
  mBuffer[12] = CheckSumHelper.GetBytesFromDecimal(packet[10]);
  mBuffer[13] = CheckSumHelper.GetBytesFromDecimal(packet[11]);
  mBuffer[14] = CheckSumHelper.GetBytesFromDecimal(packet[12]);
  mBuffer[15] = CheckSumHelper.GetBytesFromDecimal(packet[13]);
  mBuffer[16] = CheckSumHelper.GetBytesFromDecimal(packet[14]);
  mBuffer[17] = CheckSumHelper.GetBytesFromDecimal(packet[15]);
  mBuffer[18] = 0x17;
  mBuffer[19] = 0x00;
  mBuffer[20] = 0x00;
  mBuffer[21] = 0x00;
  mBuffer[22] = Convert.ToByte(int.Parse(ckSum, System.Globalization.NumberStyles.HexNumber));
  mBuffer[23] = 0x03;
  serialPort.Write(mBuffer, 0, mBuffer.Length);
 } catch (Exception ex) {
  ExceptionHandler exceptionHandler = new ExceptionHandler();
  exceptionHandler.HandleException(ex);
 }
}

How I can add a delay for ReadByteDataFromFile method?
Comment: Please provide a [mcve]. From your code, it's unclear what your question is. What does not work? At which line? What do you want to achieve? Furthermore, your code is incomplete: e.g. missing `ReadByteDataFromFile` and `packetList`.
Comment: My advice would be to first write readable code and split things up. F.e. `payload` is only used to calculate the checksum, why even copy the data? Also there are better ways to copy data from one array to the other. Also, I'm pretty sure `GetBytesFromDecimal` has nothing to do with the checksum, or does it? Also, why do you need to parse the checksum, if it's a number, it should probably return an `int` instead of a `string`. Also, the method named `ReadByteDataFromFile` doesn't read any thing from a file...
Comment: @dymanoid I want to send data to the serial port i.e to this method:`ReadByteDataFromFile`
Comment: @dymanoid Thanks for the reply! I added the code
Comment: @huysentruitw totally agreed with you!
Comment: Will do the first thing once got the time but there is nothing impacting the current execution
Comment: Please check the answer update regarding sync vs async API use.
Answer: What you need is a way to block the execution of some code, until something else has happened, or - how to get things running on two threads synchronously.
.NET has quite a few classes in the System.Threading namespace for synchronization. We'll use AutoResetEvent here.
Think of AutoResetEvent as a turnstile.

You can't move forward if the the person on the other side stops.
When you move forward, you call Wait - and it blocks you from moving,
until someone calls Set on it.
Now if we apply that to our problem:
We need to stop sending data until we get an acceptable response.
So call Wait when sending data, and let the response handling code call Set to let it move forward.
Here's an example which simulates a modem.
You send some AT commands, it responds, but the responses always end with \r\n.
var port = new SerialPort("COM2");
port.Open();

var mre = new AutoResetEvent(false);
var buffer = new StringBuilder();

port.DataReceived += (s, e) =>
{
    buffer.Append(port.ReadExisting());
    if (buffer.ToString().IndexOf("\r\n") >= 0)
    {
        Console.WriteLine("Got response: {0}", buffer);

        mre.Set(); //allow loop to continue
        buffer.Clear();
    }
};

var commandsToSend = new string[] { "AT", "AT", "AT+CSQ" };
var responseTimeout = TimeSpan.FromSeconds(10);

foreach (var command in commandsToSend)
{
    try
    {
        Console.WriteLine("Write '{0}' to {1}", command, port.PortName);
        port.WriteLine(command);

        Console.WriteLine("Waiting for response...");

        //this is where we block
        if (!mre.WaitOne(responseTimeout))
        {
            Console.WriteLine("Did not receive response");
            //do something
        }
    }
    catch (TimeoutException)
    {
        Console.WriteLine("Write took longer than expected");
    }
    catch
    {
        Console.WriteLine("Failed to write to port");
    }
}

Console.ReadLine();

Sample output when tested through virtual serial port:
(I just reply with OK<CR><LF>)
Write 'AT' to COM2
Waiting for response...
Got response: OK

Write 'AT' to COM2
Waiting for response...
Got response: OK

Write 'AT+CSQ' to COM2
Waiting for response...
Did not receive response

Answer: Wait in a loop for a full response after you write a first frame.
// Set read timeout to value recommended in the communication protocol specification
// so serial port operations don't stuck.
_port.WriteTimeout = 200;
_port.ReadTimeout = 200;

public void OnClick()
{
    // Write first frame.
    _port.Write(...);
    // Now wait for the full response.

    // Expected response length. Look for the constant value from the device communication
    // protocol specification or extract from the response header (first response bytes) if
    // there is any specified in the protocol.
    int count = ...;
    var buffer = new byte[count];
    var offset = 0;
    while (count > 0)
    {
        var readCount = _port.Read(buffer, offset, count);
        offset += readCount;
        count -= readCount;
    }
    // Now buffer contains full response or TimeoutException instance is thrown by SerialPort.
    // Check response status code and write other frames.
}

In order to not block UI thread you most probably still need to utilize synchronous API and Task.Run(). See C# await event and timeout in serial port communication discussion on StackOverflow.
For more information check Top 5 SerialPort Tips article by Kim Hamilton.
Comment: So actually i need to use that for loop after while? Or inside while?
Comment: What `for` loop? The `while` loop in my code snippet is required get a full response (some form of a frame acknowledgement) rather than a part of it from the device because `SerialPort.Read()` finish execution as soon as it receives any number of bytes.
Comment: That means i need to replace that forloop with the while


Parallellize Independent Function Calls that Each Modify Function's Parent Environment

Question: I'd like to find a way to parallelize repeated independent function calls in which each call modifies the function's parent environment. Each execution of the function is independent, however, for various reasons I am unable to consider any other implementation that doesn't rely on modifying the function's parent environment. See simplified example below. Is there a way to pass a copy of the parent environment to each node? I am running this on a linux system.
 create_fun <- function(){

        helper <- function(x, params) {x+params}
        helper2 <- function(z) {z+helper(z)}

        master <- function(y, a){
            parent <- parent.env(environment())
            formals(parent[['helper']])$params <- a
            helper2(y)}

       return(master)
}

# function to be called repeatedly
master <- create_fun()

# data to be iterated over
x <- expand.grid(1:100, 1:5)

# vector where output should be stored
results <- vector("numeric", nrow(x))

# task I'd like to parallelize
for(i in 1:nrow(x)){
    results[i] <- master(x[i,1], x[i, 2])
}

Comment: yup, the final output should be saved in results. Made this explicit.
Answer: Functions do maintain references to their parent environments.  You can look at the contents of the environment of master (the environment created by create_fun)
ls (environment(master) )
# [1] "helper"  "helper2" "master"

Using %dopar% you could do
## Globals
master <- create_fun()
x <- expand.grid(1:100, 1:5)

## Previous results
for(i in 1:nrow(x)){
    results[i] <- master(x[i,1], x[i, 2])
}

library(parallel)
library(doParallel)
cl <- makePSOCKcluster(4)
registerDoParallel(cl)

## parallel
res <- foreach(i=1:nrow(x), .combine = c) %dopar% {
    master(x[i,1], x[i,2])
}

all.equal(res, results)
# TRUE

Comment: Unfortunately I'm at home where I only have a windows computer and can't run this on my linux system. But essentially I'd be sending the task to 20 different servers. I'll see if this works tomorrow at work. Thanks a lot! Do you have any idea in the backend what's happening with the scoping?
Comment: I'll see if this works tomorrow, but I suppose the real question is that in general if you define a master function and helpers in the global environment, it seems like you still need to specify/export the helper functions in general to do parallel processing. This would suggest that the parent environment isn't copied along with a function. We shall see.
Comment: I ended up using the doSnow package but I think what you did would have worked as well even if I passed the tasks to multiple r sessions as opposed to multiple cores on the same computer.


Given two determinants, find the det of this matrix?

Question: If det $ =\begin{bmatrix} a & 1 & d\\b & 1 & e\\c & 1 & f\\\end{bmatrix} = -2$
and If det $ =\begin{bmatrix} a & 1 & d\\b & 2 & e\\c & 3 & f\\\end{bmatrix} = -1$
what is the determinant of $\begin{bmatrix} a & -5 & d\\b & -7 & e\\c & -9 & f\\\end{bmatrix}$
I'm pretty lost here, I've tried multiplying matrices and subtracting them from each other but not getting the correct answer.
Comment: The only thing that varies is the middle column. How does the determinant depend on the middle column?
Comment: @DanielFischer Not sure, do I have to factor something out?
Comment: What are the defining characteristics of the determinant?
Answer: We know that the determinant is multilinear which means linear for every column (or row) so
$$\det\begin{bmatrix} a & -5 & d\\b & -7 & e\\c & -9 & f\\\end{bmatrix} =\det\begin{bmatrix} a & -3 & d\\b & -3 & e\\c & -3 & f\\\end{bmatrix}+\det \begin{bmatrix} a & 1\times (-2) & d\\b & 2\times (-2) & e\\c & 3\times (-2) & f\\\end{bmatrix}=6+2=8$$
Answer: (This is essentially a restatement of the earlier answers, but expanding on how the multilinearity can be viewed.)
If we compute these determinants using an expansion along the second column, we find
$$\begin{vmatrix} a & 1 & d\\b & 1 & e\\c & 1 & f\\\end{vmatrix} = -1 \begin{vmatrix} b & e\\c & f\\\end{vmatrix}+1\begin{vmatrix} a & d\\c & f\\\end{vmatrix}-1\begin{vmatrix} a & d\\b & e\\\end{vmatrix}  =-2 \tag{eq 1}$$
and
$$\begin{vmatrix} a & 1 & d\\b & 2 & e\\c & 3 & f\\\end{vmatrix} = -1 \begin{vmatrix} b & e\\c & f\\\end{vmatrix}+2\begin{vmatrix} a & d\\c & f\\\end{vmatrix}-3\begin{vmatrix} a & d\\b & e\\\end{vmatrix}  =-1 \tag{eq 2}.$$
The determinant we want is
$$\begin{vmatrix} a & -5 & d\\b & -7 & e\\c & -9 & f\\\end{vmatrix} = -(-5) \begin{vmatrix} b & e\\c & f\\\end{vmatrix}+(-7)\begin{vmatrix} a & d\\c & f\\\end{vmatrix}-(-9)\begin{vmatrix} a & d\\b & e\\\end{vmatrix}$$
which is also given by $-3 \times (\text{eq } 1)-2 \times (\text{eq } 2).$  Hence the determinant is $8$.
Answer: Note that the determinant is a multilinear function (or if you prefer in this case a $3$-tensor).
We have that $(-5, -7, -9) = -3(1, 1, 1) - 2(1, 2, 3)$ and so our determinant is $-3\cdot (-2) -2\cdot (-1) = 8$.


Command Button not working

Question: I have an Excel 2010 spreadsheet with VBA code.
When I try using it in Excel 2013 some strange stuff happens:
The Command Buttons on the existing worksheets stopped working.  The VBA code associated to them does not run.  Not even an error message appears.
I tried adding a new Command Button to an existing worksheet, but it does not run either.
But, if I create a new worksheet in the spreadsheet, and add a Command Button in it, the button executes with no problems.
Comment: I sounds as if macro code does not have the permission to run; the workbook is untrusted. However, I'm not sure that explains why you can create a new worksheet in the same workbook and have code run from there. In any event, I'd start in Excel Option, Trust Center (Alt+F, T, T)
Comment: Thanks but I already tried the Trust Center.
Comment: Possible duplicate of [Microsoft Excel ActiveX Controls Disabled?](http://stackoverflow.com/questions/27411399/microsoft-excel-activex-controls-disabled)
Answer: I'm using Excel 2016 but I think I ran into a similar problem.  I put a command button into the worksheet and then the code did not run when I pressed the button.  I discovered that I was still in "Design Mode".  In the Controls group of commands in the Developer tab the "Design Mode" button was still grayed out.  I clicked it to turn off Design Mode and then the code started running when I clicked the button.  In short I was still in Design Mode (for the actual design and layout of the buttons) when I was initially trying to get the button to work.  It did not work. I exited Design Mode and then the button worked.  This may not be helpful to the person asking the question as they already had the buttons in the workbook.  However it may help someone who stumbled upon this question because they had problems getting the button to work initially.

Command Button not working


android admob id

Question: i am very new to the android app development, and i have just almost completed one app finally, and now i am trying to add admob to the app in order to show the ad undernearth.
i have installed that GoogleAdMobAdsSdkAndroid.zip version 6.1.0 and also downloaded the example from google at https://developers.google.com/mobile-ads-sdk/docs/android/fundamentals?hl=zh-CN and there is one line
adView = new AdView(this, AdSize.BANNER, AD_UNIT_ID_GOES_HERE);

that has bug AD_UNIT_ID_GOES_HERE highlighted.

is that i must replace the above AD_UNIT_ID_GOES_HERE by registering and getting an admob account? I have not registered any admob account yet.
Is that then i cannot test and preview about the ad if i have no true admob account? are there any ID available solely for testing purposes before i really register for a real one?

The java code is solely copied from the above google website and pasted as follows. android manifest and xml are also same as those in the google example.
package com.google.example.ads.fundamentals;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

/**
 * A simple {@link Activity} that embeds an AdView.
 */
public class BannerSample extends Activity {
/** The view to show the ad. */
  private AdView adView;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Create an ad.
    adView = new AdView(this, AdSize.BANNER, AD_UNIT_ID_GOES_HERE);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
    layout.addView(adView);

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest();
    adRequest.addTestDevice(AdRequest.TEST_EMULATOR);

    // Start loading the ad in the background.
    adView.loadAd(adRequest);
  }

  /** Called before the activity is destroyed. */
  @Override
  public void onDestroy() {
    // Destroy the AdView.
    if (adView != null) {
      adView.destroy();
    }

    super.onDestroy();
  }
}

Answer: Just make yourself an account. It's free!
You can't show ads without an publisher id.
Comment: many thanks!! handling all these admin things is a headache...
Google developer registering for Google wallet
admob for linking to PayPal
and then register for PayPal...

i just want to test...all in all, thanks!
Comment: Sorry for knowing here is not a proper place to ask such question but really dont know where to ask for help...As in the above website picture when I am adding site or app type, one of the field requests for "Android Package URL", my app has not uploaded to the market yet as I do not have the Publisher ID, I wont have any URL for the app yet. I make it as market://details?id= but when I press submit, it says it is invalid. What are the proper steps? How could I tackle it? Many thanks!
Comment: use fake package name like `com.dimetil.slot` instead , in a future you will change it to your real package name
Answer: Use this adunit id for testing  ads:adUnitId="a15391a0507f390"
Answer: For AppID In new Admob design. You can get in View setup instructions.

android admob id


How can I make this code faster?

Question: I have written this code for solving Euler Project No 12, but my code runs slow.
How can I make it running faster? I have read some suggests about finding divisors, but I do not understand the logic of using sqrt for n.
Can you explain the logic of it?
Here's my code:
def sumdiv(n):
  l=[d for d in range(1,int(n/2)+1) if n%d==0] # used n/2 to short loop
  return len(l)+1 # added n itself
trnums=[1,3]

while sumdiv(trnums[-1])<=501:
  k=trnums[-1]-trnums[-2]+1
  trnums.append(trnums[-1]+k)
print(trnums[-2:])

Comment: Is your question "why should I stop at `int(math.sqrt(n))+1`?
Comment: use `[d for d in range(1,int(math.sqrt(n))+1) if n%d==0]`, you're not finding any divisors after this point. That _will_ make your code faster.
Comment: If `xy = n`, then `x <= sqrt(n) <= y`, and if you identify a pair `(x, y)`, you don't need to find `(y, x)` separately.
Comment: I've already know this method but I need logic of using sqrt
Comment: Yes , Why I should stop at sqrt?
Comment: @Jean-FrançoisFabre you mean divisors instead of primer numbers, don't you?
Comment: yeah, right. But in that case multiply by 2 unless you get a perfect square.
Comment: Yes . I know prime divisors . I need all divisor
Comment: you don't need divisors, you need the number of divisors.
Comment: https://stackoverflow.com/questions/110344/algorithm-to-calculate-the-number-of-divisors-of-a-given-number someone dupehammer already used my close vote.
Answer: a and b are divisors of n if a * b = n. You can, without loss of generality, set b >= a. Therefore a * a is the upper limit; i.e. you only need to consider up to and including the square root of n. Once you have an a, you can trivially infer b.
You can set the upper limit to be d * d <= n rather than d <= sqrt(n) so obviating any computation of a square root. That will be marginally faster still.
Comment: lest take for example 28.
Comment: the divisors of 28 are 1,2,4,7,14,28. I have changed my code. but asnwer is wrong
Comment: n=28
for i in range(1,int(n**0.5)+1):
  if n%i==0:
    print(i)
Comment: answers are 1,2,4
Comment: Yes, they are the `a` in the set. You need to compute `n / a` to add in the other pair. Then you're done!
Comment: Just make sure you don't add the final element twice for the case where `n` is a prime number squared. E.g. 49.
Answer: You just want to know the number of divisors:
For any number N there are always an even number of divisors as every dividor x requires to be multiplied by another y that is itself a divisor of N except if x=sqrt(N) yileds an integer result as that would mean that x*x=N and that would be an only number. So every divisor is grouped in pairs except for sqrt(N) if it results to be a divisor.
Then you first check if the root is a divisor and then just calculate until it and add two to the count each time as the rest will always be pairs:
def number_of_divisors(n):
    root = math.sqrt(n)
    if root == int(root):
        number = 1
        limit = int(root)
    else:
        number = 0
        limit = int(root) + 1
    for i in range(1, limit):
        if (n % i) == 0:
            number += 2
    return number

i = 1
s = 1
while number_of_divisors(s) < 501:
    i += 1
    s += i

print(i) # 12375
print(s) # 76576500
print(number_of_divisors(s)) # 576

How can I make this code faster?


Java memory leak example

Question: I came across  this article about memory leak .It says that following code has memeory leak .But i dont understand how it is leaking memory.
import java.util.ArrayList;
 import java.util.List;
 import java.util.Iterator;
 import java.util.Random;

 public class MemoryLeak
 {
     // Generates long lines of gibberish words.
     static class GibberishGenerator implements Iterator<String>
     {
         private int MAX_WORD_LENGTH = 20;
         private int WORDS_PER_LINE = 250000;
         private String alphabet = ("abcdefghijklmnopqrstuvwxyz" +
                                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

         public boolean hasNext() {
             return true;
         }

         public String next() {
             StringBuffer result = new StringBuffer();
             for (int i = 0; i < WORDS_PER_LINE; i++) {
                 if (i > 0) { result.append(" "); }
                 result.append(generateWord(MAX_WORD_LENGTH));
             }

             return result.toString();
         }

         public void remove() {
             // not implemented
         }

         private String generateWord(int maxLength) {
             int length = (int)(Math.random() * (maxLength - 1)) + 1;
             StringBuffer result = new StringBuffer(length);
             Random r = new Random();

             for (int i = 0; i < length; i++) {
                 result.append(alphabet.charAt(r.nextInt(alphabet.length())));
             }

             return result.toString();
         }
     }

     // A "good" word has as many vowels as consonants and is more than two
     // letters long.
     private static String vowels = "aeiouAEIOU";

     private static boolean isGoodWord(String word) {
         int vowelCount = 0;
         int consonantCount = 0;

         for (int i = 0; i < word.length(); i++) {
             if (vowels.indexOf(word.charAt(i)) >= 0) {
                 vowelCount++;
             } else {
                 consonantCount++;
             }
         }

         return (vowelCount > 2 && vowelCount == consonantCount);
     }

     public static void main(String[] args) {
         GibberishGenerator g = new GibberishGenerator();
         List<String> goodWords = new ArrayList<String>();

         for (int i = 0; i < 1000; i++) {
             String line = g.next();
             for (String word : line.split(" ")) {
                 if (isGoodWord(word)) {
                     goodWords.add(word);

                     System.out.println("Found a good word: " + word);
                     break;
                 }
             }
         }
     }
 }

Refer following link for complete article:
What is the possible meory leak here?StringBuffer is used to generate Gibbersish worlds.
Answer: You should have kept reading, it's the reference that's added to the goodWords list as discussed in the section titled Spoiler:

Here's the culprit:
String line = g.next();
for (String word : line.split(" ")) {
    if (isGoodWord(word)) {
        goodWords.add(word);

And further on

Update: As of Java 1.7 update 6, string behaviour has changed so that taking a substring doesn't hold on to the original byte array. That's a bit of a blow to my contrived example, but the overall approach to finding the issue still holds.

Java memory leak example


time data ' 23:59' does not match format '%d/%m/%y %H:%M'

Question: Why do I get that error?
This is my forms.py:
class filtrosEntrada(forms.Form):
    fechaInicio = forms.DateField(widget=forms.DateTimeInput(attrs={'class':'form-control'}),required=True,label='Fecha de inicio: ')
    fechaFin = forms.DateField(widget=forms.DateTimeInput(attrs={'class':'form-control'}),required=True,label='Fecha final ')

This is my views.py
def reporte_salida_view(request):
    if request.user.is_authenticated(): # se genera el reporte pdf con los datos necesarios
        if request.method == "POST":
            fechaInicio = datetime.strptime(request.POST['fechaInicio']+' 00:00',"%d/%m/%y %H:%M")
            fechaFin = datetime.strptime(request.POST['fechaFin']+' 23:59',"%d/%m/%y %H:%M")

And finally my template.html
<form id='formularioEntrada' action="." method="POST">
{% csrf_token %}

        {{ form.fechaInicio.label_tag}}
        {{ form.fechaInicio}}

        {{ form.fechaInicio.errors}}

        {{ form.fechaFin.label_tag}}
        {{ form.fechaFin}}

</form>

The error is this line
fechaFin = datetime.strptime(request.POST['fechaFin']+' 23:59',"%d/%m/%y %H:%M")

I am using python 2.7 and Django 1.6
Answer: You get that error when request.POST['fechaFin'] is an empty string; e.g. the form field was cleared:
>>> from datetime import datetime
>>> datetime.strptime(' 23:59', "%d/%m/%y %H:%M")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data ' 23:59' does not match format '%d/%m/%y %H:%M'


Optimal stopping from an unknown distribution

Question: The Secretary problem
has an algorithm for fixed N and immediate accept/reject
(that is, reject reject ... accept one, stop).
There are several variants;
in mine, secretaries or samples come from a real-valued source Xj,
payoff is from best-so-far not last,
and each sample costs $ c:
maximize payoff = Xbestsofar - c * Nsample
Here's a picture of random walks of this kind: either go up to a
high-water mark (new maximum) of X1 X2 ..., or down c:

Can anyone point me to stop-or-keep-looking rules for optimal stopping
for problems like this ?
Perhaps one could combine two kinds of rule:

sample at least ..., take the best after that (Secretary problem)
stop when the peak - current exceeds some Δ (Allaart)

As @whuber points out, one needs some model of the distribution of the samples
to define the problem. This I don't have.
Nonetheless, statisticians must have looked at problems of this kind --
sequential sampling, sequential design of experiments, optimal stopping,
optimal learning ?
Then please help me rephrase my question to ask for a tutorial on ...
Added links 29Dec, thanks @James; readers please extend —
Optimal stopping
Hill, Knowing when to stop
2009, 3p, excellent
Allaart,
Stopping the maximum of a correlated random walk with cost for observation
2004, 12p
(Sorry to keep changing this — trying to converge to a standard formulation.)
Comment: What exactly is different about your problem?  The solution to the Secretary problem does not depend on the underlying distribution.  (It only requires that any possible ties can be resolved.)  If there is lack of independence among successive observations, you will need to quantify this (or at least bound it somehow) in order to make any progress.
Comment: Sorry, I wasn't clear, have tried to clarify.
Answer: Not an answer, but maybe this helps to clarify the question...
I don't think the secretary problem is without dependence on the underlying distribution.  For example, if the observations aren't stationary, the 37% approach is not likely to be optimal.
For a concrete example, a person making monthly purchases in an index fund for their retirement account could use the 37% rule to try to buy in at the low of the each month. In practice, this strategy doesn't work well because prices tend to trend upward. If some poster can cite work which deals with optimal stopping rules when the observations display trends, I'd be grateful.
Perhaps a more helpful response to the original question is this paper Skip the Square Root of n: A New Secretary Problem which deals with the objective of maximizing the expected rank of the chosen element, rather than the probability of choosing the top ranked element as in the classical secretary problem.
Comment: @James  Good points (+1).  I just want to clarify a possible misunderstanding concerning an earlier comment of mine.  In your example *there is no single underlying distribution:* if there is a trend over time, then *a fortiori* the distribution is changing.  When the distribution does *not* change in any way over time, when there is serial independence of the observations, and the distribution is continuous, then the answer to the Secretary problem remains the same no matter what shape this distribution has.
Comment: @James, could you point to some (real or synthetic) data for your problem ?
Answer: Also not an answer, but a back-of-the envelope calculation for the simpler case of i.i.d. Xj from a known distribution, to help my (non-statistician's) intuition:
Example Monte Carlo, to show the wide variation in run lengths, payoffs and costs:
<code># sample exponential*100 until > the first 3
# c0 = break-even cost, peak / n

15  [ 54 127   0  36  16  10  21  42  51  77  54 116  23 210]
17  [24 31 68  5 85]
22  [345  38 118 209 225   9   4  19 210  10  55 317  76 118  38 116 180   2 139 450]
23  [  3 111  54  82  15  22 161]
23  [ 35  34  14   2 114]

24  [ 16  89 120  11  53 119  53   5  77 109  72 289]
33  [ 88 234  15  15 165  51  18 262]
40  [138  33 156  11  59 239]
45  [ 98 139  43  31 226]
54  [ 43 139 129 215]
</code>
For Xj i.i.d. from a known distribution,
one can calculate the following:
Emax3 = Expected max( X1 .. X3 )  # or 10 or √ n ...
p = Prob { X > Emax3 }
Ntopeak ~ 1/p, the expected number of draws to the first X > Emax
Epeak = its expected value
then compare the break-even cost c0 = Epeak / (3 + Ntopeak)
to the ante you can afford.
For exponentials, as above, I get
<code>expon 3:
Emax3 158
Ntopeak 5
Epeak quartiles [187 227 296]
c0 ~ [187 227 296] / (3 + 5) = [23 28 37]
    # but Epeak dist / Ntopeak dist ?
</code>
For a given distribution and cost-per-sample c,
one could optimize the number of initial samples (here 3)
by brute force.
But the high variance in such results
makes them hard to communicate, even misleading.


Ocultar / Exibir Menu

Question: Tenho o seguinte menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_delete"
        android:orderInCategory="100"
        android:icon="@drawable/ic_delete_white_32dp"
        android:title="@string/action_faults_delete"
        app:showAsAction="always" />
</menu>

Adiciono ele em um Fragment da seguinte forma:
  @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_delete, menu);
    }

Nesta tela possuo uma lista de itens selecionáveis!
Gostaria de exibir o menu apenas quando possuo um item selecionado!
Minha dúvida é como ocultar/mostrar este menu?
Answer: rootView = inflater.inflate(R.menu.menu_delete, menu);
rootView.setVisibility(View.GONE);
Answer: Não sei se é a melhor forma, mas funciona:
private Menu menu;

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_delete"
        android:orderInCategory="100"
        android:icon="@drawable/ic_delete_white_32dp"
        android:title="@string/action_faults_delete"
        android:visible="false" //Adicionado
        app:showAsAction="always" />
</menu>

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_delete, menu);
        this.menu = menu;  //Adicionado
    }

No método o qual você seleciona um item você adiciona o seguinte código.
menu.getItem(0).setVisible(true); // Controla a exibição do item do menu True or False

Ocultar / Exibir Menu


share a global variable in multi threading doesn't work in python

Question: I am trying to share a global variable between two function in python. They are working at the same time whith multitreading.
the problem is that its like the global variable is not global at all here is my code:
import threading as T
import time

nn = False
def f1(inp):
    global nn
    while True:
        inp=inp+1
        time.sleep(0.5)
        print 'a-'+str(inp)+"\n"
        if inp > 10:
            nn=True
            print nn

def f2(inp):
    inp=int(inp)
    while nn:
        inp=inp+1
        time.sleep(1)
        print 'b-'+str(inp)+"\n"

t1= T.Thread(target=f1, args = (1,))
t1.daemon = True
t1.start()
t2= T.Thread(target=f2, args = (1,))
t2.daemon = True
t2.start()

Comment: What do you mean they are not global at all?
Comment: @AnandSKumar I meant that it seems to work localy but I found the problem. with answers, thanks
Answer: The problem is that while nn only gets evaluated once. When it is, it happens to be False because f1 has not yet made it True so f2 finishes running.  If you initialize nn = True you'll see it is being accessed by both f1 and f2
Answer: Global variables work fine in python.
The issue in your code would be that you first start f1 function , which goes into sleep for 0.5 seconds.
Then immediately after starting f1 , you also start f2 and then inside that you have the loop - while nn - but initial value of f2 is False, hence it never goes into the while loop , and that thread ends.
Are you really expecting the code to take more than 0.5 seconds to reach from start f1 thread and the while nn condition (so that nn can be set to True ) ? I think it would not take more than few nano seconds for the program to reach there.
Example of global variables working -
>>> import threading
>>> import time
>>> def func():
...     global l
...     i = 0
...     while i < 15:
...             l.append(i)
...             i += 1
...             time.sleep(1)
>>> def foo(t):
...     t.start()
...     i = 20
...     while i > 0:
...             print(l)
...             i -= 1
...             time.sleep(0.5)
>>> l = []
>>> t = threading.Thread(target=func)
>>> foo(t)
[0]
[0]
[0]
[0, 1]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Comment: thanks, and not I tought it will be like: a-1,a-2,a-3...a-9,a-10,b-1,a-11,a-12,b-2...


How to protect a database?

Question: There is a website with a server database. I'm building a desktop application which uses data from one of the tables. Hacker can just take password from assembly.
How can I protect the database?
Answer: I wouldn't store the database information in the application at all. Instead, I would create an API to the database on the website, perhaps implementing a RESTful interface or having queries that return data in an appropriate format, such as JSON, XML, or even plain text. The application could then call these web services and process the results. All of your database information stays on the server, where it is (hopefully) secure.
Comment: How all that garbage can protect the database? What is necessary is: proper access control, profuse use of integrity constraints and views for fine-grain security (capability-based security). I can think of only one reason to sandbox a DBMS implementation: it sucks and can be fooled tinkering with its communication protocol. But even in such case I don't see what a redundant textual protocol can contribute, since the network is probably the bottleneck.
Comment: This "garbage" can protect the database because you can limit the access that the user has to the content without risking security. In all cases, this might not be the optimal solution, but it is definitely one that I would consider.
Comment: I'm puzzled. Have you read something of my comment, beyond the first sentence?
Comment: The rest of it is incorrect. Lots of people sandbox their databases, and they should. It's just the right thing to do.
Comment: Thank you for this profound technical enlightenment, " *Thus Do They All* "... I hope you will be not offended if I prefer Mozart (or Tinto Brass).
Comment: More seriously: the false sense of security induced by this mythology around text-based protocols resulted in SQL code injection. What I'm saying is: "is it not time to ask DBMSes producers robust and secure protocols to communicate with their DBMSes?"
Comment: The rest (access control, integrity constraints, views for capability-based security) is about "semantics safety" and is absolutely relevant, contrary to what "lots of people" think these days.
Answer: The API adds a sometimes unnecessary application layer. Not all applications i've been involved with easily convert from using database calls to webservice calls. If the application has not been written i guess it would not matter that much.
My alternative implementation is:

Connect to the server using a secure tunnel of some sort.
Save the password encrypted on disk.

This would save me the effort of creating an API, which in most of my projects would be a waste of time.
This alternative is not viable if let's say you want to distribute the application to customers.
Answer: Your can
A) create a three tier system. Your client could interface with a server that in turn  interfaces with the database. The server stores the access credentials.
B) create personal accounts on the database for your users. This two tier model is applicable if fine grained access control to data is needed. E.g. in an inhouse application with different user roles.
Answer: Don't let the database user the application logs in as perform any write operations or read operations on anything but the application data.
Or, choose a sane architecture, as Thomas mentions above.  Databases are for storing and retrieving data, they are not a generic application server.

How to protect a database?


Vagrant acesso compartilhado entre equipe

Question: Cenário:

Duas máquinas (A e B), ambas na mesma rede local e com vagrant e virtualbox instalado. Uma é Windows e outra é mac
Na máquina A tenho um uma box homestead (laravel/homestead) com todo meu ambisnte.

Problema
Preciso que a máquina B acesse as pastas e URL da box da máquina A. Segue o que eu já tentei fazer e que não me atende:

vagrant share --ssh. Não me atende por que cai depois de uma hora
vagrant package e publicar o ambiente.

Não me atende por que não quero que os outros desenvolvedores tenham que subir cada um uma box em suas próprias máquinas

Copiar o vagrantfile ou seja o ambiente para as outras máquinas e dar um vagrant up.

Não me atende pelo motivo citado acima

Abaixo segue detalhadamente o que eu já fiz .
Solução - SHARE --SSH
Executando esses 3 comandos conforme documentação eu consegui atingir o objetivo:

Share
connect
ssh

Em sumo:
Máquina A:
vagrant login
vagrant share --ssh

Máquina B:
vagrant connect --ssh

Deu tudo certo. Mas essa solução não me atende porque a conexão cai depois de uma hora conforme documentação: security share
Solução - Package  ou copiar ambiente Vagrantfile
As soluções atráves da publicaçao de box na hashicorp
vagrant package

ou copiando o ambiente (vagrantfile) para depois dar um
vagrant up # na máquina B

Também não me atendem.
Sugestões
Alguém tem alguma sugestão para me ajudar?
Gostaria de deixar a máquina A sempre ligada para que a máquina B, C ou D dos desenvolvedores acessem ela através de uma IP ou algo do gênero.
Gostaria de saber se posso fazer isso através Private_network ou Public_network do vagrantfile?
Alguém que conhece esses recursos e que já tenha feito algo similar pode me ajudar, por favor.
Valeu pessoal.
Answer: Então, é só usar uma rede privada ("private_network") e uma chave pública para acesso via SSH. Para isso, você escreveria no Vagrantfile:
config.vm.network :private_network, ip: "220.127.116.11"

Na máquina que será acessada, é necessário adicionar a chave pública das máquinas que irão acessá-la no arquivo ~/.ssh/authorized_keys. Isso é uma forma de autenticação, você poderia também usar um usuário e senha, mas acho o uso da chave pública mais seguro por não expor credenciais. Algumas formas de configurar o acesso remoto a um servidor linux estão nesse artigo.


Number of baseballs used in a major league baseball game?

Question: The title says it all. What is the average number of baseballs used in a 9 inning game?
Answer: This question doesn't have an exact answer but there are many articles which suggest, for a particular (9 innings) game , on avergage 8 to 10 dozen baseballs are used.
From old.post-gazette.com article (link might be dead, information is taken from a thread citing this source.)

According to the Pirates' equipment manager, an actual game between the Pittsburgh Pirates and Milwaukee Brewers, used 104 baseballs.

Which is just above 8 dozen balls.
From a foxsports.com article according to Twins assistant equipment manager Tim Burke,

On any given night, teams will go through 100 baseballs or more, which equates to just a few pitches per ball.


Alterar valor de sequence com subselect

Question: Estou com o seguinte caso: Preciso alterar o valor de umas sequences de acordo com o max(id) da tabela referente, estou tentando fazer algo do tipo:
ALTER SEQUENCE CFOP_SEQ INCREMENT BY (SELECT MAX(ID)+1 FROM CFOP);

Porém me retorna a mensagem "Número inválido". Alguém sabe como posso resolver este problema ?
Answer: Você pode usar uma pl para fazer isso. Armazenando o valor do select dentro de uma variável utilizando o into e depois executando o alter com o execute immediate.
declare
  maxId number;
begin

  SELECT MAX(ID)+1
    into maxId
  FROM CFOP;

  execute immediate 'ALTER SEQUENCE CFOP_SEQ INCREMENT BY '||maxId;
end;
/

Caso você precise escrever qualquer outro comando pode adicionar ele
direto no corpo da PL/SQL, nos casos de SELECT você precisa adicionar os retornos em variáveis ou em um curso utilizando o INTO.
Você só utiliza o execute immedia
Segue o link para melhor informações sobre PL/SQL.

https://www.tutorialspoint.com/plsql/plsql_basic_syntax.htm
Comment: Perfeito, obrigado. Uma dúvida como eu faço pra executar mais 2 comandos em sequência ?
Comment: são alters também ?
Comment: Alter e um select.
Comment: Sim, na verdade em uma PL você consegue praticamente tudo.
Comment: Pode editar a resposta dando um exemplo?

Alterar valor de sequence com subselect


RAID0 (both hardware and md) slower than LVM?

Question: I'm doing some benchmarking of nodes as we build out a distributed filesystem. Since files are going to be distributed and replicated across many nodes, we're using raid0 on the nodes themselves. However, I'm getting some odd performance numbers, and I'm curious if the StackOverflow community can help figure out why. I'm using fio as my benchmarking tool; note that version 1.38 did not work for me and I needed to use version 1.59 or 1.60.
Here is my fio configuration file:
<code>[global]
directory=/mnt/gluster
lockfile=readwrite
ioengine=libaio
iodepth=8
rw=randrw
nrfiles=200
openfiles=30
runtime=900

[file]
filesize=16-8k/8M-512M
size=1GiB
</code>
For a raid0 using software raid, I got the following results (snipping to the essentials):
<code>  read : io=285240KB, bw=324535 B/s, iops=79 , runt=900011msec
  write: io=283532KB, bw=322592 B/s, iops=78 , runt=900011msec
</code>
On a raid1 using software raid, I got the following results:
<code>  read : io=683808KB, bw=778021 B/s, iops=189 , runt=900000msec
  write: io=488184KB, bw=628122 B/s, iops=153 , runt=795864msec
</code>
Single disk performance still beat the raid0 performance:
<code>  read : io=546848KB, bw=622179 B/s, iops=151 , runt=900018msec
  write: io=486736KB, bw=591126 B/s, iops=144 , runt=843166msec
</code>
LVM Striped across the four disks with 4k extents:
<code>  read : io=727036KB, bw=827198 B/s, iops=201 , runt=900007msec
  write: io=489424KB, bw=604693 B/s, iops=147 , runt=828800msec
</code>
Hardware RAID0 (HighPoint RocketRaid 2470)
<code>  read : io=326884KB, bw=371918 B/s, iops=90 , runt=900008msec
  write: io=328824KB, bw=374125 B/s, iops=91 , runt=900008msec
</code>
Note that the first four results above are running just on the motherboard's SATA controllers. However, I reproduced the results with software RAID after moving to the RocketRaid card. These are 1tb SATA drives. Running multithreaded tests delivered about the same results. Is there any reason that RAID0 would be running this slow? I thought it would deliver performance in random I/O that was superior to a single drive or RAID1.

Follow-up: Based on some suggestions from Joe at Scalable Informatics (nice guy, buy his stuff!), I changed my test to use a deeper queue and a more random block size.
<code>[global]
directory=/mnt/glusterfs
lockfile=readwrite
ioengine=libaio
iodepth=32
rw=randrw
numjobs=8

[file]
filesize=16-8k/8M-512M
blocksize_range=64k-1M
size=1GiB
nrfiles=50
openfiles=8
runtime=900
</code>
And the end result is that the HighPoint RocketRaid 2740 card sucks.

Single Disk Performance (SuperMicro motherboard SATA): 43.2mb/s read, 42.6 mb/s write
MD Raid 0 (RocketRAID used as HBA, with or without drivers loaded): 53.1 mb/s read, 54.1 mb/s write
RocketRaid Raid 0: 29.4 mb/s read, 29.2 mb/s write
MD RAID 0 (Motherboard SATA): 58.0 mb/s read, 58.3mb/s write

Performance connected to a RocketRaid card was slower across the board.
I'm going to leave this question open -- We're getting a new slate of RAID cards in within the next week or two in order to test, and I'm still looking for optimizing tips on getting more than single-disk performance, which doesn't seem to have been thoroughly answered.
Comment: RAID controller performance is heavily cache-dependent.  Can you tell us about your hardware RAID controller's cache settings?
Answer: You may be running into a stripesize issue where the written or read data is hotspotting a single disk - see the software RAID howto for details. You could check out if this is the case by looking at the output of iostat.
If you would like to check for linear access performance, try using "hdparm -t" or dd for benchmarking - this should show performance numbers which approximately double a singe disk's performance.
Answer:
I thought it would deliver performance in random I/O that was superior to a single drive or RAID1.

No, if blocks you are randomly searching for is greater then the stripe size, then it should be about the speed of a single drive.  Which still doesn't explain your results which are worse then a single drive.
From wikipedia.

For reads and writes that are larger than the stripe size, such as
  copying files or video playback, the disks will be seeking to the same
  position on each disk, so the seek time of the array will be the same
  as that of a single drive. For reads and writes that are smaller than
  the stripe size, such as database access, the drives will be able to
  seek independently.


Basic limit question to understand the methods

Question: I have a very basic question about proving limits with the epsilon-delta method.
So i want to prove  $\lim _{x\to 0}\left(\frac{1}{1-2x}\right)\:=\:1$ . first, i write it like that:
 $\left|\frac{1}{1-2x}\:-\:1\right|\:$ = $\left|\frac{2x}{1-2x}\right|\:$  = $\frac{2\left|x\right|}{\left|1-2x\right|}\:$ and from here i stuck. I know that i need to get |x|< some expression of epsilon and stuff that will be actually my delta. What i need to do from here?
Answer: Let $\varepsilon>0$, then when $|x|<\max{\frac{\varepsilon}{4},\frac{1}{4}}$ you have:
$$\frac{2|x|}{|1-2x|}<\frac{2|x|}{\frac{1}{2}}=4|x|$$
It's because $|x|<\frac{1}{4}$, so $|1-2x|>\frac{1}{2}$, and finally:
$$4|x|<\varepsilon$$
Because $|x|<\frac{\varepsilon}{4}$.
Answer: We want to show that $\dfrac{2}{|1-2x|}|x|$ can be made small (less than a preassigned $\epsilon$) by taking $|x|$ suitably close to $0$.
We can control $|x|$. The issue is with the term $\frac{2}{|1-2x|}$, which could potentially spoil things: We must make sure that $1-2x$ stays well away from $0$.
If we first of all specify that $\delta\lt \frac{1}{4}$, that forces $\frac{1}{2}\lt 1-2x$, which makes $\frac{2}{|1-2x|}\lt 4$.
Now it is easy to specify a $\delta$ that will work. We make $\delta=\min(\frac{1}{4},\frac{\epsilon}{4})$.
Comment: If $|x|\lt \frac{1}{4}$, then in particular $x\lt \frac{1}{4}$, so $2x\lt \frac{1}{2}$, and therefore $1-2x\gt 1-\frac{1}{2}=\frac{1}{2}$. There is nothing special about choosing $\delta\lt \frac{1}{4}$. We can choose it $\lt \frac{1}{10}$, and then $1-2x\gt \frac{4}{5}$.
Comment: As I mentioned, anything "small" will do, the only thing we care about is forcing $1-2x$ away from $0$. The $\frac{1}{4}$ was basically chosen to make the arithmetic simple, but as mentioned above, I could have used $\frac{1}{10}$. Anything  substantially smaller than  $\frac{1}{2}$, which makes $1-2x$ equal to $0$, bad stuff!
Comment: why δ<1/4 forces 1/2 < 1-2x?
Comment: and what is the secret to find that 1/4?


Como imprimir caracteres especiais pelo CMD ao formatar moedas?

Question: Há alguns dia atrás fiz um exercício para formatar moedas e usei o seguinte código para isso:
String us, china, france;
us = NumberFormat.getCurrencyInstance(Locale.US).format(payment);
china = NumberFormat.getCurrencyInstance(Locale.CHINA).format(payment);
france = NumberFormat.getCurrencyInstance(Locale.FRANCE).format(payment);

No prompt de comando deveria sair algo assim:
US: $12,324.13
China: ¥12,324.13
France: 12 324,13 €

Mas saiu algo assim:
US: $12,324.13
China: ?12,324.13
France: 12▒324,13 ▒

Sei que o código está correto, pois funcionou, mas ele não é exibido da forma correta. Como faço para que sempre que eu precisar rodar caracteres especiais pelo CMD eles sejam exibidos corretamente?
Comment: Creio que este problema não seja de java, e sim da codificação aceita pelo prompt
Comment: Entendo, mas existe algo que possa ser feito para arrumar isso, @Articuno?
Answer: O mais confiável seria usar a GUI. Exemplo mínimo:
JOptionPane.showMessageDialog(null, new String[] {us, china, france});

No CMD não vai conseguir mostrar todos os símbolos corretamente pois basicamente não tem suporte para Unicode. Com
> CHCP 1252

vai conseguir mostrar o € (Euro) mas provavelmente não o ¥ (\uFFE5, Yen).
Talvez essa página sobre CHCP ajude mais...
No Stackoverflow Inglês tem essa questão com algumas respostas que podem ajudar.
Comment: Testei sua sugestão de usar o JOptionPane e deu certo também. Vou usar isso, quando precisar executar pelo cmd/terminal! Muito obrigada! ^_^


Prove $x + \frac{1}{x} \geq 2$ for $x>0$.

Question: Proof that $x+\frac{1}{x}\geq2$ for $x>0$
Would this be correct?
$x*(x+\frac{1}{x}\geq2)$
$x^2+1\geq2x$
$x^2-2x+1\geq2x-2x$
$x^2-2x+1\geq0$
Plug in 1 for x:
$(1)^2-2(1)+1\geq0$
$1-2+1\geq0$
$0\geq0$ Therefore, $x+\frac{1}{2}\geq2$ is true for $x>0$
Comment: It seems to me that your started with your assumption.
Comment: [xkcd](http://forums.xkcd.com/viewtopic.php?f=17&t=110212) has a post on this with a lot of proofs.
Comment: [How to prove this inequality $ x + \frac{1}{x} \geq 2 $](http://math.stackexchange.com/questions/705064/how-to-prove-this-inequality-x-frac1x-geq-2)
Answer: We know for positive $a,b$, $\frac{ a + b}{2} \geq \sqrt{ab} $. Put $ a = x^2 $ and $b = 1$ and we obtain
$$ x + \frac{1}{x} \geq 2 $$
Added: You can also use calculus: Put $f(x) = x + \frac{1}{x} $. we have $f'(x) = 1 - \frac{1}{x^2} $ with critical values (points where $f'$ vanishes ) : $x=\pm1$. It is easy to see that $x = 1 $ will furnish a global minimum. Hence $f(x) \geq f(1) $ for all $x > 0 $. It follows that
$$ x + \frac{1}{x} \geq 2 $$
Comment: The first thing your write obscures the proof - you're going to need to prove the AM-GM inequality in basically the same way that you'd prove the original statement! (Not to mention that, if you use it, applying it to $\frac{1}x$ and $x$ directly suffices with no additional algebraic manipulation except multiplying by two)
Comment: So, what you want me to do? Delete my answer?
Comment: If it were me, I'd provide a proof of AM-GM. It's natural enough to do that for this question, since the proof of it is basically the same as the proof in the accepted answer that $x+\frac{1}x\geq 2$ - meaning such a proof could serve as a useful generalization without excessive complication and help elucidate the link between the two statements.
Answer: $$(x-1)^2\geq  0\\ \Longrightarrow x^2-2x+1\geq 0\\ \Longrightarrow x^2+1\geq2x$$ Then we assume $x$ is positive and divide by $x$. $$\Longrightarrow x+\frac1{x}\geq 2$$ See if you can prove it for negative $x$ and the case where $x=0$ then see if you can prove $$(x-1)^2\geq  0$$
Answer: Hint: You have to prove that 1 is the minimum of the function.
Answer: Proof by contradiction:
Assume that the hypothesis is wrong. Therefore, $ \exists\ x \gt 0 $ such that; $$ x + \dfrac{1}{x} \lt 2 $$
Now, follow along:
$$\begin{align}
x + \dfrac{1}{x} &\lt 2 \\
x^2 + 1 &\lt 2x \tag{$ \because x \gt 0 $} \\
x^2 - 2x + 1 &\lt 0 \\
(x - 1)^2 &\lt 0
\end{align}$$
which is a contradiction as a perfect square can never be negative for real numbers.
Comment: This is a direct proof. It is silly to phrase it as an argument by contradiction. Just delete the bit where you assume its false, reverse all the equals signs, write the equations in the opposite order, and boom!
Comment: this proof is sort of goofy


VSTS Build jobs freeze sporadically

Question: using visual studio team services online with an in house build agent. The build agent while running a job will randomly just freeze, the job is still active but there are no updates to the console, not errors in event logs etc. If I open the agent's _diag folder and look it will just repeat what is below until it decides to continue work.
<code>17:02:19.850546 LogFileTimer_Callback - enter (20)
 17:02:19.850546 LogFileTimer_Callback - processing job 7b9229d0-524e-4138-b6b3-33f630d109c6
 17:02:19.850546 LogFileTimer_Callback - found 0 records for job 7b9229d0-524e-4138-b6b3-33f630d109c6
 17:02:19.850546 LogFileTimer_Callback - leave
 17:02:20.100159 StatusTimer_Callback - enter (27)
 17:02:20.100159 StatusTimer_Callback - processing job 7b9229d0-524e-4138-b6b3-33f630d109c6
 17:02:20.100159 StatusTimer_Callback - leave
 17:02:20.240566 ConsoleTimer_Callback - enter (17)
 17:02:20.240566 ConsoleTimer_Callback - Inside Lock
 17:02:20.240566 ConsoleTimer_Callback - processing job 7b9229d0-524e-4138-b6b3-33f630d109c6
 17:02:20.240566 ConsoleTimer_Callback - leave
 17:02:20.755392 ConsoleTimer_Callback - enter (22)
 17:02:20.755392 ConsoleTimer_Callback - Inside Lock
 17:02:20.755392 ConsoleTimer_Callback - processing job 7b9229d0-524e-4138-b6b3-33f630d109c6
 17:02:20.755392 ConsoleTimer_Callback - leave
 17:02:20.864598 StatusTimer_Callback - enter (18)
 17:02:20.864598 StatusTimer_Callback - processing job 7b9229d0-524e-4138-b6b3-33f630d109c6
 17:02:20.864598 StatusTimer_Callback - leave
</code>
We have tried deleting the work folder, uninstalling the agent and reinstalling and it still just seems to freeze on random jobs. Any idea what else I could look into as why this is happening?
Comment: Will your build fail when this phenomenon occur? What kind of tasks will have this phenomenon?
Comment: If we wait it out, the builds will be fine ... sometimes this even occurs on releases too. So, a build job that normally takes 3 minutes ... if we wait 15ish it will finish
Answer: Just checked one log, and found these information existed in the log file here and there. Such as restore packages, upload logs, or retrieve files, etc.
These information don't mean there is an error. You may try to create a new agent on another machine to see whether this phenomenon would occur.
Comment: I will try installing the agent on another machine but I was hoping for more troubleshooting tips that I could use to help diagnose this machine. It doesn't seem to be a resource problem, the server is doing nothing while the job is also doing nothing.
Comment: As there is no error message, it's hard for us to troubleshoot this phenomenon. Try with another machine to monitor it.

VSTS Build jobs freeze sporadically


Meaning of a symbol in an orchestral score

Question: What does '+' mean in an orchestral score? This was an examination question in a music theory exam and I have been unable to find out the meaning
Comment: In _what context_?
Comment: Context! Please.
Answer: Above a note it could mean 'stopped' for a French Horn. 'Closed' or 'damped' for a hi-hat or crush cymbal. For a stringed instrument it means a left hand pizzicato.  In a chord symbol it means the basic triad has an augmented 5th, as in 'C7+'.
Did the theory paper give no further context?


How to resolve Error listenerStart when deploying web-app in Tomcat 5.5?

Question: I've deployed an Apache Wicket web-application that uses Spring and Hibernate to my Tomcat 5.5 instance. When I navigate to the Tomcat Manager interface I see that the web-application I deployed is not running. When I press 'Start' I get the following error message; "FAIL - Application at context path /spaghetti could not be started".
My catalina.log contains the following:
Apr 15, 2010 1:51:22 AM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/var/lib/tomcat5.5/webapps/spaghetti/WEB-INF/lib/jsp-api-6.0.16.jar)   - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/jsp/JspPage.class
Apr 15, 2010 1:51:22 AM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/var/lib/tomcat5.5/webapps/spaghetti/WEB-INF/lib/servlet-api-6.0.16.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Apr 15, 2010 1:51:24 AM org.apache.catalina.core.StandardContext start
SEVERE: Error listenerStart
Apr 15, 2010 1:51:24 AM org.apache.catalina.core.StandardContext start
SEVERE: Context [/spaghetti] startup failed due to previous errors

Excerpt from web.xml:

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

Any help is greatly appreciated.
Comment: Look at this: http://blog.trifork.com/2011/03/18/debugging-the-dreaded-severe-error-listenerstart-and-severe-error-filterstart-tomcat-error-messages/
Answer: I found that following these instructions helped with finding what the problem was. For me, that was the killer, not knowing what was broken.
http://mythinkpond.wordpress.com/2011/07/01/tomcat-6-infamous-severe-error-listenerstart-message-how-to-debug-this-error/
Quoting from the link

In Tomcat 6 or above, the default logger is the"java.util.logging" logger and not Log4J. So if you are trying to add a "log4j.properties" file – this will NOT work. The Java utils logger looks for a file called "logging.properties" as stated here:
  http://tomcat.apache.org/tomcat-6.0-doc/logging.html
So to get to the debugging details create a "logging.properties" file under your"/WEB-INF/classes" folder of your WAR and you're all set.
And now when you restart your Tomcat, you will see all of your debugging in it's full glory!!!
Sample logging.properties file:

org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = java.util.logging.ConsoleHandler

Comment: This is only part of the(/my) problem. The listenerStart-error occured in `catalina.out` while the stacktrace was written to `localhost-.out`. So be sure to check all of tomcat's log files for stacktraces (possibly by finding out the pid using `ps aux | grep tomcat` and finding out tomcat's open files using `lsof -p `)
Comment: Thanks!  Exactly what you've said and magically we can check the real cause of the problem and stop wasting time. :-)   Cheers.
Comment: The link is no longer active.
Answer: /var/lib/tomcat5.5/webapps/spaghetti/WEB-INF/lib/jsp-api-6.0.16.jar
/var/lib/tomcat5.5/webapps/spaghetti/WEB-INF/lib/servlet-api-6.0.16.jar

You should not have any server-specific libraries in the /WEB-INF/lib. Leave them in the appserver's own library. It would only lead to collisions in the classpath. Get rid of all appserver-specific libraries in /WEB-INF/lib (and also in JRE/lib and JRE/lib/ext if you have placed any of them there).
A common cause that the appserver-specific libraries are included in the webapp's library is that starters think that it is the right way to fix compilation errors of among others the javax.servlet classes not being resolveable. Putting them in webapp's library is the wrong solution. You should reference them in the classpath during compilation, i.e. javac -cp /path/to/server/lib/servlet.jar and so on, or if you're using an IDE, you should integrate the server in the IDE and associate the web project with the server. The IDE will then automatically take server-specific libraries in the classpath (buildpath) of the webapp project.
Comment: Thanks for the help BalusC. I haven't explicitly added the servlet-api or jsp-api to the dependencies list in my pom.xml, actually they're not listed there. Perhaps it's a dependency of one of the dependencies I have.

I'll give your suggestion a go.
Comment: I had a similar issue, in WTP within Eclipse, in `/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/...` It started after upgrading from Servlet 2.5 to 3.0, so I figured a right-click on the server, and then "Clean Tomcat Work Directory" should wipe it. But things kept coming back. Eventually, `mvn dependency:tree` showed that some dependencies were including both `javax.servlet:servlet-api:jar:2.5` and `org.mortbay.jetty:servlet-api:jar:2.5-20081211`.
Answer: I encountered this error when the JDK that I compiled the app under was different from the tomcat JVM. I verified that the Tomcat manager was running jvm 1.6.0 but the app was compiled under java 1.7.0.
After upgrading Java and changing JAVA_HOME in our startup script (/etc/init.d/tomcat) the error went away.
Comment: That was my problem exactly, thanks for this solution
Answer: Answered provided by Tom Saleeba is very helpful.
Today I also  struggled with the same error

Apr 28, 2015 7:53:27 PM org.apache.catalina.core.StandardContext startInternal
  SEVERE: Error listenerStart

I followed the suggestion and added the logging.properties file. And below was my reason of failure:

java.lang.IllegalStateException: Cannot set web app root system
  property when WAR file is not expanded

The root cause of the issue was a listener (Log4jConfigListener) that I added into the web.xml. And as per the link SEVERE: Exception org.springframework.web.util.Log4jConfigListener , this listener cannot be added within a WAR that is not expanded.
It may be helpful for someone to know that this was happening on OpenShift JBoss gear.


Twig adds an unwanted 'row' class that doesn't appear in class array

Question: My site theme includes customised templates for 4 regions in the footer, all containing identical code:
  set classes = [
    'region',
    'region-' ~ region|clean_class,
    'col-sm-12',
    'col-md-6',
    'col-lg-3',
  ]
%}
{% if content %}

    {{ content }}

{% endif %}

However, the HTML it produces varies.
In the first two regions, the class row is being inserted - despite it not appearing in the classes array:

In the last 2 regions, the row class is missing - which is the correct code I want:

I can't figure out where it's coming from, so my workaround is to include an additional step that removes the class, in the first two region templates only:

This gives the result I want, but I'm still not sure where the row class comes from and why it was being applied inconsistently??
Answer: Those 5 classes are being added using .addClass() to a pre-existing {{ attributes }} Twig object. The 'row' class must already be in the attributes object when Drupal passes it to the Twig template.
Typically, the place where something like that would happen is in your .theme file (or the parent theme's .theme file if yours is a subtheme). Look for a function like THEMENAME_preprocess_region() and see if you can find a hard-coded "row" string being thrown in there.
Alternatively, some fancier theme systems might expose configuration options in the Drupal interface that could be setting that class. You'd find those under Admin > Appearance, then in "Settings" for your active theme.
Comment: Ah, that was the culprit! My theme settings in the UI had the 'row' class being added on the first 2 footer regions.


is the inequality true??

Question: It is given that $f$ is a continuous function and $A$ a bounded set.
Is the inequality $$\sup_{x \in A} \int_a^b f(x) dx \leq \int_a^b sup_{x \in A} f(x) dx$$ true?
If it is true,could we maybe prove it,with this relation: $$\sup_{x \in A} (\sup_{x \in A} f(x))=\sup_{x \in A} f(x)$$ ?
Comment: the expression on the left side is ill-formed. x can not be a variable of the sup and a variable of the integral at the same time.
Comment: Yes,right..but $\sup_{x \in A} f(x)$ is a real number,so it does not depend from $x$,or not?
Comment: @Michael It's not ill-formed, just weird. The integral operation re-binds $x$, i.e. introduces a *new* variable that happens to also be called $x$, and *within* the integral $x$ refers to that new variable. The outter variable $x$ - the one bound by the supremem - is therefore simply unused.
Comment: I think he meant $\sup_{a,b \in A}$ on the LHS
Answer: The statement is false.
Counterexample:
$$a=0, b=1, f(x)=x, A=(-3,-2)$$
Because: Who said that $A$ has to be the integration intervall?
Answer: As states, this is false. You need to impose some restriction on $a,b$ vs. $A$ for this to hold, otherwise you can do something like $a=0$, $b=2$, $A=[0,1]$ and let $f(x) = 0$ if $x \leq 1$ and $1$ otherwise. Then $$
  \int_a^b f = 1 > \int_a^b \sup_{x\in[0,1]} f(x) = \int_a^b 0 = 0 \text{.}
$$
Also, note that $x$ isn't a free variable in the expression $\int_a^b f(x)\,dx$, so writing $$
  \sup_{x \in A} \int_a^b f(x)\,dx
$$
is a bit weird. It comes down to writing $\sup_{x \in A} c$ for some constant $c$, which is of course just $c$.
However, you do have that $$
  \int_A f(x) \,dx \leq \int_A |f(x)| \,dx \leq \int_A \sup_{x\in A}|f(x)| \,dx = |A|\cdot \sup_{x\in A}|f(x)| \text{,}
$$
where $|A|$ denoted the measure of $A$, i.e. $|A| = \int_A 1 \,dx$.
You also have that if $f(x) < g(x)$ then $$
  \int_A f(x) \,dx \leq \int_A g(x) \,dx \text{.}
$$
For $f \geq 0$ this is clear. In the general case (this is where I was confused prevously), you can split $f$ into it's positive and negative parts, i.e. write $f(x) = f^+(x) - f^-(x)$ where $f^+,f^- \geq 0$, and the same for $g$. Then, $f^+ \leq g^+$ and $f^- \geq g^-$, and thus $$
  \int_A g(x) - \int_A f(x) = \underbrace{\int_A g^+ - \int_A f^+}_{\geq 0} + \underbrace{\int_A f^- - \int_A g^-}_{\geq 0} > 0
$$
Comment: Isn't it $$\sup_{x \in [0,1]} f(x) dx=0$$?
Comment: But f is continous...
Comment: really... $\int_0^1 \sup_{x\in[0,1]} f(x) \,dx = 0$ !!!
Comment: @Michael Yeah, ups, sorry for that.

is the inequality true??


Help about cosine simulating in h2o

Question: So I'm beginning in deep learning and especially in h2o.
I tried to simulate cosine function in R, not to compute it like for example by using h2o.cos(),
But after many and many more combinations of hyperparameters, I really didn't get any serious results.
My dataset is:
x=1:3000
y=cos(x)
data=cbind(x,y)
colnames(data)<- c("C1","C2")
ind<- 1:2000
traincos<- data[ind,]
validcos<- data[2001:2500,]
testcos<- data[2501:3000,]

My model:
modelcos<- h2o.deeplearning(x= "C1",
                            y= "C2",
                            seed = 1234,
                            training_frame = as.h2o(traincos),
                            validation_frame = as.h2o(validcos),
                            keep_cross_validation_predictions = T,
                            overwrite_with_best_model = T,
                            nesterov_accelerated_gradient =T,
                            epochs = 1e3,
                            adaptive_rate = F,
                            rate = 1e-3,
                            l1=1e-5,
                            l2=1e-5,
                            max_w2 = 1,
                            distribution = "laplace",
                            stopping_rounds = 100,
                            stopping_metric = "RMSE",
                            stopping_tolerance = .0001)

Without any test, in a normal case with neural networks, we have by application on the training frame the predictions curve practically the real curve by superposition
Plot:
predcos<- as.data.frame(h2o.predict(modelcos,as.h2o(traincos)))
plot(1:2000,t(predcos),type="b", ylim = c(-1,1))
lines(1:2000,cos(x))

I tried and I tried many options (withdrawing all, rising up epochs, dropout, momentum, activation, distribution etc.). Without testing the model, I didn't get any acceptable figures. And tests are systematically inconclusive.
If I use neuralnet package, the key is to drop down the learning rate. But it doesn't work for all the dataset.
I tried to simulate logarithm too, but no results.
Please somebody could help me?
Comment: Please don't cross post to both here and the h2o mailing list.  (Here is a better place for this type of question.)
Comment: Please register &/or merge your accounts (you can find information on how to do
this in the **My Account** section of our [help]), then you will be able to
edit & comment on your own question.
Comment: Thank you Darren for your answer, it works only with hidden=c(200,200,200) c(100,100,100) with epochs 1e4; but it absolutly doesn't work to predict. It allows to copy the training frame but predictions are not good Structure the dataset with h2o.split() could be a solution, but I'm afraid for predictions...
Comment: @Boris I got chance to do some experiments, and have expanded my answer. Hope it helps.
Answer: Remember that R's cos() works in radians. So 1:3000 is a
lot of cycles, all squeezed together tightly. (Do plot(x,y) to see the training data you are giving to the deep learning model.)
However, I think the root problem you are having is that "x" is your
only predictor variable. You train on x values of 1 to 2000, then ask it
what 2500 to 3000 might give. That is rather hard.
I think you might get better results if you split the training data
randomly; see h2o.splitFrame(). Then, e.g. it might train on x=1,2,4,5,6,8
and you will ask it to predict x=3 and x=7. It might be able to learn
the relationship between x=3 and the nearby x values.
BTW, more generally, the first thing I would try is to get rid of all
your arguments, and use a default model. H2O default settings are generally good. I suppose I might increase epochs, from the default 10, given that the data is not too large:
m = h2o.deeplearning(
  "C1",
  "C2",
  as.h2o(traincos),
  as.h2o(validcos),
  epochs=1e3
  )

And then add one tuning argument at a time, to see if it makes things
better or worse.
Rather than messing with learning rates, or the more exotic parameters, the first tuning I might try is a deeper network. E.g. hidden=[200,200,200] instead of the default [200,200].

UPDATE:
I did a few experiments. I also got poor results, at least until I tried all the ideas together. Here are some of the things I tried, and what happened:
Choosing A Tighter Region
Instead of x = 1:3000 I tried:
x = seq(0, 30, length.out = 3000)

This gives just over 3 full cycles.
This was the thing that made the biggest difference.
Deeper Network:

  epochs = 1000,
  activation = "Tanh",
  hidden = c(40,40,40,40,40)

The switch to Tanh was just because I got numerical instability.
This didn't really help when x was 1:3000, but did when x was just 3 cycles. However I didn't experiment further to narrow down what is optimal. It did take 600 to 700 epochs to get the best validation error.
Some Hints:
x2 = x^2
x3 = x^3
x4 = x^4
x5 = x^5

x^3, x^5 and x^7 should be enough to give a good prediction from -PI to +PI (See http://pages.pacificcoast.net/~cazelais/187/maclaurin-sin.pdf). Pretending to be honest I tried just the first 5 powers. I then later also added x^6 and x^7.
The default model with these hints was no better than without them. But they did help the deeper model, and the deep model was better with x^6 and x^7 than without.
With the hints up to x^7, and using the deep network, and using just 3 cycles, this was the best result I got:

(green is on training data, blue is on validation data, red is the test data.)
Using splitFrame
I only briefly experimented with this; judging from the score history, it was not learning very well. However I only tried it when x=1:3000, so let's try it with my last experiment... The split code:
x = seq(0,30,length.out = 3000)
y = cos(x)
x2 = x^2
x3 = x^3
x4 = x^4
x5 = x^5
x6 = x^6
x7 = x^7
data = as.h2o(data.frame(y, x, x2, x3, x4, x5, x6, x7))
parts = h2o.splitFrame(data, c(0.67, 0.167))
train = parts[[1]]
valid = parts[[2]]
test = parts[[3]]

The split ratios mean I am still training on 2000 values, using 500 for validation, and 500 for test.
Then the model building:
m_deep = h2o.deeplearning(2:ncol(data), "y",
                          training_frame = train,
                          validation_frame=valid,
                          epochs = 1000,
                          activation = "Tanh",
                          hidden = c(40,40,40,40,40)
                          )
m_deep
h2o.performance(m_deep, test)
plot(m_deep)

You will notice the MSE is now nicely low, and the score history graph looks satisfying too.
Finally predictions and plotting:
m=m_deep

# The -1 in next few lines means exclude the answer

pt = as.data.frame(h2o.predict(m, train[,-1]))
ptdata = data.frame(as.data.frame(train$x),y=pt)

pv = as.data.frame(h2o.predict(m, valid[,-1]))
pvdata = data.frame(as.data.frame(valid$x),y=pv)

p = as.data.frame(h2o.predict(m, test[,-1]))
pdata = data.frame(as.data.frame(test$x),y=p)

plot(as.data.frame(data[,c(2,1)]), type="p",pch=".")
points(ptdata, col="green",pch=".")
points(pvdata, col="blue",pch=".")
points(pdata, col="red",pch=".")

The chart shows predictions on unseen data is basically as good as on training data. It does show that it is not very good at predicting the extreme values (as it never saw enough examples of them?):

Help about cosine simulating in h2o


JQuery ajax / PHP arrays interactions

Question: When I load this code in my browser, there is just an alert window ('

I just want to get multiple elements from my PHP file.
main.html:
<!DOCTYPE html>

<button>clic</button>

And test.php:
<?php
$data = array();
require ('steamauth/steamauth.php');
if(!isset($_SESSION['steamid'])) {
$data[0] = "test00";
$data[1] = "test01";
echo $data[];
}  else {
include ('steamauth/userInfo.php');
$data[0] = "test0";
$data[1] = "test1";
echo $data[];
}
?>

Thank you very much
Nathan
Comment: `echo $data[];` Is incorrect (if you have error reporting enabled you'll be able to see the response error). `json_encode` then `echo`.
Comment: `echo $data[];` will also throw a `FATAL ERROR Cannot use [] for reading on line number x`
Comment: When you want to display a array, you have to write print_r() instead of echo.
Comment: @Melody print_r() is not such a good choice to transfer data between 2 languages... It's useful for a quick look while debugging.
Comment: With 'echo json_encode($data);', I still have the same error (pop up '<b')
Comment: @NathanLoisel are you parsing it on JS side via `JSON.parse`?
Comment: @Jeff yes you are right, but it works :-)
Answer: Your JQuery looks fine, however if you're targeting $("button"), this will listen for all buttons on that page. Consider giving it an ID. For this example I updated it to this:
$(document).ready(function(){
    $("button").click(function(){
        $.get("test.php", function(data){
            alert(data.foo + data.bar);
        });
    });
});

Inside your PHP file, you're not outputting a JSON encoding result. You need to add the content type to the header() and then use json_encode(). For this example, for maintenance, I did not use an assoc array.
header('Content-Type: application/json');
session_start(); // Your missing this unless Steamauth.php contains it

$data = array();
require ('steamauth/steamauth.php');

if(!isset($_SESSION['steamid'])) {
    $data['foo'] = "test00";
    $data['bar'] = "test01";
    echo json_encode($data);
} else {
    include ('steamauth/userInfo.php');
    $data['foo'] = "test0";
    $data['bar'] = "test1";
    echo json_encode($data);
}

Note, if the steamauth does not contain session_start() then $_SESSION will be unset.

JQuery ajax / PHP arrays interactions


How to calculate the distance between two points on a unit sphere?

Question: I have got this formula for spherical distance i.e. istance of the shortest path along the surface of the sphere on the wolfram alpha site:-
http://mathworld.wolfram.com/SphericalDistance.html
But is there any reference or standard textbook where I can get this?
Answer: It's not really a complex formula to prove.
If you have two points on a sphere of radius $r$, the shortest distance between them lies on a great circle - a circle with the same radius and center as the sphere, that passes through those two points.
What we want to find is now the distance between two points on a circle of radius $r$, following the circle. By definition, that's the angle between the vectors formed from the center to each of these points, in radians, times the radius of the circle.
Now, how do we find that angle? Well, we remember that the dot product between two vectors $A$ and $B$ can be written as $A \bullet B = \|A\|\cdot\|B\|\cdot cos(\theta)$, with theta being the angle between the two vectors.
The vectors are of norm r (because they're on the circle of radius r), so we have $cos(\theta) = \frac{A \bullet B}{r^2}$.
Hence the distance you're looking for is $cos^{-1}(\frac{A \bullet B}{r^2}) \cdot r$
Comment: Thank you! It was really helpful, but do you have any reference in mind i.e. any standard textbooks? Actually I have used this concept in a paper but this is not the focus, so just want to show a reference instead of proving it.
Comment: I'd suggest  http://www.navlab.net/Publications/A_Nonsingular_Horizontal_Position_Representation.pdf , it seems to be what you are looking for, as a reference (this is the one the wikipedia article quotes, eh).
Other than that, no idea for a real reference.. I personally would state it and put the proof in an appendix if the reader is curious for a demonstration.