Opening of Bach's mass in b minor: why does it make sense?

Question: The opening harmonic progression of the B minor mass:
Bm moves to a C# half-diminished 7th — a ii7 chord, nothing unusual yet. To put it in root position is a bit unconventional for the style, but the stepwise motion in the bass is worth it. A common predominant chord.
Instead of the expected dominant, however, Bach throws in a V7-iv chord, a B7. This does move to Em as expected, but to shift from a ii7 to a secondary dominant isn't something you see very often, especially not a V7-iv.
The rest of the opening is pretty conventional, with a V7, i, VI, very brief ii7, and finally resolving to a V. I'm mainly asking about the ii7 to V7-iv, which seems like it should sound very abrupt, yet it does not. Why does this progression make sense to our ears?
Answer: Structurally, the second chord is not a C♯ half-diminished seventh but an A♯ diminished seventh in first inversion.  The B in the second soprano is a suspension.  The harmonic rhythm is in half notes but with some embellishment.
The first four measures are basically
i          viiº(6)
V(6/5)/vi  iv
V(6/5)     i
iv(6)      V

or

Bm      A♯º7/C♯
B7/D♯   Em
F♯7/A♯  Bm
Em/G    F♯

This fits a classic functional harmonic analysis: tonic to dominant, secondary dominant of subdominant to subdominant, dominant to tonic, subdominant to dominant.
But, perhaps more significantly, the theory of functional harmony was only in its infancy in Bach's day.  Jean-Philippe Rameau was promoting the innovative idea that E-G-C was the same chord as C-E-G, but it hadn't yet reached broad acceptance, and in fact Bach was resistant to it.
Rather, Bach was coming from the tradition of partimenti, of which a prominent element was the "rule of the octave," essentially a template for harmonizing ascending and descending scales, often with a series of suspensions.  Typically, those suspensions are embellishments of a series of stepwise 6/3 chords (except the first and last, because the 6/3 chord isn't stable).  Here, Bach gives us a stepwise ascending bass with the figures
8     7
7     6
(7/5) 6
9     8

Ignoring the embellishments, that's
[5/3]
6
6
[5/3]

...bog standard stuff.
Comment: *in fact Bach was resistant to it.* An interesting thesis! Do we have historical evidence of this?
Comment: @AlbrechtHügli apparently CPE wrote about Rameau's theory while noting that he (CPE) and his father (JS, of course) disagreed with it.  But I haven't found a primary source nor a secondary source with a citation.  I did scan and search the _Versuch über die wahre Art das Clavier zu spielen_ without finding the name "Rameau," but I haven't found the whole text in a single file, so I'm not confident that I've searched it effectively, and furthermore it is of course possible that this _is_ the document in which he said this but that he didn't mention Rameau by name.
Comment: I was probably misunderstanding ... you don't man that Bach was
Comment: @AlbrechtHügli that comment appears to be incomplete.  Is it?
Comment: A bit of a nitpick, I think there's a little mistake in the functional harmonic analysis. vii°(6) implies just a diminished chord. I think it would be more accurately shown as vii°(6+ 5) to show the 7th G and the sharpened A.
Answer: As phoog already pointed out the second chord is not in fact C# half diminished, but A# diminished with root suspension, effectively the dominant 7 b9 with omitted root.
But this is not actually what makes the progression work (as it works totally fine if you do keep that b on the second chord). What makes this work is the ambiguity between the tonic b minor and a secondary tonic e minor. Now, we usually consider the half diminished 7 chord a characteristic chord, as it only occurs in one position in a diatonic scales. But if you consider a melodic minor scale you will actually see that a natural half diminished 7 happens on the raised VI. And this is exactly the type of movement we have here:
The bass forms a line from B to E using the notes of E melodic minor. And the chords on top somehow do represent that. This is quite a strong move, as is forms a strong cadence towards that E minor, creating lots of tension.
Comment: Glad someone agrees.
Answer: It's because of the A-natural passing tone in the tenor, combined with smooth voice leading.
A-natural is native to E minor, which is where we're headed in this brief segment, and the brief presence of a i7 chord slightly undermines the sense of B minor. (This, as opposed to Bach using A# as the leading tone to strongly confirm B minor.) This subtly prepares the ear for the upcoming E minor chord, even whispering that we might be in the key of E minor, starting on its v chord.
What's more, the bass outlines ^5 - ^6 - ^7 - ^1 in E minor, while the soprano outlines b^7 - ^1 - ^2 - ^3. The parallel between the soprano and bass gives the effect of v - vi - viio - i in E minor.

D  E  F#  G
B  C# D#  E

In fact, the apparent ii7 chord — the result of the suspension of B from the first chord — is also the vi7 chord in E minor. So the progression can be interpreted as v(7)  vi7  viio7  i in E minor.
Below is a reduction showing the analysis in both B minor and E minor.
Comment: We're not headed to E minor; E minor is present only as the subdominant of B minor: the entire four measure section moves decisively to the dominant of B minor to introduce the fugue, which is very squarely in B minor.  Also the A natural doesn't make this harmonic progression work, as you can see by substituting a B there for the A.  You lose the rich dissonance of the minor seventh chord, but the progression from i to iv still works perfectly well from a functional standpoint.
Comment: @phoog You've misunderstood. I don't mean we're headed to E minor as a cadence point or modulation. I just meant that in the limited segment presented by OP, we're going to wind up on an E minor chord. And your own comment makes my point about the A — without it, the progression loses some of its richness. The sound of the progression has to be considered in any analysis. Analysis is interpretive as much as rule-based. However, it would be clearer, I think, if I put the ii7 in parentheses and comment on the suspension. I left it as is to offer consistency with the OP's understanding.
Comment: Fair enough.  I read it in the sense one might find in a classical or romantic sonata where it would mean "preparing for a new large section in [the named key]."  It also occurred to me that you could see the whole first two measures as an extended `iv` subdominant, followed by the third and fourth measures as an extended `V` dominant, followed by the fugue opening in `i`.  So I'd remove my downvote but the answer hasn't been edited since then so I can't.  If you do edit it, ping me so I can change my vote.
Comment: @phoog Done. I also tried to clarify the limits of the analysis to the two measures rather than in the larger context. Other clarifications I'll get to a bit later. But you've understood me in suggesting the possibility of viewing the passage as an extended subdominant.
Comment: @phoog I think I've got this cleaned up now. Should be more clear.


JFXTextField not usable with JxBrowser

Question: Im a littlebit stuck with that.
Im using JxBrowser for my JavaFX project.
Im also using JFoenix to push the look.
Now when i browse to some sites, the URLBar which is a JFXTextField becomes unusable. I cant write anymore in it.
But the default JavaFX contextmenu still works.
Whats interesting is that key listeners on the website, like F for fullscreen on youtube still works.
To fix this, the only way that works, is to click a completely different Window on the PC.
For example im clicking to IntelliJ than back to the JavaFX frame, than its working again. As i wrote its only on specefic sites.
No Exceptions are thrown.
If its maybew important, im using 2 TabPanes, One for the Tab and another for the Content.
Do someone has an idea?
The class that generates the Tab:
package de.liz3.liz3web.browser;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import de.liz3.liz3web.gui.controller.MainController;
import javafx.application.Platform;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.input.KeyCode;

import java.util.Vector;

/**
* Created by Liz3 on 22.02.2017.
*/
public class TabManager {

private Vector<BrowserTab> activeTabs;
private MainController controller;

public TabManager(MainController controller) {
    this.controller = controller;

    activeTabs = new Vector<>();

    controller.getUrlField().setOnMouseClicked(event -> controller.getUrlField().requestFocus());
    TabPane head = controller.getHeaderTabPane();
    controller.getUrlField().setOnKeyReleased(event -> {

        if (event.getCode() == KeyCode.ENTER) {

            selectedBrowserTab().browseOrSearch(TabManager.this.controller.getUrlField().getText());
        }
    });
    controller.getBackBtn().setOnAction(event -> selectedBrowserTab().getEngine().goBack());
    controller.getForwardBtn().setOnAction(event -> selectedBrowserTab().getEngine().goForward());
    head.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {

        if (newValue == controller.getNewTab()) {
            newTab(null);
            return;
        }
        controller.getMainTabPane().getSelectionModel().select(getContentTab(newValue));

        controller.getUrlField().setText(selectedBrowserTab().getCurrentUrl());
    });
}

public void newTab(String url) {

    new Thread(() -> {

        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        Tab headerTab = new Tab("New Tab");
        Tab contentTab = new Tab();
        contentTab.setContent(view);
        BrowserTab t = new BrowserTab(browser, view, contentTab, headerTab);
        activeTabs.add(t);

        Platform.runLater(() -> {

            controller.getHeaderTabPane().getTabs().add(headerTab);
            controller.getMainTabPane().getTabs().add(contentTab);
            controller.getHeaderTabPane().getTabs().remove(controller.getNewTab());
            controller.getHeaderTabPane().getTabs().add(controller.getNewTab());
            controller.getMainTabPane().getSelectionModel().select(contentTab);
            controller.getHeaderTabPane().getSelectionModel().select(headerTab);
            controller.getUrlField().setEditable(true);
            controller.getUrlField().setText("");
            t.browseOrSearch(url);

        });
    }).start();

}

public BrowserTab selectedBrowserTab() {

    for (BrowserTab tab : activeTabs) {

        if (tab.getHeaderTab() == controller.getHeaderTabPane().getSelectionModel().getSelectedItem()) {
            return tab;
        }
    }
    return null;
}

public Tab getSelectedHeaderTab() {

    return controller.getHeaderTabPane().getSelectionModel().getSelectedItem();
}

public Tab getSelectedContentTab() {

    return controller.getMainTabPane().getSelectionModel().getSelectedItem();
}

public Tab getHeaderTab(Tab contentTab) {

    for (BrowserTab tab : activeTabs) {

        if (tab.getContentTab() == contentTab) {
            return tab.getHeaderTab();
        }
    }
    return null;
}

public Tab getContentTab(Tab headerTab) {

    for (BrowserTab tab : activeTabs) {

        if (tab.getHeaderTab() == headerTab) {
            return tab.getContentTab();
        }
    }
    return null;
}

public MainController getController() {
    return controller;
}

public Vector<BrowserTab> getActiveTabs() {
    return activeTabs;
}
}

The Browser class:
package de.liz3.liz3web.browser;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.events.*;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import de.liz3.liz3web.Main;
import javafx.application.Platform;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Tab;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by Liz3 on 21.02.2017.
 */
public class BrowserTab {

private Browser engine;
private BrowserView view;
private Tab contentTab;
private Tab headerTab;
private String currentUrl;

public BrowserTab(Browser engine, BrowserView view, Tab contentTab, Tab headerTab) {
    this.engine = engine;
    this.view = view;
    this.contentTab = contentTab;
    this.headerTab = headerTab;
    this.currentUrl = "";

    setUpTab();

}

public void browseOrSearch(String address) {

    if (address == null) {
        return;
    }

    if (address.startsWith("http://") || address.startsWith("https://")) {

        this.engine.loadURL(address);

        return;
    }
    if (!address.contains(" ") && address.contains(".")) {

        address = "http://" + address;
        this.engine.loadURL(address);
        return;
    }

    String encode = null;
    try {
        encode = URLEncoder.encode(address, "utf-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    this.engine.loadURL("https//google.com/search?q=" + encode);

}

private void setUpTab() {

    this.headerTab.setClosable(true);
    this.headerTab.setOnCloseRequest(new EventHandler<Event>() {
        @Override
        public void handle(Event event) {

            Main.tm.getController().getMainTabPane().getTabs().remove(contentTab);
            Main.tm.getActiveTabs().remove(this);

        }
    });
  this.engine.addTitleListener(titleEvent -> Platform.runLater(() -> BrowserTab.this.headerTab.setText(titleEvent.getTitle())));
    this.engine.addLoadListener(new LoadListener() {
        @Override
        public void onStartLoadingFrame(StartLoadingEvent startLoadingEvent) {

            Platform.runLater(() -> {
                BrowserTab.this.currentUrl = BrowserTab.this.engine.getURL();
                if (Main.tm.getSelectedHeaderTab() == BrowserTab.this.headerTab) {

                    Main.tm.getController().getUrlField().setText(BrowserTab.this.currentUrl);
                }
            });

        }

        @Override
        public void onProvisionalLoadingFrame(ProvisionalLoadingEvent provisionalLoadingEvent) {

        }

        @Override
        public void onFinishLoadingFrame(FinishLoadingEvent finishLoadingEvent) {
            Platform.runLater(() -> {

                BrowserTab.this.currentUrl = BrowserTab.this.engine.getURL();
                if (Main.tm.getSelectedHeaderTab() == BrowserTab.this.headerTab) {

                    Main.tm.getController().getUrlField().setText(BrowserTab.this.currentUrl);
                }
            });
        }

        @Override
        public void onFailLoadingFrame(FailLoadingEvent failLoadingEvent) {

        }

        @Override
        public void onDocumentLoadedInFrame(FrameLoadEvent frameLoadEvent) {

        }

        @Override
        public void onDocumentLoadedInMainFrame(LoadEvent loadEvent) {

        }
    });
    this.engine.setPopupHandler(popupParams -> {
        Platform.runLater(() -> Main.tm.newTab(popupParams.getURL()));

        return null;
    });

}

public Browser getEngine() {
    return engine;
}

public BrowserView getView() {
    return view;
}

public Tab getContentTab() {
    return contentTab;
}

public Tab getHeaderTab() {
    return headerTab;
}

public String getCurrentUrl() {
    return currentUrl;
}
}

Answer: By default JxBrowser is running in HEAVYWEIGHT rendering mode. It's not recommended to use HEAVYWEIGHT rendering mode in applications like yours with tabs and layers. Please use LIGHTWEIGHT rendering mode. It should work without any issues:
Browser browser = new Browser(BrowserType.LIGHTWEIGHT);
BrowserView view = new BrowserView(browser);

Comment: Sadly with that its not even rendering.
Im just putting the Browser view into the tab content. i have already tried things like, making all actions on the UI Thread, or putting the browser into a pane and the pane into the tab.
Comment: In heavyweight rendering mode, the library embeds Chromium native window into your JavaFX window which may cause focus issues. Please try lightweight rendering mode and let me know the results.
Comment: Yes thats it, in lightweight its not working as said in my comment above.
In Havyweight its rendering just fine

JFXTextField not usable with JxBrowser


how to check whether a file exists before creating it

Question: I am creating an xml file. I need to check first if the file exists or not. If the file does not exist, create it and add the data cmg from a .cs file.
If the file exists, don't create the file just add the data cmg from a .cs file.
My code looks like this:
string filename="c:\\employee.xml";
XmlTextWriter tw=new XmlTextWriter(filename,null);//null represents
the Encoding Type//
tw.Formatting=Formatting.Indented; //for xml tags to be indented//
tw.WriteStartDocument(); //Indicates the starting of document (Required)//
tw.WriteStartElement("Employees");
tw.WriteStartElement("Employee","Genius");
tw.WriteStartElement("EmpID","1");
tw.WriteAttributeString("Name","krishnan");
tw.WriteElementString("Designation","Software Developer");
tw.WriteElementString("FullName","krishnan Lakshmipuram Narayanan");
tw.WriteEndElement();
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();

so next time we add data to file we need to check if the file exits and add data to xml file
and as we have made empID as a primary key, if user tries to make duplicate entry we need to avoid

Is this possible to do?
Comment: Though your question is about checking existence of a file, I can't help but to notice you are adding data to an XML file if it exists. If an XML file exists, you wouldn't want to use the same method to 'append' to the existing file. Just a thought.
Answer: if (!File.Exists(filename))
{
    // create your file
}

or
if (File.Exists(filename))
{
    File.Delete(filename);
}

// then create your file

File class is in System.IO namespace (add using System.IO;  to your file)
Comment: Do check or at least prepare for File Permission exception if your web app is indeed writing a file at the C: root.
Answer: You can't append records to an XML file, you have to read the file and then rewrite it.
So, just check if the file exists, and read the records from it. Then write the file including all previous records and the new record.
Answer: Have a look at the File.Exists method here
Answer: Testing for existance of a file before attempting to create it inherently is subject to a "things change after check" race condition. Who can guarantee you that your application isn't preempted and put to sleep for a moment after you checked, someone else creates/deletes that file, your app gets to run again and does exactly the opposite of what you intended ?
Windows (as well as all UN*X variants) supports file open/create modes that allow to perform that create-if-nonexistant/open-if-existant operation as a single call.
As far as .NET goes, this means for your task (create an XML file) you'd first create a System.IO.FileStream with the appropriate modes, see http://msdn.microsoft.com/en-us/library/system.io.filemode.aspx and then pass that stream to the XmlWriter constructor. That's safer than simply performing an "exists" check and hoping for the best.


How do I decide my image has bright text or dark text? [LabVIEW]

Question: I'm working on a Text Extraction algorithm in which I need some assistance with thresholding an image. My development platform is LabVIEW 2015 and I'm using the "AutoBThreshold2.vi" from Vision Development Module 2015. I decided to go with Otsu's Algorithm for thresholding which is available as "Inter Class Variance" Method. Now, The problem is that I need to specify the "Look for" option to extract the text! Unfortunately, my input images will not always be same.
Kindly refer the attached source code along with sample images. My question is that Is there any way to find whether the image has Dark objects/Bright Objects on Dark Background/Bright Background? Meanwhile I'm also playing with Histogram to find out the BG & FG type!

I'd really appreciate your help...
Comment: @CrisLuengo: How do you copy Labview code here? It would be just a screenshot.
Comment: @sweber: according to the [tag wiki](https://stackoverflow.com/tags/labview/info) one should use a VI Snippet (whatever that is, I used Labview once, 20 years ago).
Comment: Basic internet security protocols for anyone using a computer: Do never, ever, download a zip file that a stranger offers in the internet.
Comment: background usually covers more pixels than text in any normal text document. just count the pixels
Comment: Please read [mcve]. The code needs to be copied *into* the question. Don't post links to the code.
Comment: @AnderBiguri, Sorry! I didn't know that! Anyway I have replaced the link with VI snippets now!
Comment: @sweber, Thanks for suggesting the VI snippet idea!..
Answer: With the help of NI forum, I'm able to solve this problem.

https://forums.ni.com/t5/LabVIEW/Auto-Thresholding-an-image-for-text-extraction/m-p/3904533#M1108133
Answer: Use equalize vi to solve this problem before thresholding, see below image to find it.


Can an n-by-n matrix have rank <n and still have no linearly dependent rows or columns?

Question: I have a school assignment which asks me to create a 49x49 matrix of rank 47 and none of the columns or rows are allowed to be parallel.
According to my textbook it says that the rank of a matrix is defined as the maximum amount of linearly independent columns of the matrix. Doesn't this mean that the kind of matrix they are asking me to create is impossible? Two columns that are parallel are also linearly dependent, right?
What am I misunderstanding here?
Comment: The three vectors $(1,0), (0,1), (1,1)$ are linearly dependent although no two of them are parallel.
Comment: Yes, 2 parallel columns are dependent. But if three (or 48, or 49) columns are dependent *(as a whole)*, two of them are not necessarily dependent.
Answer: A matrix of size $n \times n$ and rank $m \le n$ has $(n-m)$ eigenvalues equal to zero. Then it means $(n-m)$ lines are a linear combination of the other $m$.
$$\mathbf{l}_{i} = \sum_{j=1}^{m} a_{ij} \cdot \mathbf{l}_{j} \ \ \ \ \ \forall \ i=m+1,  \  \cdots,  \ n$$
A specific case of linear combination is when one line is parallel to other line. So, there's a constant $a_{ij}$ such
$$\mathbf{l}_i = a_{ij} \cdot \mathbf{l}_{j}$$
To create a rank-deficient matrix with no parallel lines, just select two (or more) independent lines and do
$$\mathbf{l}_{i} = a_{ij} \cdot \mathbf{l}_{j} + a_{ik} \cdot \mathbf{l}_{k}$$
With $a_{ij} \ne 0$, $a_{ik} \ne 0$ and $j \ne k$


State not updating in Component

Question: Hey I am trying to create a simple to-do list and I have added the components necessary. However, the state is not being updated in the Title {this.state.data.length} and the TodoList {this.state.data}. A Codepen and the relevant code is below.
https://codepen.io/skasliwal12/pen/BREYXK
const TodoForm = ({addTodo}) => {
  let input;
  return (

    <input ref={node => {input = node;}} />
    <button onClick={(e) => {
        e.preventDefault();
        addTodo(input.value);
        input.value='';
    }}> +
    </button>

  );
};

const TodoList = ({todos}) => {
    let todoNodes = todos.map(todo => {
        return
{todo}

    });
    return
 {todoNodes}
;
}

const Title = ({todoCount}) => {
  return (

To-do App {todoCount} items

  );
}

class TestApp extends React.Component {
  constructor(props) {
    super(props);
    this.state = { data : [] }
  }

  addTodo(val) {
    let todo = {text: val}
    this.state.data.push(todo);
    this.setState = ({data: this.state.data});
    console.log('state updated?')
  }

  render(){
    return (

        <Title todoCount={this.state.data.length}/>
        <TodoForm addTodo={this.addTodo.bind(this)}/>
        <TodoList todos={this.state.data}/>

    );
  }
}

ReactDOM.render(<TestApp />, document.getElementById('root'));

Answer: Quite simply it is important that you DO NOT MUTATE the state like you are doing here
this.state.data.push(todo);

It is hard to debug and adds side effects that are hard to keep track of. Following your approach you should copy the state to a var, update that var and then pass it as the new field in your state. Which could work but it's also something I do not recommend. A general good approach is to to compute the new state based on the old one
// this.state.data.push(todo); You can remove this line
this.setState(prevState => ({ data: prevState.data.concat(todo) }))

This will fix your issue and avoid mutating the state, which is something you should never do, only update the state using the setState method.
I also updated your TodoList which was not displaying properly, you have to access the text field of the todo in order to show something.
const TodoList = ({todos}) => {
    let todoNodes = todos.map(todo => {
        return
{todo.text}

    });
    return
 {todoNodes}
;
}

https://codepen.io/anon/pen/MmRVmX?editors=1010

State not updating in Component


Can I hide or ignore symlinks with Samba

Question: I'm trying to back up our company's website using a regular old windows copy utility.  Website is riddled with symlinks and if they were all followed would bloat the backup to 400% of it's normal girth.
I've told Samba not to follow symlinks, but copying the folders errors out really quick.  Maybe it's the copy util's fault. Maybe it's the server's fault (har har).  Anybody thought of a workaround, besides chmodding all my symlinks to make them transparent to the backup user?
Answer: If Samba is displaying but not following the symlinks and it's just windows explorer that can't handle the resulting permission denied errors, perhaps you can just use the command line  (e.g. xcopy /S or cwRsync) where the errors will be non fatal.  Alternatively if you can also connect to the server via SSH, SCP/SFTP clients like WinSCP are certainly smart enough to actually handle symlinks sanely as part of a recursive transfer.
Comment: +1 Something like SCP or rsync is much better suited to this problem.


Clarification: how to get the following asymptotics

Question: I'm having some trouble justifying some steps in a paper.

Let $a_n$ be an increasing sequence of integers satisfying $n! \le a_n \le 2(n!)$, and let $f:\mathbb{N} \to \mathbb{N}$ be a function satisfying
  $$f(2a_n+1) = \frac{1}{2}(2a_n+1)(2a_n+2)+na_n-\frac{1}{2}(n-1)(n-2).$$
Let $l:=2h_n+1$. The paper claims $$f(l) \le \frac{l(l+1)+l\cdot g(l)}{2} \tag{*},$$
  where $g(l) \sim \frac{\log l}{\log \log l}$ as $l \to \infty$. Moreover,
  $$f(l) \ge \frac{l^2 + l \cdot O\left(\frac{\log l}{\log \log l}\right)}{2}.\tag{**}$$

My attempts:
If we replace $2a_n+1$ with $l$, we have
$$f(l) \le \frac{l(l+1)}{2} + na_n \le \frac{l(l+1)}{2}+\frac{l\cdot n}{2}.$$
So we need something like $n \sim g(l)$. I have an intuition for where the expression for $g$ comes from, but it is extremely imprecise. I use the symbol $\leftrightsquigarrow$ to denote similar growth.
\begin{align*}
l:=2a_n+1 &\leftrightsquigarrow n!\\
l &\leftrightsquigarrow n^n & \text{Stirling's approx.?}\\
\log l &\leftrightsquigarrow n \log n\\
\log \log l &\leftrightsquigarrow \log (n \log n)\\
g(l)\sim \frac{\log l}{\log \log l} &\leftrightsquigarrow n\frac{\log n}{\log (n \log n)} \sim n
\end{align*}
There are several issues:

something is lost in Stirling's approximation (it is not true that $n^n \sim n!$).
I am not sure how to rigorously express $\leftrightsquigarrow$. In particular, there is a constant factor lurking somewhere in the work toward $g(l) \leftrightsquigarrow n$, but (*) shows no such constant.

As for the lower bound, I have no idea. The only difference from the upper bound is the change of $l+1$ to $l$ and the $\sim$ to $O(\cdot)$. This suggests that both bounds are pretty tight...
Answer: By setting $\nu = 2a_n+1$ and taking $\eta$ as the inverse map of $\theta:n\to a_n$ we have:
$$f(\nu)=\frac{\nu(\nu+1)}{2}+\frac{\nu-4}{2}\eta((\nu-1)/2)-\frac{1}{2}\eta((\nu-1)/2)^2-1,$$
hence we just need tight asymptotics for the $\eta$ function. Since:$$\left(\frac{m+1}{e}\right)^{m+1}\geq 2m!\geq\theta(m)\geq m!\geq\left(\frac{m}{e}\right)^m,$$
we must have:
$$\left(\frac{\eta(t)}{e}\right)^{\eta(t)}\leq t\leq \left(\frac{\eta(t)+1}{e}\right)^{\eta(t)+1},$$
so:
$$ \eta(t)\left(\log\eta(t)-1\right)\leq \log t\leq (\eta(t)+1)\left(\log(\eta(t)+1)-1\right).\tag{1}$$
To estimate the root of $u(\log u-1)-\log t=0$ we can exploit the convexity of the LHS.
The Newton's method gives that the root of such equation is:
$$\frac{\log t}{\log \log t}+O\left(\frac{\log t\,(\log\log\log t)}{(\log \log t)^2}\right),$$
hence
$$ \eta(t)=\Theta\left(\frac{\log t}{\log \log t}\right)$$
due to $(1)$.
Comment: The bound $2m! \le ((m+1)/e)^{m+1}$ only works for large $m$, correct? Also, did you get the precise asymptotics from Newton's method be obtained computationally or analytically?
Comment: Yes, that bound works only for (moderately) large $m$. The asymptotics for the Newton bound are derived by starting the iteration in $x_0=\frac{\log t}{\log \log t}$ and exploiting the fact that $\frac{d}{du}u(\log u-1)=\log u$.


Why do I get "Undefined variable error" when returning an array from a class?

Question: I am experiencing "Undefined variable" error when returning an array from a class, but I do not understand why this would happen.
If I print_r($order_ids) from within the class, there is no issue. The issue only occurs when I try to print_r($order_ids) from outside the class.
CLASS FUNCTIONS FILE
function getOrderIds($start_order, $end_order) {

    $conn = new Database();

    $sql = "SELECT order_id FROM oc_order WHERE order_status_id = '17' AND order_id BETWEEN '$start_order' AND '$end_order'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $order_ids[] = $row['order_id'];
        }
    }

    return($order_ids);

}

CALL
$order = new Order();
$order->getOrderIds($start_order, $end_order);

print_r($order_ids);

RESULT
Notice: Undefined variable: order_ids

Comment: Missing: $order_ids = $order->getOrderIds($start_order, $end_order); // ?
Comment: I suggest to change `function getOrderIds($start_order, $end_order) {
` into `function getOrderIds($start_order = null, $end_order = null) {
`. In case one of the two variables are empty, you can return false.
Answer: $order = new Order();
$order_ids = $order->getOrderIds($start_order, $end_order);

print_r($order_ids);

Comment: Thank you, this works, but I'm not sure why my method did not work? My understanding was that the `return` function would define $order_ids for me. Do you know where I am going wrong with my understanding of `return`?
Comment: your variable $order_ids is defined in getOrderIds function of your class, it isn't reachable outside of this function. The return of a function returns you the value of the variable, not the variable


Validation set in presence of cross-validation

Question: I am new to machine learning and want to ask regarding a confusion I have. I have a data set which is labeled and I want to do supervised learning.
My question is related to cross-validation and validation set. I checked other SO question like this and a paper from academia.
I understand that I need training and test sets. I have labeled set of data, so I see that cross-validation may be an option for me to check my classifier accuracy (correct me if I'm wrong on the accuracy part). I want to ask if I plan to do the above, will validation set still have any significance ? I read that it is used to try different settings for the algorithms. If I do try different settings e.g. if I try Naive Bayes and do 10 fold cross-validation, and also want to try some settings for Naive Bayes, what is will be validation set in this situation or is a validation set even needed ?
Answer: I will borrow an example from Data Mining Techniques (by M Berry and G Linoff) which will hopefully help explain why you still need a true hold-out (validation) set which should never be touched during the model building process, and only to be used to evaluate the accuracy of the final model that you chose from the k-fold cross-validation process.
Imagine yourself back in the fifth grade. The class is taking a spelling test. Suppose that, at the end of the test period, the teacher asks you to estimate your own grade on the quiz by marking the words you got wrong. You will give yourself a very good grade, but your spelling will not improve.
If, at the beginning of the period, you thought there should be an 'e' at the end of "tomato", nothing will have happened to change your mind when you grade your paper. No new information have entered your system. You need cross validation.
Now, imagine that at the end of the test the teacher allows you to look at the papers of several neighbors before grading your own. If they all agree than "tomato" has no final 'e', you may decide to mark your own answer wrong. If the teacher gives the same quiz tomorrow, you will do better. But how much better? If you use the papers of the very same neighbors to evaluate your performance tomorrow, you may still be fooling yourself. If they all agree that "potatoes" has no more need of an 'e' than "tomato", and you have changed your own guess to agree with theirs, then you will overestimate your actual grade on the second quiz as well. That is why the the final validation set (i.e., the hold-out) should be different from the training/cross-validation set.
In summary, you can use cross-validation to select the best model (i.e., the best modeling technique, best parameters, and the optimal number of features). And to evaluate how your model will perform in future, you can check its accuracy on the validation set.
Answer: If you optimize your parameters (if any) via cross validation on the training set, then you can simply apply this model to the validation set to perform its accuracy. So in this case the need for a validation set might be for comparison with existing methods.


Array com capacidade dinâmica e como invalidar outros caracteres na leitura

Question: Fiz o código abaixo porém encontrei 2 problemas:

O valor do vetor está setado como 100 e deve ser a quantidade de números que o usuário quiser.
Outro problema que encontrei é que o enunciado especifica que deve haver uma validação caso o usuário insira valores inválidos:

Argumentos inválidos devem ser desconsiderados (Imprimir uma mensagem para cada valor inválido.), sem provocar a exibição de exceções (Exiba uma mensagem para o usuário pedindo a ele que informe novamente o valor).

import java.util.Scanner;

//falta tratar valores inválidos

public class Media {

    public static void main(String[] args) {
        //esse vetor não pode ter valor fixo, o usuário deve inserir quantos valores quiser, como fazer?
        float valor[] = new float[100], media = 0;
        int j = 0, i;

        Scanner scanner = new Scanner(System.in);

        System.out.println("Insira um valor: ");

        while (!scanner.hasNext("S")) {
            System.out.println("Insira um valor: ");
            valor[j] = scanner.nextFloat();
            j++;
        }
        for (i = 0; i < j; i++) {
            media += valor[i];
        }
        media = media/i;

        System.out.println("Média: "+ media);
    }
}

Comment: Usando vetores, a unica forma é inicia-lo depois de pegar o tamanho digitado pelo usuario, setando este valor, algo como `int entrada = scanner.nextInt();  float valor[] = new float[entrada]`.
Answer: Arrays têm tamanho fixo. Quando precisa de tamanho desconhecido precisa usar um ArrayList. Usei porque é a pergunta e pode ser que vá usar em outro lugar depois, no jeito atual nem array, nem ArrayList é necessário:
import java.util.Scanner;
import java.util.ArrayList;

class Media {
    public static void main(String[] args) {
        ArrayList<Float> valores= new ArrayList<Float>();
        float acumulador = 0;
        int contador = 0;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("Insira um valor: ");
            float valor = scanner.nextFloat();
            if (false) { //vai colocar a valisação aqui
                System.out.println("Valor inválido digite um válido");
                continue;
            }
            valores.add(valor);
            contador++;
            acumulador += valor;
            System.out.println("Deseja inserir outro valor S/N? ");
            if (!scanner.next().equals("S")) {
                break;
            }
        }
        for (int i = 0; i < valores.size(); i++) System.out.println("Nota " + (i + 1) + ": " + valores.get(i));
        System.out.println("Média: "+ acumulador / contador);
    }
}

Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.
Mais simples e começando fazer a validação:
import java.util.Scanner;

class Media {
    public static void main(String[] args) {
        float acumulador = 0;
        int contador = 0;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("Insira um valor: ");
            float valor = scanner.nextFloat();
            if (false) { //vai colocar a valisação aqui
                System.out.println("Valor inválido digite um válido");
                continue;
            }
            contador++;
            acumulador += valor;
            System.out.println("Deseja inserir outro valor S/N? ");
            if (!scanner.next().equals("S")) break;
        }
        System.out.println("Média: "+ acumulador / contador);
    }
}

Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.
Não coloquei a validação porque a pergunta não descreve como deve ser validado, mas a lógica básica está presente, basta mudar a condição.
Não sei se atende o requisito mas eu faria diferente:
import java.util.Scanner;

class Media {
    public static void main(String[] args) {
        float acumulador = 0;
        int contador = 0;
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("Insira um valor: ");
            String dado = scanner.nextLine();
            if (dado.equals("N")) break;
            try {
                float valor = Float.parseFloat(dado);
                contador++;
                acumulador += valor;
            } catch (NumberFormatException ex) {
                System.out.println("Valor inválido digite um válido");
            }
         }
        System.out.println("Média: "+ acumulador / contador);
    }
}

Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.
Nas próximas perguntas coloque um problema de cada vez e bem definido. Ter feito isso na pergunta anterior prejudicou as respostas, por isso o que foi postado lá acabou não ajudando solucionar o problema de fato, tanto que aqui não está usando a solução apresentada lá, mesmo tendo aceito uma resposta. Isso aconteceria aqui também.
Eu mudei o código para uma solução adequada. Pelo menos dentro do que eu entendi do problema que não está tão claro. O caminho que estava sendo adotado gerava várias confusões e desperdícios.


How can I apply css3 scale to an element's first-letter pseudo-element?

Question: I have a paragraph tag with a first-letter pseudo rule. I would like to apply the scale rule to that first letter but it doesn't seem to work.
Does the first-letter pseudo element support css3 scale?

This is some Text

This is some more Text

p:first-child:first-letter { font-family: "Baskerville"; font-size: 60px; -webkit-transform: scaleX(.8); display: inline-block; }

Comment: Why are there like 3 downvotes on this question? I don't see what's unclear or not useful about it.
Comment: I can not figure that out at all. There's nothing wrong with the question IMO.
Answer: working fine here, you sure you're using Webkit browser to view the results?

UPDATE:
This DOES work: http://jsfiddle.net/szXHZ/5/
You have to float the element and you have to set display to block!!
You will have to mess around with the style a little because it looks a bit ragedy (change line-height for a better look and maybe remove the clear that i've put on the second paragraph), but the transform does work when hovered. You can alter it so that you don't need to hover if you prefer that..
Comment: I looked at it in Chrome 14 and Safari 5.1 on both Mac and PC... nope, doesn't scale to 0.8.
Comment: @BoltCock, very strange, I uploaded a pic of what I see. Perhaps this is a platform bug? I'm on Ubuntu and it's fine in Chrome, Chromium and FF - all quite recent versions
Comment: ok, I think I see the problem, ignore my answer, you're right, it's not scaling, I have got the wrong idea here
Comment: No it works well. open it jsfiddle.net/sP3JZ/4 Problem is `.8`
Answer: Try this instead.

This is some Text

.firstletter { font-family: "Baskerville"; -webkit-transform: scale(2); display: inline-block; }

Now it works. http://jsfiddle.net/sP3JZ/7/
Comment: I did not downvote, but I'm not one to take kindly to answers that address "Does X support this?" with "Do Y." That said, you did provide a working alternative which is why I didn't downvote.
Comment: @BoltClock Don't you see "but it doesn't seem to work."? I suggested another working solution to this problem. What's wrong with my answer?
Comment: @stephenmurdoch What's wrong with my answer? It's my working solution. You can apply this rules with the help of simple function on dynamic pages too
Comment: your code, like mine, does not work if you remove the "font-size: 60px" - here's a js fiddle of your code with the font-size removed: http://jsfiddle.net/sP3JZ/ - I think scale only works on hover and hover doesn't appear to work when applied to first-letter
Comment: your code does not apply the transform to the first-letter pseudo element, instead it works on a custom class (called **.firstletter**) that you have created. This won't work on dynamic code, since you can't isolate the first letter without using pseudo elements (or js). So, your code is ok, nothing really wrong with it, but it's not solving the original problem. The question applies specifically to whether or not pseudo elements (:first-letter etc) support scaling as a transform function. You have managed to prove that custom classes do. But we knew that already :)


jQuery UI multiple selections

Question: see this demo  from jquery ui
you have to hold down the Ctrl key to make multiple selections
I really like the code but I don't want to force my visitor to press ctrl key
I want the code to allow multiple selections without holding ctrl key
is this possible?
Comment: Do you need the drawing box functionality?  If not you could probably roll your own and I or somebody else could help with the code.
Answer: I asked you a questions in the comments but I'll just write up a simple selection solution so you can see what I was thinking.
So basically you can use the jquery toggle()  effect to roll your own selector. When a user clicks you'll add the orange class, when he clicks again it will remove the orange class.
<code>$(document).ready( function() {
    $('ul#selectable li').toggle( function() {
        $(this).addClass('orange'); }, function() {
        $(this).removeClass('orange'); } );
});
</code>
Then all your job is to grab all the li elements with the orange class and post them to a form or whatever your end goal is. Haven't checked this code but what your doing is asking for all the li elements within selectable that have the orange value at the end of the class attribute.
With the code below I'm creating a new array and then adding the text() value of each "orange li" into it.
<code>var theSelections = new Array();

$('ul#selectable li[class$="orange"]').each( function(i) {
    theSelections[i] = $(this).text();
});
</code>
Comment: Thanks, this is what I am talking about :)
Answer: Yes.  However, the implementation would have to allow selected items to be deselected when clicked on a second time.  You would just need to modify the code slightly to achieve this.
Do a .selectable( 'Enable' )   on all the items.
Then you will need to do a .selectable( 'toggle' ) onClick on all items.
That should do the trick.
Comment: From the doc: Toggles the state of selectable functionality. This method either enables or disables selecting based on the current state.  If I'm reading this right your solution would basically just disable all functionality of .selectable.

jQuery UI multiple selections


How to find general solution for this matrix

Question: I'm trying to find the general solution to this matrix
\begin{bmatrix}1&-2&1&3&0\\2&-4&4&6&4\\ -2&4&-1&-6&2\\1&-2&-3&3&-8\end{bmatrix}
Ax=$\begin{bmatrix}1&6&0&-7&\end{bmatrix}^T$
I think I'm supposed to get it in x=x*+z format, I'm still not sure if this is the correct way to do it.
But I ended up getting this matrix in row echelon form.
-2(r1)+(r2)
2(r1)+(r3)
-(r1)+(r4)
\begin{bmatrix}1&-2&1&3&0\\0&0&2&0&4\\ 0&0&1&0&2\\0&0&-4&0&-8\end{bmatrix}
And then
2(r2)+(r4)
-1/2(r2)+(r3)
\begin{bmatrix}1&-2&1&3&0\\0&0&2&0&4\\ 0&0&0&0&0\\0&0&0&0&0\end{bmatrix}
then
1/2(r2)
\begin{bmatrix}1&-2&1&3&0\\0&0&1&0&1\\ 0&0&0&0&0\\0&0&0&0&0\end{bmatrix}
and lastly  -r2 + r2
\begin{bmatrix}1&-2&0&3&-2\\0&0&1&0&1\\ 0&0&0&0&0\\0&0&0&0&0\end{bmatrix}
after doing some algebra i ended up getting
x1 = 2(x2)-3(x4)-2(x5)
x3 = -2(x5)
and set x5 = x4 = x2 = 1
and got z =  $\begin{bmatrix}1&1&-2&1&1\end{bmatrix}^T$
But I'm when i try to solve for Ax = $\begin{bmatrix}1&6&0&-7\end{bmatrix}^T$
The last two rows are full of zeros
so I can't have 0=-7
How would i solve this?
Comment: You can (insert the right hand side as an additional column and) perform the same row operations to arrive to the new right hand side
Comment: @Berci thanks , i think i got it now, is my z correct? I just realized i have 5 rows in it but my x* has 4
Comment: I think it(z) should be $[-2\  3 -1\  4]^T$ and definitely not 5 components.
Answer: Hint:  (As in the comments) begin with $\left(\begin{array}{rrrrr|r} 1&-2&1&3&0&1\\2&-4&4&6&4&6\\-2&4&-1&-6&2&0\\1&-2&-3&3&-8&-7\end{array}\right)$.
Then row reduce and back substitute to get your solutions.
Note:  your $z$ is a member of the kernel.   The kernel is $3$-dimensional,  since the rank is $2$.
Answer: You have to work with the augmented matrix, and do the same operations on the rows of this matrix (including the supplementary column). Here is how it goes:
\begin{align}
&\begin{bmatrix}\begin{array}{@{}rrrrr|r@{}}
1&-2&1&3&0&1\\2&-4&4&6&4&6\\-2&4&-1&-6&2&0\\1&-2&-3&3&-8&-7
\end{array}\end{bmatrix}\rightsquigarrow
\begin{bmatrix}\begin{array}{@{}rrrrr|r@{}}
1&-2&1&3&0&1\\0&0&2&0&4&4\\0&0&1&0&2&2\\0&0&-4&0&-8&-8
\end{array}\end{bmatrix}\rightsquigarrow\\
&\begin{bmatrix}\begin{array}{@{}rrrrr|r@{}}
1&-2&1&3&0&1\\0&0&1&0&2&2\\0&0&0&0&0&0\\0&0&0&0&0&0
\end{array}\end{bmatrix}\rightsquigarrow\begin{bmatrix}\begin{array}{@{}rrrrr|r@{}}
1&-2&0&3&-2&-1\\0&0&1&0&2&2\\0&0&0&0&0&0\\0&0&0&0&0&0
\end{array}\end{bmatrix}
\end{align}
so the solutions are given by the equations
\begin{cases}
x_1=2x_2-3x_4-2x_5-1,\\
x_3=-2x_5+2.
\end{cases}


boosting with linear svm

Question: I am working on boosting classifier. I am planning to use linear svm as the weak classifier. I am using liblinear for it.
My question is how can I weight each instance of liblinear based on the boosting weights?
Comment: how come you consider linear SVM is a weak classifier? It is much better than random guessing, doesn' it?
Answer: A simple Matlab code using adaBoost+SVM, probably you can start from here...
<code>    N = length(X); % X training labels
    W = 1/N * ones(N,1); %Weights initialization
    M = 10; % Number of boosting iterations

    for m=1:M
        C = 10; %The cost parameters of the linear SVM, you can...
                 perform a grid search for the optimal value as well

        %Calculate the error and alpha in adaBoost with cross validation
        cmd = ['-c ', num2str(C), ' -w ', num2str(W)];
        model = svmtrain(X, Y, cmd);
        [Xout, acc, ~] = svmpredict(X,Y,cmd);

        err = sum(.5 * W .* acc * N)/sum(W);
        alpha = log( (1-err)/err );

        % update the weight
        W = W.*exp( - alpha.*Xout.*X );
        W = W/norm(W);

    end
</code>
In Wang's et al's Boosting Support Vector Machines for Imbalanced Data Sets, they applied a slightly different formula in the weight update, aiming at dealing with class imbalance.
Weights for data instances instruction is also shown in Prof. Lin's website.
Hope it helps.
Comment: Thank you very much for your reply. I think in libsvm/liblinear you cannot give weights to instances. You can give weights to classes.
Comment: @user570593 In fact the link I provided in the answer is trying to give weights to instances. I didn't get a chance to try, but in their readme file, it says, -training_weight_vector: An m by 1 vector of training weights, where m is the instance number. You may take a look at the wgt weight file included as well. Thanks

boosting with linear svm


Getting my Daughter Started in Music

Question: My daughter is currently 3, she has a very keen interest in music. She sings a lot and "composes" her own little "songs" to her own tunes. She has apparently has a keen ear for music, she inserts her own beats in the off-beat in quite a few songs I play for her from time to time and that is as far as I'm concerned quite impressive. I would like to know what type instrument to get her started with and how to go about doing it. I have very basic knowledge of music and used to play piano years ago, a skill I have now lost. Any suggestions would be great
Answer: Typically if children are started on instruments that young in life, the instruments are either violin or piano  The violins are 1/4, 1/8, or even 1/16 size if necessary.
Age 3 is an incredibly fertile time for a child - the brain is still taking shape and neurological connections are just beginning to be formed.  For example, this age would also be the best time to introduce a second language if you wanted them to be fluent.  That said, they may mix languages because they may not differentiate between the two.
At any rate, I just want to caution about "programming" children to be musicians.  Yes, pretty much any child that begins at 3 can become a virtuoso - it's the nature of the way our brains function.  That said, they may grow up feeling like they did not have a choice in the matter.
That said, developing kinesthetic motor coordination, aural skills, and a healthy appreciation for music is absolutely necessary for creating a well-rounded person.  What is important is that it isn't forced onto her, that if she follows it, it's her own choice - which is exactly how it sounds so far!
My recommendation would be that if she is interested, you could rent a violin for a month or two or a keyboard from a music store, and try a couple months of violin / piano lessons.  If she doesn't like them, then you can always wait a couple years before introducing a different instrument (though not stopping music!)  In addition, renting by month is nice because you're not committing several hundred dollars to something that's not guaranteed.
I hope that was more helpful than advice-y.
Comment: Nice one. I would also recommend buying a few toy instruments simultaneously to figure out which one is becoming the favorite..
Comment: I disagree a bit about the forcing -- I disliked piano for 7 years and then somewhat abruptly began to love it.  If not for being "forced" this wouldn't have happened.  Best for the parents to play it out as they see fit.
Comment: Yes, I'd agree with this as well - especially small percussion instruments.
Comment: Your comment Matthew is essentially the crux of my point in my answer - that the parents should be aware that they're not going overboard with things.  You want children to have fun and explore, but on the other hand it is important to teach about commitment.  I think you are a special circumstance as most people wouldn't spontaneously like an instrument that they disliked for so many years.  I also agree with you that things should "play out."  The child might love music but may end up a trumpet or flute player instead of piano or violin.
Comment: I played violin for 8 years - initially liked it, went through a phase of hating it (luckily my parents 'forced' me to continue) then liked it a bit more. Sure, I then dropped it for guitar when I was 16, but I think your point is very valid.
Comment: I don't think there is any reason to believe that a child cannot differentiate between two languages. In fact, most of the more recent research into the subject is showing quite the opposite. It's as harmless as your child not speaking with perfect grammar, correct them and move on, they won't just suddenly start speaking any language perfectly let alone 2 without your guidance. It isn't necessarily caused by lack of differentiation, it's more a lack of vocabulary in one language. Children will definitely do it but adults do it all the time also.
Comment: That sounded like exactly what I was looking for. I'm gonna rent the violin and see how it goes. I am going to buy myself one of the electronic keyboards to restart my music, she can use that too, Thanks a lot!
Comment: Glad to help. :)  Have fun making music!
Answer: There are a number of well-established, traditional education programs that are around a century old which are geared toward introducing preschoolers to music and musical concepts.
An element of this is found in the Montessori educational program.
The next area to investigate is finding a program that incorporates the Orff method (also known as the Orff Schulwerk or Orff Approach), using Orff instruments.
After this, I'm aware of several new educational franchises in the United States that facilitate parents working together with their preschool children to explore music and movement in a group setting with a teacher. One of the best known is MusikGarten. Another is The Music Groove.
What all of these programs have in common is that none of them involve the child learning to play a traditional musical instrument. Instead, in these programs, preschoolers learn about rhythm and counting and dancing and singing, and this prepares them for musical concepts they can learn when they are older. In the case of the Orff Approach, the children learn to play simple melodies and rhythms on special percussion instruments designed for children, in an explorational environment.
I should also mention the Suzuki method, primarily known for teaching violin playing to very young students. You can read a discussion about it at this Wikipedia link.
Learn about these programs, the well-established traditional academic ones and the new, less-academic  franchises, and see if there are schools in your area that provide one or more of these programs.
Comment: Personally I waited until my kids were older as I didn't want to push them at that age, as you say, but there do seem to be a large number of children brought up playing violin or piano from kindergarten age - so it is obvious that preschoolers **can** handle it.
Comment: You've seen the suzuki method,  right?  It starts them in preschool. A core tenet of the method is that learning from the environment at that age is natural.
Comment: I'm not a parent. But I've never heard of anybody being successful at studying a conventional musical instrument before the age of 5 or 6.
Comment: Good point. I had not considered the Suzuki method. But this approach with children so young is not without controversy. I'll amend my post.
Comment: I came to this question because I was thinking the question was not an appropriate question for this forum (although it is a great question to which I would otherwise want to contribute.) But you have 7k+ experience and you gave a good answer, so maybe I just need to revisit the boundaries for the forum. (Or maybe I have a good point?) Just curious how this fits within the pervue of musical practice and performance.
Comment: There have been many other questions here about studying and taking music lessons. That comes under "Practice".
Answer: You want the child to play music?   Focus on the word "play".  Make instruments available.  Play yourself (even if it's just banging two spoons together.)   Sing.  Have fun.  Don't worry about formal music for several more years.

Getting my Daughter Started in Music


Random assignment: why bother?

Question: Random assignment is valuable because it ensures independence of treatment from potential outcomes.  That is how it leads to unbiased estimates of the average treatment effect.  But other assignment schemes can also systematically ensure independence of treatment from potential outcomes.  So why do we need random assignment?  Put another way, what is the advantage of random assignment over nonrandom assignment schemes that also lead to unbiased inference?
Let $\mathbf{Z}$ be a vector of treatment assignments in which each element is 0 (unit not assigned to treatment) or 1 (unit assigned to treatment).  In a JASA article, Angrist, Imbens, and Rubin (1996, 446-47) say that treatment assignment $Z_i$ is random if $\Pr(\mathbf{Z} = \mathbf{c}) = \Pr(\mathbf{Z} = \mathbf{c'})$ for all $\mathbf{c}$ and $\mathbf{c'}$ such that $\iota^T\mathbf{c} = \iota^T\mathbf{c'}$, where $\iota$ is a column vector with all elements equal to 1.
In words, the claim is that assignment $Z_i$ is random if any vector of assignments that includes $m$ assignments to treatment is as likely as any other vector that includes $m$ assignments to treatment.
But, to ensure independence of potential outcomes from treatment assignment, it suffices to ensure that each unit in the study has equal probability of assignment to treatment.  And that can easily occur even if most treatment assignment vectors have zero probability of being selected.  That is, it can occur even under nonrandom assignment.
Here is an example. We want to run an experiment with four units in which exactly two are treated.  There are six possible assignment vectors:

1100
1010
1001
0110
0101
0011

where the first digit in each number indicates whether the first unit was treated, the second digit indicates whether the second unit was treated, and so on.
Suppose that we run an experiment in which we exclude the possibility of assignment vectors 3 and 4, but in which each of the other vectors has equal (25%) chance of being chosen.  This scheme is not random assignment in the AIR sense.  But in expectation, it leads to an unbiased estimate of the average treatment effect.  And that is no accident.  Any assignment scheme that gives subjects equal probability of assignment to treatment will permit unbiased estimation of the ATE.
So: why do we need random assignment in the AIR sense?  My argument is rooted in randomization inference; if one thinks instead in terms of model-based inference, does the AIR definition seem more defensible?
Comment: I haven't read Angrist et al., so maybe I'm missing something, but I have a quibble w/ your phrasing. We don't use random assignment to insure that the treatment is independent of the potential outcomes. Whether the treatment is independent of the outcomes in a true experiment depends on whether there is a direct causal connection b/t the treatment & the outcome. Rather, random assignment insures that the treatment is independent of *lurking variables* (or, potential confounders). It is the possibility that the outcome was caused by something other than the treatment that we hope to exclude.
Comment: "[T]o ensure independence of potential outcomes from treatment assignment, it suffices to ensure that each unit in the study has equal probability of assignment to treatment." **This is incorrect.** Suppose you have enrolled $x$ males and $x$ females in a study. Flip a fair coin: if heads, assign all females to the treatment group (and all males to the control group); if tails, all males will be in the treatment group and all females in the control group. Each subject (obviously) has 50% chance of assignment to the treatment group--but treatment is completely confounded with gender.
Comment: Sure, the estimate is "unbiased" in the same sense that a stopped clock gives an unbiased estimate of the time!  Actually, it's worse than that: this method of random selection yields results that cannot be attributed to the treatment, because they can just as well be attributed to gender. That's what confounding means. Focusing on obtaining unbiased results while destroying all useful information in the experiment is the proverbial throwing out of the baby...
Comment: @gung, I think that you are conflating "potential outcomes" and "outcomes."  It's true that random assignment doesn't ensure independence of treatment from outcomes (that is, from observed outcomes).  But potential outcomes are not the same as observed outcomes, and random assignment does ensure independence of treatment from potential outcomes.  I won't edit the original post to expand on this point; doing so would take me too far afield from the main topic.  But http://en.wikipedia.org/wiki/Rubin_causal_model may be helpful on this point.
Comment: @whuber, your comment doesn't sound correct.  To see why, suppose $x$ = 1.  The man's potential outcomes are Y(1) = 1 and Y(0) = 0.  (That is, $Y_m$ = 1 if the man is treated, 0 if not.)  For the woman, the potential outcomes are Y(1) = -1 and Y(0) = 2.  (The particular potential outcomes don't matter much, but small integers keep things simple.)  Then E[Y(1) | Z] = E[Y(1)] =  0.  Similar equalities hold for E[Y(0)].  More generally, your assignment mechanism isn't confounded with gender, and it will produce an unbiased ATE estimate.  If I am misunderstanding something, please let me know.
Comment: @user697473: but for whuber's scheme (or your no. 1 or 6), you'd only observe Y(1,m) and Y(0, w). Due to the non-random assignment Y(0, m) and Y (1, w) are excluded by design and will *never* be observed. In other words, after the experiment, you can conclude that the effect of making the patient male and treating *him* is 101. If you'd chosen the opposite, you'd conclude that that making the patient female and treating *her* does have an effect of -99. **But you do only one of these experiments** [Think of the other one as what happens when a colleague tries to reproduce your study...]
Comment: "[T]his method of random selection yields results that [...] can just as well be attributed to gender." @whuber, we must have different premises in mind. Your claim seems wrong to me -- here is a demonstration. Experiment has 1 man, 1 woman, and assignment by coin flip. We observe Y(1,m) = 101 if the subject is a treated man. Y(0,m) = 100; Y(1,w) = 1; Y(0,w) = 0. The diff-of-means estimate of the ATE is either 101 or -99. On average, it is 1 -- the true ATE. So the estimator is unbiased and there is no confounding with gender.  (Note that the effect of gender in this example is 100, not 1.)
Comment: @cbeleites -- I never meant to imply that a single experiment would produce an accurate estimate of the treatment effect. It won't. I only meant that the coin-flip assignment mechanism produces an unbiased estimate of the treatment effect. In my example, an experiment that uses the coin-flip mechanism will produce an estimate that is far from the true effect -- but that isn't a special point against the mechanism, because the same is true of many experiments when considered in isolation. (That's why we say that experiments produce estimates that are "unbiased," not "accurate.")
Answer: This follows up on gung's comment. Overall average treatment effect is not the point.
Suppose you have $1000$ new diabetes cases where the subject is between the ages of $5$ and $15$, and $1000$ new diabetes patients over $30$. You want to assign half to treatment. Why not flip a coin, and on heads, treat all of the young patients, and on tails, treat all of the older patients? Each would have a $50\%$ chance to be selected fro treatment, so this would not bias the average result of the treatment, but it would throw away a lot of information. It would not be a surprise if juvenile diabetes or younger patients turned out to respond much better or worse than older patients with either type II or gestational diabetes. The observed treatment effect might be unbiased but, for example, it would have a much larger standard deviation than would occur through random assignment, and despite the large sample you would not be able to say much. If you use random assignment, then with high probability about $500$ cases in each age group would get the treatment, so you would be able to compare treatment with no treatment within each age group.
You may be able to do better than to use random assignment. If you notice a factor you think might affect the response to treatment, you might want to ensure that subjects with that attribute are split more evenly than would occur through random assignment. Random assignment lets you do reasonably well with all factors simultaneously, so that you can analyze many possible patterns afterwards.
Comment: Thank you, Douglas.  This answer makes sense to me.  For the record, I didn't have in mind anything as extreme as your example or @whuber's example above.  I was thinking instead of cases in which we eliminate from consideration just a few treatment vectors.  (Consider a case in which a client says "you can treat this person or that one, but not both.")  But I think that your general points hold even for the milder cases that I have in mind.
Comment: I think if you only eliminate a few vectors, then you don't change the amount of information you can extract by much. Quantifying this accurately may be messy -- there are naive bounds which are probably too pessimistic.
Comment: @DouglasZare I have a question about your extreme example. I believe the goal is to find whether the treatment is effective for the population which has both young and old patients. Then, your method will generate two samples that cannot be regarded as the representative sample from the potential outcome distribution $F_t$ where all people take treatment and potential outcome distribution $F_c$ where all people take control. So then your observed treatment effect is biased
Answer: In your example you can leave 2 and 5 out as well and not contradict yourself.  At an item level there's still an equal chance of being 1 or 0 when there's just a 1:1 odds of selecting 1 or 6.  But, now what you did by removing 3 and 4 becomes more obvious.
Comment: Thanks, John.  Yes, you are correct.  It seems that we can eliminate as many treatment assignment vectors as we like, in any combination, so long as we use the remaining vectors in a way that gives each unit equal probability of assignment to treatment.
Comment: I don't think you're getting what I'm saying.  What I've presented is the ad absurdum case for your argument that argues against it.
Comment: Your example is extreme, but I don't see anything absurd about it.  It's a valid demonstration of the point: nonrandom assignment schemes (like using only vectors 1 and 6) can lead directly to unbiased estimation of the average treatment effect.  It follows that we don't need random assignment to get unbiased ATE estimates.  Of course, there may yet be reasons why it's bad to eliminate vectors 2 through 5.  (See [Douglas Zare's comment above](http://stats.stackexchange.com/a/37780/9618).)  I haven't yet thought through these reasons.
Comment: You should.  It's why you can't eliminate them.
Answer: Here's another one of the lurking or confounding variables: time (or instrumental drift, effects of sample storage, etc.).
So there are arguments against randomization (as Douglas says: you may do better than randomization). E.g. you can know beforehand that you want your cases to be balanced over time. Just as you can know beforehand that you want to have gender and age balanced.
In other words, if you want to manually choose one of your 6 schemes, I'd say that 1100 (or 0011) is a decidedly bad choice. Note that the first possibilites you threw out are those that are most balanced in time...  And the worst two are left in after John proposed to thow out also 2 and 5 (against which you did not protest).
In other words, your intuition which schemes are "nice" unfortunately leads towards bad experimental design (IMHO this is quite common; maybe ordered things look nicer - and for sure it is easier to keep track of logical sequences during the experiment).
You may be able to do better with non-randomized schemes, but you are also able to do much worse. IMHO, you should be able to give physical/chemical/biological/medical/... arguments for the particular non-random scheme you use, if you go for a non-random scheme.

Random assignment: why bother?


AWS API Gateway Enable Cloudwatch Logs via Boto3

Question: I am writing deployment scripts in Python using boto3 for an API Gateway.  The script will create the API, resources, methods, and deployment.  When I look in the AWS Management Console there is a checkbox for Enable CloudWatch Logs

Is there a way to enable this via boto3 or the REST API?  I haven't seen any options in the documentation for this.
Answer: This is available in update_stage in boto3 (http://boto3.readthedocs.io/en/latest/reference/services/apigateway.html#APIGateway.Client.update_stage)
The patch paths are documented here https://docs.aws.amazon.com/apigateway/api-reference/link-relation/stage-update/
You can use a patch operation like below to set log level on all resources/methods
   { "patchOperations" : [
    {
        "op" : "replace",
        "path" : "/*/*/logging/loglevel",
        "value" : "INFO"
    },
   }

Comment: As noted above, the `patchOperations` is an array, so multiple values can be set in a single endpoint call.
Answer: The cloudwatch log setting you are looking for is associated with a stage resource. In your case, you need to use the update_stage method in boto3 to apply a patch operation to set the correct log level.
From the boto docs:

loggingLevel (string) --
Specifies the logging level for this method, which effects the log
  entries pushed to Amazon CloudWatch Logs. The PATCH path for this
  setting is /{method_setting_key}/logging/loglevel , and the available
  levels are OFF , ERROR , and INFO .

Here is the link to the update stage REST API reference.


Using ActiveSupport to convert from a utc to a ruby object

Question: This seems like a simple problem but I can't seem to find the answer. I have a UTC time (ie. 1323481111) and I want to change it to a DateTime or TimeWithZone object in a ruby script. I haven't found a way to do it with just ruby but I believe there should be a way to do it using ActiveSupport. It seems to me that I should be able to do it as follows:
require "active_support/all"

Time.zone.at(1323481111)

http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html
But that doesn't seem to work. Anyone know how to convert a utc int to a datetime object?
Answer: What exactly does "doesn't seem to work" mean? Are you getting an exception like NoMethodError: undefined method 'at' for nil:NilClass? If yes, make sure you have the tzinfo gem installed and also require tzinfo. Then set a time zone and you are good to go:
>> Time.zone.at(1323481111)
NoMethodError: undefined method 'at' for nil:NilClass
[..]
>> require 'tzinfo' #=> true
>> Time.zone = "Vienna" #=> "Vienna"
>> Time.zone.at(1323481111) #=> Sat, 10 Dec 2011 02:38:31 CET 01:00

Comment: Both great answers (@Michael Kohl and @steenslag). I wish I could check them both. But since I phrased the question using ActiveSupport this is probably the better answer.
Answer: Without (or with) ActiveSupport:
require 'date'
DateTime.strptime("1323481111",'%s')

If you want to set the timezone to be something else than UTC, then go by @Michael Kohl's answer.


Continuity concerning family of projections

Question: Let $X$ be a compact topological space, $H$ be a complex Hilbert space and endow $F(H)$, the space of bounded Fredholm operators in $H$, with the uniform norm topology (inherited from $B(H)$).
Let $T: X\to F(H)$, $x\mapsto T_x$, be a continuous map.
There exists a closed subspace $V\subseteq H$ of finite codimension, i.e. $\dim H/V<\infty$, such that $V\cap \ker T_x = \{0\}$ for all $x\in X$.
I have proved that $H/T(V) = \bigsqcup\limits_{x\in X} H/T_x(V)$ is a vector bundle over $X$ (of finite rank).
In particular, $\dim H/T_x(V)$ is independent of $x$ (here we can assume connectedness of $x$).
For $x\in X$, let $P_x: H\to H$ be the orthogonal projection onto $T_x(V)$.
In order to induce a specific map of bundles (see here for details), I need to check the continuity of the map $X\times H\to H$ given by $(x,u)\mapsto P_x(u)$.

Question: Is $(x,u)\mapsto P_x(u)$ continuous?

Looking at the inequality
$$ \|P_y(v)-P_x(u)\| \leq \|P_y(v-u)\| + \|(P_y-P_x)(u)\|$$
we conclude it suffices to prove that $x\mapsto P_x$ is continuous when one gives $B(H)$ the strong operator topology, but I could not prove it.
Any help is appreciated. Thanks in advance!
Answer: Let $S_x=T_x|_V$.  Then $S_x$ coincides with the composition of $T_x$ with the inclusion of $V$ into $H$, both Fredholm operators, so $S_x$ is also Fredholm.
Like every Fredholm operator, $S_x$ has closed range and it is clearly one-to-one.  From this and the open mapping Theorem it easily follows that $S_x^*S_x$ is invertible.
Therefore $R_x:=S_x(S_x^*S_x)^{-1/2}$ is a well defined isometry having the same range as $T_x$ and we deduce that $P_x=R_xR_x^*$, from where the norm continuity of $P_x$ follows.
Comment: @RodrigoDias, I am sorry.  The exponent should be $-1/2$.  I have just edited my answer to fix this.  It should be like in the polar decomposition.
Comment: Thank you Ruy! I was trying to prove that $R$ is an isometry without success. In order to an operator $A:V_1\to V_2$ to be an isometry, it is necessary that $A^*A=id_{V_1}$. We have $R^*R=(S^* S)^{-1}$, which need not to be $id_{V}$. Am I missing something?
Comment: Right, I got it!


Use variables in a textfile as input

Question: As the topic says..
I want to use the file
indata.txt:
f=lambda x,y:-2*x*y
xa=0
xb=2
A=2
n=18

Into my big program:
from math import *

'''Function which calculates the d.e. using runge-kuttas formula'''
def runge(f,xa,xb,A,n):
    .....

indata=open("indata.txt","r")
runge(f,xa,xb,A,n)
indata.close()

So my question is:
How to I use the variables in the text file indata.txt as input to the bigger program?
Comment: Looks like python, why not just call it `indata.py` and import it?
Comment: It's python, sorry for not mentionting that. The project require it to be a textfile
Comment: You can still import it via filename, see the dupe I linked.
Answer: As I know, you could use one of two options:
 1. Rename your file to inpdata.py as mentioned in comments and in your main file use
from inpdata import *

Use built in python execfile() function. For Python3 execfile() alternative look here.
Comment: [Alternative for Python 3](http://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3-0)
Comment: Thank you so much!
Comment: @M.T Thank you for helpful addition!

Use variables in a textfile as input


When trying to escape apostrophe with 'gsub', I get backreference

Question: I have this code:
"1'2".gsub("'","\\'")

Instead of "1\'2", I get: "122". Why?
Answer: You need to use this:
puts "1'2".gsub("'","\\\\'")

Answer: It is because "\\'" means the context following the match, which is "2".


Programmatically set Panelizer View Template on Node

Question: I have panelizer installed so that I can panelize the full content views on a node by node basis on my content types. For example I have a news content type but I have a default panelizer view mode and I have a 'press release' version meant for the news nodes that are considered press releases.
I'm attempting to perform a query on them so I can programmatically set the view modes in bulk instead of having to go through each node and do it manually, but I'm having trouble. The machine names of the views are 'news' and 'press_release' and I'm attempting to change them like so
<code>$node->panelizer->view_mode = 'press_release';
$node->save();
</code>
But it doesn't seem to have any effect. How would I programmatically change the panelizer view mode on a node?
UPDATE:
I used $node->panelizer->getValue(); and this is what I received.
<code>string(4) "news"
array(2) {
  [0]=>
  array(3) {
    ["view_mode"]=>
    string(4) "news"
    ["default"]=>
    string(4) "news"
    ["panels_display"]=>
    array(0) {
    }
  }
  [1]=>
  array(3) {
    ["view_mode"]=>
    string(4) "full"
    ["default"]=>
    string(13) "press_release"
    ["panels_display"]=>
    array(0) {
    }
  }
}
</code>
It seemed like instead of getting the view_mode that is set I got every view mode which made me think Iwas on the wrong path.
Answer: It turned out the output of $node->panelizer->getValue(); was giving me a clue that I missed.
I had forgotten that there were different panelizer templates for different view modes.
I was able to accomplish what I needed with
<code>    $panelizer_arr = $node->panelizer->getValue();
    for ($i = 0; $i < count($panelizer_arr); $i++) {
        if ($panelizer_arr[$i]['view_mode'] == 'full') {
            $panelizer_arr[$i]['default'] = 'test_display';
        }
    }
    $node->panelizer = $panelizer_arr;
    $node->save();
</code>
Comment: Found out the [1] element of the panelizer array didn't always correspond to the 'full' view mode so I fixed it to search for that instead of depending on a number;


'javax.persistence.RollbackException' recebido ao invocar escuta de ação '#{}

Question: Eu informo os valores na pagina, porem os valores nao são persistidos no banco.
é como se minha pagina nao enviasse os valores para as classes controladoras. Sim os valores sao informados no entanto, o erro é quando os valores são persistidos. Não sei como solucionar tal problema, gostaria de uma ajuda.
Page
<ui:composition template="/WEB-INF/template/LayoutPadrao.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

    <ui:define name="titulo">Novo Cliente</ui:define>

    <ui:define name="corpo">
        <f:metadata>
            <f:event listener="#{cadastroClienteBean.inicializar}"
                type="preRenderView" />
        </f:metadata>

        <h:form>

Novo Cliente

        </h:form>
    </ui:define>
</ui:composition>

Service
import java.io.Serializable;

import javax.inject.Inject;

import com.honda.model.Cliente;
import com.nastek.repository.Clientes;

public class CadastroClienteService implements Serializable{

    private static final long serialVersionUID = 1L;

    @Inject
    private Clientes clientes;

    public Cliente salvar(Cliente cliente) {
        return clientes.guardar(cliente);
    }

}

Bean
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import com.honda.model.Cliente;
import com.honda.model.Vendedor;
import com.nastek.service.CadastroClienteService;

@ManagedBean
@Named
public class CadastroClienteBean implements Serializable{

    private static final long serialVersionUID = 1L;

    @Inject
    private EntityManager manager;

    @Inject
    private CadastroClienteService cadastroProdutoService;

    private Cliente cliente;

    private List<Vendedor> listaVendedores;

    public void salvar() {
        System.out.println(this.cliente);
        this.cliente = cadastroProdutoService.salvar(this.cliente);
    }

    public CadastroClienteBean() {
        this.cliente = new Cliente();
        this.listaVendedores = new ArrayList<>();
    }

    public void inicializar() {
        System.out.println("iniciando...");

        //listaVendedores = manager.createQuery("SELECT v FROM Vendedor v", Vendedor.class).getResultList();

        System.out.println(listaVendedores);
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public List<Vendedor> getListaVendedores() {
        return listaVendedores;
    }
}

repository
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import com.honda.model.Cliente;

@Named
public class Clientes implements Serializable {

    private static final long serialVersionUID = 1L;

    @Inject
    private EntityManager manager;

    public Cliente guardar(Cliente cliente) {
        EntityTransaction trx = manager.getTransaction();
        trx.begin();
        cliente = manager.merge(cliente);
        trx.commit();

        return cliente;
    }

}

Error
fev 23, 2018 1:24:02 PM javax.faces.event.MethodExpressionActionListener processAction
GRAVE: 'javax.persistence.RollbackException' recebido ao invocar escuta de ação '#{cadastroClienteBean.salvar}' para o componente 'botaoSalvar'
fev 23, 2018 1:24:02 PM javax.faces.event.MethodExpressionActionListener processAction
GRAVE: javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:94)
    at com.nastek.repository.Clientes.guardar(Clientes.java:24)
    at com.nastek.service.CadastroClienteService.salvar(CadastroClienteService.java:18)
    at com.nastek.controller.CadastroClienteBean.salvar(CadastroClienteBean.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:775)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:82)
    ... 42 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
    ... 42 more
Caused by: org.postgresql.util.PSQLException: ERRO: valor nulo na coluna "cpf" viola a restrição não-nula
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    ... 55 more

fev 23, 2018 1:24:02 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
GRAVE: JSF1073: javax.faces.event.AbortProcessingException obtido durante o processamento de INVOKE_APPLICATION 5: UIComponent-ClientId=j_idt25:botaoSalvar, Message=javax.persistence.RollbackException: Error while committing the transaction
fev 23, 2018 1:24:02 PM com.sun.faces.context.AjaxExceptionHandlerImpl log
GRAVE: javax.persistence.RollbackException: Error while committing the transaction
javax.faces.event.AbortProcessingException: javax.persistence.RollbackException: Error while committing the transaction
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:182)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:775)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javax.persistence.RollbackException: Error while committing the transaction
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:94)
    at com.nastek.repository.Clientes.guardar(Clientes.java:24)
    at com.nastek.service.CadastroClienteService.salvar(CadastroClienteService.java:18)
    at com.nastek.controller.CadastroClienteBean.salvar(CadastroClienteBean.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153)
    ... 30 more
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:82)
    ... 42 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
    at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
    ... 42 more
Caused by: org.postgresql.util.PSQLException: ERRO: valor nulo na coluna "cpf" viola a restrição não-nula
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    ... 55 more

Comment: Seu erro é esse: ERRO: valor nulo na coluna "cpf" viola a restrição não-nula. O CPF está vindo nulo.
Comment: Nosso idioma é o **Português**. Traduza sua pergunta.
Answer: Como a Giuliana Bezerra comentou, o erro é por causa do CPF que está nulo:
'ERRO: valor nulo na coluna "cpf" viola a restrição não-nula'
Esta é a exceção original do banco de dados, que provoca a exceção 'javax.persistence.RollbackException' do servidor de aplicação por estar em uma transação de banco de dados.
Answer: Certo, no entanto, o valor CPf e todos os outros são informados na pagina!
Um exemplo disso é o debug colocado no método de salvar que tem na minha classe CadastroClienteBean. Como podem ver o valor nao vem nulo. ou seja, os valores informados na pagina são obtidos pela entidade, porem nao é persisdo no banco.
O erro de que o CPF está em branco irá acontecer para os demais campos caso eu remova o input CPF. Compreendem ?


Drupal Commerce hide cart to non authenticated users

Question: I have a drupal 7 commerce install. I have made the views only accessible to authenticated users, but when I am not authenticated, and type in "mysite.com/cart" I still can see the cart (even though it has nothing in it).
I am using the content access module to prevent non authenticated users from viewing my produces, but I don't want anyone who is not logged in, to even know the website doubles as a store. How do I hide the cart page?
Answer: A quick fix might be to write a tiny custom module that overrides the access callback for the cart page:
function MYMODULE_menu_alter(&$items) {
  $items['cart']['access callback'] = 'MYMODULE_cart_access';
}

function MYMODULE_cart_access() {
  if (user_is_logged_in()) {
    return user_access('access content');
  }

  return FALSE;
}

Comment: I don't know the first thing about writing modules, could you explain further where to put the code, and any other steps that need to be done.
Comment: Check out [Telling Drupal about your module](http://drupal.org/node/1075072) and [Writing comments and implementing your first hook](http://drupal.org/node/1095546), those two pages have everything you need to know