Coordinate descent with constraints

Question: When performing constrained optimization on a smooth, convex function using coordinate descent, for what types of constraints will the algorithm work ? (i.e. converge or reach an approximate optimum within a tolerance of the constraint)
My understanding is that coordinate descent will work for

Box constraints: e.g.  $x_1 \leq -1$ and $x_2 \leq -1$
Linear constraints: e.g. $x_2 \leq -x_1 -1$
Any others ?

In other words, how can we know whether or not coordinate descent can be applied to a contrained optimization problem ?
PS: irrespective of whether this is the right algorithm to use
Answer: The bland answer is that to solve constrained optimization problems, you need an algorithm that knows and uses the constraints - simply applying unconstrained optimization algorithms will not work.
Box constraints
Taking the algorithm you described in your answer, it will ''not work'' for box constraints, depending on the value of the step-size $\alpha$;

Take $f(x) = x$, $x \leq 0.9$, start at $x_0 = 0$ with a step-size $\alpha=1$.
The only step available does not satisfy the constraint.

You can decrease $\alpha$ to make it ''work'', but you can always find a function/starting point for which it does not work; the correct step-size depends on the interaction between the function and the constraints and thus require solving a constrained optimization problem to set it.
If you are able to solve the subproblem of finding the minimum along the dimension you are optimizing while respecting the constraint, i.e. taking steps
$$x_i^{k+1} = \arg \min_{x_i \in C} f(x_1^{k}, ..., x_{i-1}^{k}, x_i, x_{i+1}^{k}, ..., x_D^{k}),$$
you can solve box constraints, as in your first reference.
Linear constraints
However, this approach will choke on linear constraints.

Take $f(x,y) = x^2 + y^2$, with $x + y < -1$. The minimum is at $(-0.5,-0.5)$, but if you start at $(-1,0)$ you can not make progress on $x$ (not allowed to be bigger than $-1$) nor $y$ (is at the minimum given $x = -1$).

Your second ref. is able to get around that issue by considering block coordinate updates; changing $k$ coordinates at each iteration makes it possible to get around linear constraints involving less than $k$ variable at a time.
Non-linear constraints
Your non-linear constraint is also non convex; start at $(0,-3)$ and the algorithm would converge to $(0, -2.9...)$, not at the minimum.
What to do
You can either use a constrained optimization algorithm (Frank-Wolfe comes to mind) or re-parametrize your problem so that it incorporates the constraints; if you want to find the minimum of $f(x), x \geq 0$, try to find the minimum of $g(y) = f(y^2), y \in \mathbb{R}$
Comment: Excellent answer thanks a lot - you are right I did have to "cheat" when building the toy examples below - forcing the starting points at specific values. My feeling is that by randomly initializing the starting point within the constraint we could get around some limitations. But of course this is less optimal than alternative methods
Answer: Perhaps this question is more difficult than I thought, or would be better asked on math.stackexchange.com. But since it arose in the context of SVM I will leave it here.
There are some papers discussing coordinate descent and constrained optimization with the following key words.

Linear and box constraints and blocks of primal variables here
Coupled constraints, Compact convex sets and Polyhedra here
A cryptic MIT paper

While the mathematics are beyond me, I will proceed to show visually and experimentally that for the following toy, convex function, coordinate descent works for box, linear and highly non linear constraints.
Consider the function to be minimized:
$$f(x_1,x_2) = .01x_1^2 + 0.03x_2^2$$
$$\nabla f = [2 \times 0.01 x_1, 2 \times .03 x_2]$$
Since the gradient has only terms in $x_1$ and $x_2$ respectively, we can evaluate it at each step $k$ holding the other variable constant. Checking the constraint at each step allows to

Do nothing if $x_i^{(k+1)}$ has a value outside the constraint boundary
Update $x_i^{(k+1)} = x_i^{(k)} - \alpha \ \nabla_i \ f(x_1, x_2)$ otherwise

A few examples
1) Box and 2) linear constraints

$c1: \ x_1 \leq -1 \ \ c2: \ x_2 \leq -1.5 $
$c: \ x_1 + x_2 \leq -2$

3) Non linear constraint

$c: \ (x_1 + 1.5)^3 + (x_2 + 1.5)^3 \leq 2$

Next steps
Perhaps a function which is convex, but whose gradient has cross terms $x_1x_2$ will prove more difficult or even impossible to optimize using CD
Comment: In general, it is not possible to make CD work in constrained problems without adapting it for the constraints at hand.
Linear constraints don't necessarily work; take $f(x,y) = x^2 + y^2$, $x + y <= -2$ and start at $(-3,-3)$. You will hit a wall (unless using decreasing step-sizes). Non-convex constrained sets won't work either; start your example at $(0,-3)$ and you will converge to $(0,-2.9...)$
Comment: thanks for the comment ! Do you think you could expand it a little and post it as an answer ? 1) it will help me better understand your points and 2) I don't want 50 hard earned bounty points to go to waste !

Coordinate descent with constraints


Audio Frequency in Python

Question: I have made a script which checks the frequency of the sound from a wave file. But I keep getting an error in the 24 line which is while len(data) == chunk*swidth:. Can anyone help me ?? I am using PyAudio, wave and Numpy to do this.
# Read in a WAV and find the freq's
import pyaudio
import wave
import numpy as np

chunk = 2048

# open up a wave
wf = wave.open('output.wav', 'rb')
swidth = wf.getsampwidth()
RATE = wf.getframerate()
# use a Blackman window
window = np.blackman(chunk)
# open stream
p = pyaudio.PyAudio()
stream = p.open(format =
                p.get_format_from_width(wf.getsampwidth()),
                channels = wf.getnchannels(),
                rate = RATE,
                output = True)

# read some data data = wf.readframes(chunk)
# play stream and find the frequency of each chunk
while len(data) == chunk*swidth:
    # write data out to the audio stream
    stream.write(data)
    # unpack the data and times by the hamming window
    indata = np.array(wave.struct.unpack("%dh"%(len(data)/swidth),\
                                         data))*window
    # Take the fft and square each value
    fftData=abs(np.fft.rfft(indata))**2
    # find the maximum
    which = fftData[1:].argmax() + 1
    # use quadratic interpolation around the max
    if which != len(fftData)-1:
        y0,y1,y2 = np.log(fftData[which-1:which+2:])
        x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
        # find the frequency and output it
        thefreq = (which+x1)*RATE/chunk
        print ("The freq is %f Hz." % (thefreq))
    else:
        thefreq = which*RATE/chunk
        print ("The freq is %f Hz." % (thefreq))
    # read some more data
    data = wf.readframes(chunk)
if data:
    stream.write(data)
stream.close()
p.terminate()

This is the error message
Traceback (most recent call last): File "C:\Users\Admin\Desktop\Ishan\Python\Sublime Text Projects\Sound Frequencies second.py", line 23, in <module> while len(data) == chunk*swidth: NameError: name 'data' is not defined
Comment: What is the error message? Also where is the declaration/process of `data`?
Comment: `# read some data data = wf.readframes(chunk)` Did you perhaps mean for part of this to be on a separate line, instead of being part of the comment?
Comment: Here you have it, you didn't declare `data` variable but you try to get the length of it
Comment: Well, when you write `while len(data) == chunk*swidth:`, how exactly are you expecting it to decide what `len(data)` is equal to? Where is the value of `data` supposed to come from?
Answer: you use data in while, which defined later.
use flag in while with post check, whittled preset as True to start while 1st round:
# Read in a WAV and find the freq's
import pyaudio
import wave
import numpy as np

chunk = 2048

# open up a wave
wf = wave.open('output.wav', 'rb')
swidth = wf.getsampwidth()
RATE = wf.getframerate()
# use a Blackman window
window = np.blackman(chunk)
# open stream
p = pyaudio.PyAudio()
stream = p.open(format =
                p.get_format_from_width(wf.getsampwidth()),
                channels = wf.getnchannels(),
                rate = RATE,
                output = True)

# play stream and find the frequency of each chunk

flag = True # << define flag and set to True to make while 1st stap

while flag:

    # read some data << move under while to this plase
    data = wf.readframes(chunk)

    # write data out to the audio stream
    stream.write(data)
    # unpack the data and times by the hamming window
    indata = np.array(wave.struct.unpack("%dh"%(len(data)/swidth),\
                                         data))*window
    # Take the fft and square each value
    fftData=abs(np.fft.rfft(indata))**2
    # find the maximum
    which = fftData[1:].argmax() + 1
    # use quadratic interpolation around the max
    if which != len(fftData)-1:
        y0,y1,y2 = np.log(fftData[which-1:which+2:])
        x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
        # find the frequency and output it
        thefreq = (which+x1)*RATE/chunk
        print ("The freq is %f Hz." % (thefreq))
    else:
        thefreq = which*RATE/chunk
        print ("The freq is %f Hz." % (thefreq))
    # read some more data
    # data = wf.readframes(chunk) << comment this line

    if (len(data) != chunk*swidth): # << put check while condition
        flag=False

if data:
    stream.write(data)
# ^^^ I think you need to put in to the while loop upper

stream.close()
p.terminate()

I didn't watch working capacity as a whole, and only logical mistake at this stage with the cycle while and an entry condition.
Comment: Ive adit post to all code. I didn't watch working capacity as a whole, and only logical mistake at this stage with the cycle while and an entry condition.
Comment: I don't using  stream methods, but I think : if data: stream.write(data) - you need to put in to the while loop

Audio Frequency in Python


How can I read comma separated values from a text file in Java?

Question: I have got this text file with latitude and longitude values of different points on a map.
How can I split my string into latitudes and longitudes? What is the general way to do these type of things that is with other delimiters like space or tab etc.?
Sample file:
28.515046280572285,77.38258838653564
28.51430151808072,77.38336086273193
28.513566177802456,77.38413333892822
28.512830832397192,77.38490581512451
28.51208605426073,77.3856782913208
28.511341270865113,77.38645076751709

This is the code I am using to read from the file:
try(BufferedReader in = new BufferedReader(new FileReader("C:\\test.txt"))) {
    String str;
    while ((str = in.readLine()) != null) {
        System.out.println(str);
    }
}
catch (IOException e) {
    System.out.println("File Read Error");
}

Comment: OpenCSV is a popular library if you would prefer using that.
Details here: http://opencsv.sourceforge.net/ There are also a few other stackoverflow questions on a similar topic: - https://stackoverflow.com/questions/101100/csv-api-for-java - https://stackoverflow.com/questions/200609/can-you-recommend-a-java-library-for-reading-and-possibly-writing-csv-files
Answer: You may use the String.split() method:
String[] tokens = str.split(",");

After that, use Double.parseDouble() method to parse the string value to a double.
double latitude = Double.parseDouble(tokens[0]);
double longitude = Double.parseDouble(tokens[1]);

Similar parse methods exist in the other wrapper classes as well - Integer, Boolean, etc.
Comment: @user1425223 note that `String.split` takes a regex and if you want to ensure nothing funny happens (like with `.` or `|`) you can first pass the separator through  `Pattern.quote()`: `str.split(Pattern.quote("."))` (or escape it manually)
Comment: @AVD:I guess the solution was provided to read the comma seperated values from text file?Which it does not do correctly.While reading it is not reading the first values in that text file.So if someone use this as a example he would run into problems unneccessarily.That was the reason to bring it up through a comment.
Comment: Thanks a lot. Worked like a charm.
Comment: @AVD ::  did you get the first line in your text file whcih you have shown above?I am not able to get the content of the first line...rest all is great.
Comment: @user3560140 That code snippet explains the usage of `Split` method. Please ask a new question if you've some issues with your code.
Answer: Use OpenCSV for reliability. Split should never be used for these kind of things.
Here's a snippet from a program of my own, it's pretty straightforward. I check if a delimiter character was specified and use this one if it is, if not I use the default in OpenCSV (a comma). Then i read the header and fields
CSVReader reader = null;
try {
    if (delimiter > 0) {
        reader = new CSVReader(new FileReader(this.csvFile), this.delimiter);
    }
    else {
        reader = new CSVReader(new FileReader(this.csvFile));
    }

    // these should be the header fields
    header = reader.readNext();
    while ((fields = reader.readNext()) != null) {
        // more code
    }
catch (IOException e) {
    System.err.println(e.getMessage());
}

Answer: To split your String by comma(,) use str.split(",") and for tab use str.split("\\t")
    try {
        BufferedReader in = new BufferedReader(
                               new FileReader("G:\\RoutePPAdvant2.txt"));
        String str;

        while ((str = in.readLine())!= null) {
            String[] ar=str.split(",");
            ...
        }
        in.close();
    } catch (IOException e) {
        System.out.println("File Read Error");
    }

Answer: Use BigDecimal, not double
The Answer by adatapost is right about using String::split but wrong about using double to represent your longitude-latitude values. The float/Float and double/Double types use floating-point technology which trades away accuracy for speed of execution.
Instead use BigDecimal to correctly represent your lat-long values.
Use Apache Commons CSV library
Also, best to let a library such as Apache Commons CSV perform the chore of reading and writing CSV or Tab-delimited files.
Example app
Here is a complete example app using that Commons CSV library. This app writes then reads a data file. It uses String::split for the writing. And the app uses BigDecimal objects to represent your lat-long values.
package work.basil.example;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;

import java.io.BufferedReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class LatLong
{
    //----------|  Write  |-----------------------------
    public void write ( final Path path )
    {
        List < String > inputs =
                List.of(
                        "28.515046280572285,77.38258838653564" ,
                        "28.51430151808072,77.38336086273193" ,
                        "28.513566177802456,77.38413333892822" ,
                        "28.512830832397192,77.38490581512451" ,
                        "28.51208605426073,77.3856782913208" ,
                        "28.511341270865113,77.38645076751709" );

        // Use try-with-resources syntax to auto-close the `CSVPrinter`.
        try ( final CSVPrinter printer = CSVFormat.RFC4180.withHeader( "latitude" , "longitude" ).print( path , StandardCharsets.UTF_8 ) ; )
        {
            for ( String input : inputs )
            {
                String[] fields = input.split( "," );
                printer.printRecord( fields[ 0 ] , fields[ 1 ] );
            }
        } catch ( IOException e )
        {
            e.printStackTrace();
        }
    }

    //----------|  Read  |-----------------------------
    public void read ( Path path )
    {
        // TODO: Add a check for valid file existing.

        try
        {
            // Read CSV file.
            BufferedReader reader = Files.newBufferedReader( path );
            Iterable < CSVRecord > records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse( reader );
            for ( CSVRecord record : records )
            {
                BigDecimal latitude = new BigDecimal( record.get( "latitude" ) );
                BigDecimal longitude = new BigDecimal( record.get( "longitude" ) );
                System.out.println( "lat: " + latitude + " | long: " + longitude );
            }
        } catch ( IOException e )
        {
            e.printStackTrace();
        }
    }

    //----------|  Main  |-----------------------------
    public static void main ( String[] args )
    {
        LatLong app = new LatLong();

        // Write
        Path pathOutput = Paths.get( "/Users/basilbourque/lat-long.csv" );
        app.write( pathOutput );
        System.out.println( "Writing file: " + pathOutput );

        // Read
        Path pathInput = Paths.get( "/Users/basilbourque/lat-long.csv" );
        app.read( pathInput );

        System.out.println( "Done writing & reading lat-long data file. " + Instant.now() );
    }

}

Comment: Fourteen decimal places is probably sufficient… https://gis.stackexchange.com/a/8674
Answer: //lat=3434&lon=yy38&rd=1.0&| in that format o/p is displaying
public class ReadText {
    public static void main(String[] args) throws Exception {
        FileInputStream f= new FileInputStream("D:/workplace/sample/bookstore.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(f));
        String strline;
        StringBuffer sb = new StringBuffer();
        while ((strline = br.readLine()) != null)
        {
            String[] arraylist=StringUtils.split(strline, ",");
            if(arraylist.length == 2){
                sb.append("lat=").append(StringUtils.trim(arraylist[0])).append("&lon=").append(StringUtils.trim(arraylist[1])).append("&rt=1.0&|");

            } else {
                System.out.println("Error: "+strline);
            }
        }
        System.out.println("Data: "+sb.toString());
    }
}

Answer: You can also use the java.util.Scanner class.
private static void readFileWithScanner() {
    File file = new File("path/to/your/file/file.txt");

    Scanner scan = null;

    try {
        scan = new Scanner(file);

        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            String[] lineArray = line.split(",");
            // do something with lineArray, such as instantiate an object
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        scan.close();
    }
}


Which sport uses the term "Butterfly Stroke"?

Question: What sport uses the term "Butterfly Stroke"?
Comment: This is the first time I've seen someone post a question and then include the answer in the tags.
Comment: @F1Krazy that is because I've removed all the other tags the question does not belong to.
Answer: The butterfly is a swimming stroke, where both arms move symmetrically.


Can taxonomy tags/terms be more than one word?

Question: I've created a view that filters content based on the tags I associate with each page. It works great when my tags are single words.
In this case, posts containing any one of the above tags appear on my page.
However, I want to create and select tags for posts that contain multiple words (e.g. movie titles) such as Eternal Sunshine of the Spotless Mind, Garden State, The Aviator.
Currently, I apply tags via checkboxes (in case that's relevant), not using the autocomplete.
When I do this, the views filter only displays content containing the FIRST tag. For example, Only content using the Eternal Sunshine of the Spotless Mind tag is displayed, but content containing the other two tags is absent.
Any idea why this might be? Are my tags just too long? If so, how can I get around this? The following are the contextual filter settings.
Answer: I found Only works with first taxonomy term views3 D7 which is about the same issue you have.
This is what comment #18 and #20 say. You might also look at comment #28.

This works very well, but it only works with the first tag/taxonomy term applied to a node. For instance, if a post was tagged Cheese, Recipe, Tomatoes, and Pasta, it would show other posts tagged with Cheese, but not those tagged with Recipe, Tomatoes, or Pasta. Is there a way to get it to consider all taxonomy terms, not just the first one?

I just tried this solution and it seems to work. Just select "Reduce Duplicates" then under "More" select "Allow multiple values." That's it, you should have all the things being printed out and not just things related to the first term. Let me know if this helps.
Comment: You are awesome, thank you SO much. Worked like a charm.


Error: Socket hang up inside google cloud (nodejs) function

Question: I'm using a google cloud function to get an token from Firestore, check if it is still active, if so return access token. Else:

Get refresh token from secretmanager
Refresh
Update refresh token in secret manager
Update access token in firebase

Now i get this error sometimes... This function is minimally called every 15 minutes from an different cloud function. The error occurs like 5 times a day. I've seen this post but can't wrap my head around to get this translated to my code.
Error:
FetchError: request to https://europe-west1-project.cloudfunctions.net/getToken failed, reason: socket hang up
        at ClientRequest.<anonymous> (/workspace/node_modules/node-fetch/lib/index.js:1461:11)
        at ClientRequest.emit (events.js:315:20)
        at ClientRequest.EventEmitter.emit (domain.js:467:12)
        at TLSSocket.socketOnEnd (_http_client.js:493:9)
        at TLSSocket.emit (events.js:327:22)
        at TLSSocket.EventEmitter.emit (domain.js:529:15)
        at endReadableNT (internal/streams/readable.js:1327:12)
        at processTicksAndRejections (internal/process/task_queues.js:80:21)

Code:
const { SecretManagerServiceClient } = require("@google-cloud/secret-manager");
const client = new SecretManagerServiceClient();
const name = `projects/${process.env["PROJECT"]}/secrets/TOKEN/versions/latest`;

const axios = require("axios");
const milliseconds = new Date().getTime()
const now = Math.round(milliseconds / 1000);

const { Firestore } = require("@google-cloud/firestore");
const firestore = new Firestore();
const tokens = firestore.collection("tokens");

module.exports = async function getToken() {
  let responseData;

  await tokens.where("system", "==", "x").limit(1).get()
    .then((res) => {
      res.forEach((snap) => {
        responseData = snap.data();
      });
    })
    .catch((err) => {
      console.error(JSON.stringify(err));
    });

  console.log(`custom_log: now = ${now}`)
  console.log(JSON.stringify(responseData))

  if (now - responseData.timestamp < 250) {
    console.log(`custom_log: Returning access token from Firebase`)
    return responseData.access_token;
  } else {
    console.log(`custom_log: Return new access token`)
    return await refreshToken();
  }
};

async function refreshToken() {
  const [version] = await client.accessSecretVersion({ name: name });
  const secret = version.payload.data.toString();

  const config = {
    method: "post",
    url: "https://login.webpage.com/token?grant_type=client_credentials",
    headers: {
      Authorization: `Basic ${secret}`,
    },
  };
  return axios(config)
    .then((response) => {
      updateAccesToken(response.data.access_token)
      return response.data.access_token;
    })
    .catch((error) => {
      console.error(`custom_error: Error refresh token: ${error.message}`);
    });
}

async function updateAccesToken(token) {
  await tokens.doc('token').update({
    timestamp: now,
    access_token: token,
    dateTime: new Date(milliseconds).toISOString()
  })
  console.log('custom_log: access_token updated in Firestore')
}

Comment: For HTTP triggered Cloud Functions [require the response to be sent](https://firebase.google.com/docs/functions/http-events#terminate_http_functions), otherwise you should make sure that you're returning a correct Promise to terminate the function, is your `return responseData.access_token;` getting called correctly? (it doesn't seem to be a valid Promise). I can't debug it since you have multiple calls to multiple services that could fail and return an incorrect response, I suggest refactoring your code to return a proper value (a Promise is expected and not just log it to the console)
Answer: 2021/09/13 updated, even i made a new cloud function without setting the timeout (with the same codes), the error doesn't occur anymore; I updated google-auth-library from 6.1.6 to 7.9.1 just in case, and so far it works
2021/09/10 updated, in order to test how much time my program needs, i changed timeout back to 60 secs, but it still works...finding way to reproduce now
the original message:
I encountered the same error at cloud function (it works in local env).
After I set the timeout of cloud function to 80 secs, the error seems resolved.
These are the steps my program does, just for your reference

getting data from secret manager
using google-api to read 2 spreadsheets
using google-api to update 1 spreadsheet

With the previous version (getting data from secret-manager and then read 1 spreadsheet and update it), this error didn't occur.


C without stdio, what is possible?

Question: I've been interested in programming an operating system for some time.  Delving through a few different sites, I've come across an interesting concept (to paraphrase): if you start writing your bootloader with #include, you've already made a fatal mistake.
I've gone through K&R, and the entire book includes it throughout each lesson.  Having used it throughout learning C, I don't know what I've learned that uses stdio and what doesn't.  What can you do in C without stdio?
Comment: No `#include` meaning a bootloader shouldn't use any functions from the C standard library? Are you supposed to roll your own instead? I'd argue that doing that would be a sure way to make your bootloader buggy.
Comment: 'stdio' - did you mean 'C runtime library'?
Comment: @Praetorian - I believe the problem is that many of the functions in stdio are implemented in terms of system-calls to the OS.  Any such function will self-destruct if called in an environment like a bootloader in which there is no OS to provide a system-call API to invoke.  Either the bootloader must provide this API itself, or it must avoid calling any such functions until after it has loaded an OS that does.  So in that sense, yes, you are supposed to roll your own if you require them.
Comment: @aroth, yes, but there are many libraries suitable for embedded environments (i.e. no OS). See uclibc and newlib for the leading open-source candidates.
Answer: The C standard (ISO/IEC 9899:1999) recognizes two types of implementation (§4 Conformance, ¶6):

Freestanding - where the implementation (compiler plus library) only provides seven headers:
<float.h>
<iso646.h>
<limits.h>
<stdarg.h>
<stdbool.h>
<stddef.h>
<stdint.h>

These provide basic facilities for the language and declare no functions (the facilities in <stdarg.h> are explicitly defined as macros in the standard).  Note that it does not include complex numbers.
Hosted - where the implementation provides the complete library defined by the standard.

The whole point of a freestanding implementation is to allow you to write any code you need unencumbered by the standard library in general, and the standard I/O functions specifically.  The downside is that you not only can but must provide those functions - or make use of alternatives provided by the implementation.  Note that in a freestanding implementation, the entry point for the program need not be called main.
What you are seeking is a freestanding implementation - or to use the freestanding portion of a hosted implementation.  You will use headers (unless you're insane), but they probably won't be the standard C library headers other than those listed for use with a freestanding implementation.
Comment: In practice, you are most likely to get a full implementation of the C standard library (not the minimal subset defined for freestanding environments) with various hooks defined to implement the required low-level functionality. This allows you to implement any subset of the standard library you need.
Comment: This isn't really accurate, those headers are the necessary minimum for a freestanding compiler to implement. In reality, most freestanding compilers will implement the whole ISO C. Where you get in trouble is when you take a compiler for a hosted system and try to write programs for a freestanding system, as there might be API calls inside the libraries.
Answer: stdio stands for Standard Input/Output. As the name said, it contains things related to standard IO. The Wikipedia article of stdio.h lists the contents of stdio.h. You will need stdio.h if you use them. And you can check the man page of stdio.h too for further details.
And for the OS part, writing an OS is much more than simple programming, even if it is an academic one. Before jumping to that you should learn data structures, algorithms, OS theories and much more. The Design of the UNIX Operating System is a very good book to learn OS. Nachos is an academic OS simulation program. You can check that too. And if you are fanatic about OS, then you should read the autobiography of Linus Torvalds Just for Fun. Well, it's not a technical book, but you will get a feeling what it means writing an OS.
Comment: Definitely going to pick up the book, thank you for the feedback as well.
Answer: Any bootloader will almost certainly have #include directives, unless it is poorly designed. I don't know where you got that quote, but perhaps you misunderstood it. A bootloader will invariably start with a piece of pure assembly code for the initial low-level processor initialization and to initialize the C runtime environment. This code is simply not possible to write in C. But the rest of the code can be as complex as needed, within the restrictions of the environment (memory, etc).
stdio is simply a set of file streams connected to some kind of device which can receive input and output. You can implement stdin, stdout, stderr or any subset of them. You also may implement a filesystem for which you can open arbitrary file streams. In a modern OS, this ends up connected to some kind of terminal typically, which involves quite a bit of layers in-between because it's a virtual terminal that shows up on your monitor and thus requires a graphics driver and so forth. On a primitive embedded system, stdio might be connected to a serial port, or an LED display.
stdio is implemented with the read() and write() functions. If these are not implemented, you simply can't use the stdio functions like printf(), fprintf(), fgets(), etc. That doesn't mean you can't write to your graphics display, serial port or whatever else. It just means you don't have a standard facility to do so and will need to call custom functions.
To answer your question: you can do anything in C without stdio. It's all written in C anyway, you're just losing some common functionality implemented in any C standard library.
Answer: The other answers about minimal C environments are good, but here's another more abstract angle.
The C standard defines two kinds of "observable side-effects", which basically means things your program does that cannot be optimized away or otherwise modified by the compiler. These are firstly calls to I/O functions, and secondly reads and writes of volatile objects.
If you don't have access to stdio or other parts of your C implementation that rely on the presence of the OS, you can't use I/O functions. That leaves reads and writes to volatile objects as the only things that your program will actually "do". In practice, you may or may not have to actually mark everything volatile, since your C implementation probably provides guarantees beyond the standard. If there's a CPU instruction that makes a beep noise, your compiler will treat that (and inline assembly in general) like an I/O function even though it's not really a function, and so on.
In other words, you will have to use low-level hardware access to achieve whatever it is your bootloader is supposed to achieve (which amounts to: display a message on screen, establish some resources that the OS needs in order to run, load it and run it). This access is of course entirely implementation-dependent - hardware vendors might provide libraries to help you, they just won't be the standard C headers.
Answer: As taskinoor mentions, Wikipedia provides an excellent reference about what is included in stdio.h.
As for what's possible without stdio.h, pretty much everything that is possible with it, although you will have to work a lot harder for certain things.  If you want to use files, for instance, you will have to define your own abstraction of a file, and your own interface for working with it.  If you want to read input from the keyboard then instead of calling scanf() you'll have to use assembly code to call a BIOS interrupt.
Such is the nature of programming an OS.  None of these higher-level constructs exist until you define them and the API for working with them.  You need to start with the smallest basic pieces of functionality, and start grouping them together to create increasingly more useful and complex constructs.

C without stdio, what is possible?


Unknown Variable '$L$' encountered in equation

Question: I was going through S.L. Loney's Plane Trigonometry-Part 1.
I encountered this equation over here :
$$ \sin{B} = \frac{b}{c}$$
Where $B$ is an angle of a Triangle $ABC$, $b$ and $c$ respective sides opposite to angles $B$ and $C$. ($c$ - hypotenuse , triangle is right angled)
Now , the problem arises when : the equation takes form -
$$L \sin{B} = 10 + \log{b} - \log{c}$$
I know he is taking $\log$ on both sides, but I really don't know what '$L$' is for and how $10$ came into the situation. Kindly help. Is he taking $\log$ to the base of $10$ or $e$? Kindly help, I really cannot sort how '$L$' came into the situation.
Comment: You'll need to give more context if you want help understanding this. No one can guess the meaning of a variable without context.
Comment: Can it be a typo and $L$ be actually $\log$?
Comment: @Patricio no it isn't . I've seen this in other proofs by S.L. Loney too in the same book.
Answer: The meaning of the notation is explained on pp.166-67.
Comment: thanks for your help !

Unknown Variable ‘$L$’ encountered in equation


Finding an upward-opening catenary passing through $(x_1,y_1),(x_2,y_2)$ with length $L$

Question: I want an equation $y=a\cosh(\frac{x-b}{a})+c$ for a catenary opening upward that passes through $(x_1,y_1)$ and $(x_2,y_2)$. WLOG, assume $x_2>x_1$.
Let $L$ be the length of the catenary between $(x_1,y_1)$ and $(x_2,y_2)$
Comment: See this question and answer of mine:
https://math.stackexchange.com/questions/3557767/how-to-construct-a-catenary-of-a-specified-length-through-two-specified-points
Comment: If $L$ is greater than the distance between the two points, then the low point of the catenary (assuming it opens up, so $a > 0$), which occurs at $x = b$, lies between $x_1$ and $x_2$. Thus $(x_1 - b)/a < 0$. But $\cosh^{-1}$ only returns *positive* values. Thus $b = x_1 + a\cosh^{-1}((y-c)/a)$, not "$-$" as you claim.
Comment: @PaulSinclair The low point of the catenary (where the tangent line has slope zero) can occur anywhere with respect to $x_1$ and $x_2$. $x_1$ and $x_2$ are just two points that reside somewhere on the catenary. I did not specify that the low point of the catenary would have to occur at an x value between $x_1$ and $x_2$. I left the order intentionally ambiguous to allow for greater generality.
Comment: You've ignored the condition that I lead with: $L$ is greater than the distance between the two points. However, I will grant you that this limit was not fully sufficient for the claim. It does require a somewhat greater length to ensure the low point is between $x_1$ and $x_2$. But In cases where the length is long enough, the low point will occur between $x_1$ and $x_2$, and then your formula **gives the wrong result**. You did not allow for greater generality. What you did was ignore the fact that $\cosh$ is not a 1-1 function, so only has a limited inverse.
Comment: @PaulSinclair When I looked back over this post I did realize that I had unwittingly proceeded with my formula without acknowledging that since $cosh$ can't pass the vertical line test its inverse will be, as you say, limited. What I'll try to do now is create the generality I desire by the appropriate placements of ± when b is in a given interval with respect to $x_1$ and $x_2$
Comment: @PaulSinclair and I'll also be trying to derive a formula for the length needed in order to ensure that the low point occurs between the two specified points. I imagine such a formula has long since existed in mathematics and been used in physics calculations.
Comment: horizontal* line test
Comment: See https://physics.stackexchange.com/a/279247/392 for the equations
Answer: Take the horizontal separation (the span) $S = x_2-x_1$ and the vertical separation $h = y_2-y_1$.
But you also need the catenary constant $a$ which describes the curvature of the curve (in units of length). If total length $L$ is given then one needs to solve for $a$ in
$$ L = a \left(  \sinh \left( \frac{ x_2 - x_c}{a} \right) - \sinh \left( \frac{x_1-x_c}{a} \right) \right) \tag{1} $$
Where the lowest point is $(x_c,y_c)$ that is evaluated with the following exact equations.
$$ x_c =x_1 + \frac{S}{2} +a\, \sinh^{-1} \left( \frac{h \hat{e}^{S/(2a)}}{a(1-\hat{e}^{S/a})} \right) \tag{2}$$
and
$$y_c = y_1 -a \left( \cosh \left( \frac{x_c-x_1}{a} \right) -1 \right) \tag{3}$$
So if $L$ is given, one must solve (1) and (2) simultaneously which is not possible to do analytically because (1) is of the form of $y = x \exp(x)$ which is unsolvable for $x$.
The combined (1) and (2) equations are
$$ L = 2 a \sinh \left( \frac{S}{2 a} \right) \sqrt{1 + \left( \frac{h \hat{e}^{S/(2a)}}{a(1-\hat{e}^{S/a})} \right)^2} $$
which must be solved for $a$ when $L$ is given. But in many cases, the horizontal tension $H$ and the unit weight of the cable is given, providing with a direct estimation of $a = \frac{H}{w}$.
The equation for the curve is
$$ \boxed{ y(x) = y_1 + a \left( \cosh \left( \frac{x-x_c}{a} \right)- \cosh \left( \frac{x_1-x_c}{a} \right) \right) } \tag{4}$$
You can try for yourself to see that $y(x_1) = y_1$ and $y(x_2)=y_2$
An example of the equations above is shown below

taken from my answer(s) in SE #1 and SE #2
Example is $(x_1,y_1) = (50,15)$ and $(x_2,y_2) = (450, 45)$ with a catenary constant $a = H/w = 300$

These points result in
$$S=450-50 = 400$$
and
$$h = 45-15 = 30$$

The catenary constant

The lowest point is
$$x_c = 50 + \frac{400}{2} + 300 \sinh^{-1} \left(  \frac{30 \exp(400/(2\,300)}{300 (1-\exp(400/300))} \right) = 50 + 200 - 20.89897 =  229.101028 $$
$$y_c = 15 + 300 (1-1.183563) = -40.06883 $$

The shape for this example is
$$ y=15+300 \left( \cosh \left( \frac{x}{300}-0.7636700940207649 \right)-\cosh \left(-0.5970034273540983\right) \right) $$

You can delve into C# code to handle catenaries in my GitHub - CatenaryCalculator.cs
<code>/// <summary>
/// Calculate the lower point on the catenary (center point)
/// </summary>
/// <param name="span">The span</param>
/// <param name="w">The unit weight</param>
/// <param name="H">The horizontal tension</param>
/// <returns>A position vector in (x,y) coordinates</returns>
public static Vector2 CenterPosition(Vector2 span, double w, double H)
{
    double a=H/w;
    double η=span.X/(2*a);

    double xc=span.X/2+a*DoubleEx.Asinh((span.Y*Math.Exp(η))/(a*(1-Math.Exp(2*η))));
    double yc=-a*(Math.Cosh(-xc/a)-1);

    return new Vector2(xc, yc);
}
</code>
A Desmos implementation of the above linked. Change the parameter $t=\frac{S}{2a}$ to get the length required.
Comment: I've saved a modified copy of the desmos solver you linked, where instead of fixing the left endpoint to the origin you can choose each of $x_1$, $x_2$, $y_1$ and $y_2$, keeping in mind that we assume $x_2>x_1$.  Here it is https://www.desmos.com/calculator/pd3pgnerks
Comment: Do these formulas hold if I construct an example where the lowest point of the catenary occurs at an x value less than $x_1$ and less than $x_2$?
Comment: Yes. By increasing the catenary constant, for example, it will move the lowest point to the left past the support to cause tower uplift. This happens when $x_c < x_1$. Do you want to see the rest of the equations describing the length, tension, sag for an uneven span?
Comment: I do, but now I'm a bit confused. In the problem that I originally described, we're dealing with an abstract, platonic, mathematically ideal string. It's my understanding that no physics variables ought to be involved. In fact, the only variables that should have any impact on the equation we end up with are the 2 chosen points as well as the length of catenary between the two points. If you first choose the 2 points and then you vary the length of the string, the length will determine where $x_c$ ends up with respect to $x_1$ and $x_2$. That's why my answer needs only the points and length L
Comment: The catenary constant $a$ is a purely geometrical constant with units of length. If you know the total length $L$ you need to find the corresponding catenary constant $a$, but there is no analytical solution for this. For the flat case this is $$ L = 2 a \sinh \left( \frac{S}{2 a} \right) $$
Comment: Oh, ok. That makes sense. What I'm still wondering is why my answer involves so few variables. My answer must be wrong, then? I've provided the equation solved for $y$ for each of the 5 possible cases. You could then take that equation and derive the corresponding equation for the arc length from $x_1$ to $x_2$. Then, set that equal to $L$ to obtain an equation that relates the length $L$ to the free variable that I've called $c$. Solving for $c$ given $L$ would not be analytical, but it would be all you need to do in order to obtain the right equation for your catenary
Comment: My original hope when beginning this problem was that closed form equations for $a$, $b$, and $c$ in terms of length $L$ could be obtained. I see that that's not possible. Nonetheless, I still firmly believe that after you choose your two points, the only other thing you need to choose in order to determine a unique catenary is the length $L$. The unit weight of the cable and the horizontal tension would not be appropriate to take into consideration.
Comment: @SimonM after you have the endpoints, what you need to do is **define the constant $a$** for a unique catenary. Remember that $a$ has a geometric meaning, and it is related to the directrix of the curve. The length, curve, slope etc are all a function of $a$.
Comment: I prefer to think of defining the length $L$, and then $a$ is a function of $L$
Comment: I think in order to clear up my confusion I would like to know what parts of my answer are wrong, if any
Comment: @SimonM - there is no function of $a$ in terms of $L$. Only a function of $L$ in terms of $a$. That is the nature of the problem. I have worked with the sag of transmission lines for many years and it always involves numerical methods to find a solution.
Comment: I misspoke. What I mean to say is that $a$ can be found in terms of $L$.
Comment: After I have chosen the endpoints, would it suffice to simply choose a length $L$ in order to determine a unique catenary? Instead of choosing a value for the catenary constant?
Comment: @SimonM - as you know even the end points are not needed. To parameterize a catenary you need three values. The (x,y) coordinates of the lowest point and the catenary constant. If you are asking if you can derive these three parameters from the end points and the length, yes you can as an approximation. For _exact_ values you need the end points and the constant.
Comment: There is another reason not to pick the length $L$ as a parameter because the allowed values are limited and it needs to be specified very precisely. The range is $\sqrt{S^2+h^2} < L < \infty$ but only values near the lower limit make much sense. On the other hand, picking $a$, or in fact picking $\eta = \frac{S}{2 a}$ as a parameter is much better because it is dimensionless, it range is all positive real numbers and the smaller the number near zero the more straight the catenary is.
Comment: [Look at this Demsos](https://www.desmos.com/calculator/gvu9m3iy0n) solver where you manipulate _one_ parameter (called $t$ here) to set the length. Without loss of generality, you can put the coordinate origin at the left support and specify only $S$ and $h$ the span length and height difference.
Answer: WLOG, assume $x_2>x_1$.
Let $L$ be the length of the catenary between $(x_1,y_1)$ and $(x_2,y_2)$
Your particular choices of $x_1$, $x_2$, $y_1$, $y_2$ and $L$ will determine which of the following 5 cases applies to you and your catenary.
Case 1:

$y_1<y_2$ and $\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}<L<k\sinh(\frac{x_2-x_1}{k})$, such that $k$ is the unique solution to $$0=x\cosh(\frac{x_2-x_1}{x})+y_1-x-y_2$$

The length $L$ is not enough so that the low point must occur inside of the interval from $x_1$ to $x_2$, and $y_1<y_2$

These two conditions mean that the low point of the catenary occurs at an $x$ value less than $x_1$

Define:
$$g(x)=x_1-x\cosh^{-1}(\frac{y_1-c}{x})$$
$$f(x)=x_1-x_2+x(\cosh^{-1}(\frac{y_2-c}{x})-\cosh^{-1}(\frac{y_1-c}{x}))$$
Then:
$$a=f^{-1}(0)$$
$$b=g(a)$$
Then, define:
$$\alpha(c)=f^{-1}(0)$$
$$\beta(c)=g(\alpha(c))$$
Then an upward-opening catenary through the desired points can be generated by choosing a value of c for the function
$$y=\alpha(c)\cosh(\frac{x-\beta(c)}{\alpha(c)})+c$$

Case 2:

$y_1<y_2$ and $L=k\sinh(\frac{x_2-x_1}{k})$, such that $k$ is the unique solution to $$0=x\cosh(\frac{x_2-x_1}{x})+y_1-x-y_2$$

The length L is exactly enough so that the low point of the catenary must occur at either $x_1$ or $x_2$

These two conditions mean that the low point is at $(x_1,y_1)$

The upward-opening catenary through the desired points can be generated by the function
$$y=k\cosh(\frac{x-x_1}{k})+y_1-k$$
where $k$ is the unique solution to
$$0=x\cosh(\frac{x_2-x_1}{x})+y_1-x-y_2$$

Case 3:

$L>k\sinh(\frac{x_2-x_1}{k})$, such that $k$ is the unique solution to $$0=x\cosh(\frac{x_2-x_1}{x})+y_1-x-y_2$$

The length $L$ is enough so that the low point must occur inside the interval from $x_1$ to $x_2$

These two conditions mean that the low point of the catenary occurs at an $x$ value that satisfies $x_1<x<x_2$

Define:
$$g(x)=x_1+x\cosh^{-1}(\frac{y_1-c}{x})$$
$$f(x)=x_1-x_2+x(\cosh^{-1}(\frac{y_2-c}{x})+\cosh^{-1}(\frac{y_1-c}{x}))$$
Then:
$a$ is either zero of $f$ and
$$b=g(a)$$
Then, define:
$\alpha(c)$ is either zero of $f$ and
$$\beta(c)=g(\alpha(c))$$
Then an upward-opening catenary through the desired points can be generated by choosing a value of c for the function
$$y=\alpha(c)\cosh(\frac{x-\beta(c)}{\alpha(c)})+c$$

Case 4:

$y_1>y_2$ and $L=k\sinh(\frac{x_2-x_1}{k})$, such that $k$ is the unique solution to $$0=x\cosh(\frac{x_2-x_1}{x})+y_2-x-y_1$$

The length L is exactly enough so that the low point of the catenary must occur at either $x_1$ or $x_2$

These two conditions mean that the low point is at $(x_2,y_2)$

The upward-opening catenary through the desired points can be generated by the function
$$y=k\cosh(\frac{x-x_2}{k})+y_2-k$$
where $k$ is the unique solution to
$$0=x\cosh(\frac{x_1-x_2}{x})+y_2-x-y_1$$

Case 5:

$y_1>y_2$ and $\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}<L<k\sinh(\frac{x_2-x_1}{k})$, such that $k$ is the unique solution to $$0=x\cosh(\frac{x_2-x_1}{x})+y_1-x-y_2$$

The length $L$ is not enough so that the low point must occur inside of the interval from $x_1$ to $x_2$, and $y_1>y_2$

These two conditions mean that the low point of the catenary occurs at an $x$ value greater than $x_2$

Define:
$$g(x)=x_2+x\cosh^{-1}(\frac{y_2-c}{x})$$
$$f(x)=x_1-x_2+x(\cosh^{-1}(\frac{y_1-c}{x})-\cosh^{-1}(\frac{y_2-c}{x}))$$
Then:
$$a=f^{-1}(0)$$
$$b=g(a)$$
Then, define:
$$\alpha(c)=f^{-1}(0)$$
$$\beta(c)=g(\alpha(c))$$
Then an upward-opening catenary through the desired points can be generated by choosing a value of c for the function
$$y=\alpha(c)\cosh(\frac{x-\beta(c)}{\alpha(c)})+c$$
Comment: How do you know this answer is correct? have you tried an example?
Comment: Yes. I have provided 5 examples in desmos with the 5 links for the possible types of cases. What I did for these examples is manually find the x-intercept(s) of $f$ and then apply my formulas
Comment: @JohnAlexiou   [1]: https://www.desmos.com/calculator/xsvg0tatvd
  [2]: https://www.desmos.com/calculator/odfgmngdww
  [3]: https://www.desmos.com/calculator/hsq8eeul0q
  [4]: https://www.desmos.com/calculator/nzglfzjp0o
  [5]: https://www.desmos.com/calculator/epfhpmrjkb
Comment: I see 6 inputs, plus an eyeballing of $a$ to get it to work. You wanted a solution with 5 inputs (two point coordinates + length) and I gave you one with 5 inputs. Plus the complexity of different cases is not needed. One solution works for all cases.
Comment: @JohnAlexiou The 5 inputs are $x_1$, $x_2$, $y_1$, $y_2$ and $c$, where $c$ is a quantity related to the length $L$. The $k$ you see is actually a leftover bit unrelated to everything else. I had simply forgotten to delete it. Here is the updated graph https://www.desmos.com/calculator/8njkvoicuy
Answer: WLOG, assume $x_2>x_1$.
Let $L$ be the length of the catenary between $(x_1,y_1)$ and $(x_2,y_2)$
Define:
$$S=x_2-x_1$$
$$h=y_2-y_1$$
$$A(t)=\frac{S}{2t}$$
$$B(t)=\frac{S}{2}+A(t)\sinh^{-1}\left(\frac{he^{t}}{A(t)(1-e^{2t})}\right)=\frac{S}{2}+A(t)\sinh^{-1}\left(\frac{-h}{2A(t)\sinh\left(t\right)}\right)$$
Such that $t$ is the solution to $$L=2A(t)\sinh\left(t\right)\sqrt{1+\left(\frac{he^{t}}{A(t)\left(1-e^{2t}\right)}\right)^{2}}=A(t)\sinh\left(\frac{S-B(t)}{A(t)}\right)+A(t)\sinh\left(\frac{B(t)}{A(t)}\right)$$
Then the equation for the catenary is
$$ \boxed{ y=A(t)\left(\cosh\left(\frac{x-B(t)-x_{1}}{A(t)}\right)-\cosh\left(\frac{B(t)}{A(t)}\right)\right)+y_{1} }$$
Let k be the solution to
$0=x\cosh\left(\frac{x_{1}-x_{2}}{x}\right)-|y_{2}-y_{1}|-x$
If $L>k\sinh\left(\frac{x_{2}-x_{1}}{k}\right)$ then the lowest point of the catenary will occur between $x_1$ and $x_2$
If $L=k\sinh\left(\frac{x_{2}-x_{1}}{k}\right)$ then the lowest point of the catenary will occur at whichever is lower of the two endpoints
If $L<k\sinh\left(\frac{x_{2}-x_{1}}{k}\right)$ then the lowest point of the catenary will occur outside of the interval from $x_1$ to $x_2$


Can odds ratios be negative?

Question: Since the odds ratio is defined as
$OR = \frac{P(A)}{1-P(A)} / \frac{P(B)}{1-P(B)}$, where
$P(A)$ and $P(B)$ are between 0 and 1
How can an odds ratio be negative?
Thanks
Answer: It cannot be negative. However, the (often natural) logarithm of it can be. Usually, an odds ratio is actually derived on the log-transformed scale (where confidence intervals derived based on log-odds ratio $\pm 1.96 \times $ SE work better) and estimate & confidence intervals are the transformed onto the odds ratio scale for reporting.
Answer: It simply can not be negative.
Perhaps you read somewhere about negative values of logarithm of odds rato or the logit function defined as $\ln(p)$ where $p$ is $\frac{OR}{1+OR}$

Can odds ratios be negative?


Expanding multiple brackets is it 'better' to do them in a particular order?

Question: Just watching a tutorial which explained how to expand:

-2(3a-2)(4a-1)
  and
(5x - 2)(x - 2)(2x + 1)

In both cases the tutorial recommends to start with solving the rightmost pair first. Obviously (as the tutorial says) there is no mathematical reason to do this,  but are there any rules of thumb/guidelines on which pair to pick for ease of computation?
My instinct is that you should start with the two most complex, and work backwards in terms of complexity.
Answer: There aren't really general rules of that kind.
I would tend to do the simplest ones first to keep the arithmetic as simple as possible for as long as possible. And if, like the first example, there are constants there, put them in at the end.
Also look for pairs of the form $(ax+b)(ax-b)=a^2x^2-b^2$ which lose a term from the calculation.
As an example, to compute the product of three successive integers I would write it as $(n-1)n(n+1)=n(n^2-1)=n^3-n$ rather than $n(n+1)(n+2)$. In this way I would use the choice I have to write a simpler form of product.
But others may have different hints.


How are dotted eighth-sixteenth note patterns counted in compound meter?

Question: As someone who was taught to count compound meter like this:

How would this system count the following patterns whilst still keeping the beat on the dotted quarter note?
Answer: I learned the same way of counting, and I've found two solutions that work well for me:

Count each eighth note as a beat. Thus a regular 6/8 measure would be 1 - 2 - 3 - 4 - 6, and a measure with the dotted rhythms illustrated in the question would be 1 (and 2) and 3 4 (and 5) and 6.

The Rhythm Book, by Richard Hoffman, uses a system in which triplets (and thus compound meter) are Ta - Ki - Da, and sextuplets are Ta - Va - Ki - Di - Da - Ma. It took a while to get used to his mnemonics, but now I prefer them a great deal. So, the dotted rhythm would be Ta - Di - Da.

Here's a scored illustration of both approaches:
Answer: In my experience, counting is primarily supposed to represent the meter, not the rhythm.  So the best thing to do would be to count straight eighth notes while playing the sixteenth notes halfway between "and" and "a."
Beginners may have trouble with this, though, and the best solution will depend on the tempo and the individual.  For slow practice, at least, you can introduce another syllable, perhaps "one and-y-a, two and-y-a," etc., or you could count it in twelve: one two-and-three, four five-and-six," etc.
For faster practice, you can try using three syllables per beat in the dotted rhythm, but that tends to lead to the sixteenth note getting accented, which is generally to be avoided.
Comment: This pattern is sometimes called 'Siciliana' (https://en.wikipedia.org/wiki/Siciliana). In French, we use 'Si-Ci-Lienne' as the three syllables you mention, I guess one could use 'Si-Ci-Lia' in English.


How to interpret OR (odds ratio) decimals? Are they percentages?

Question: How to interpret OR (odds ratio) decimals? Are they percentages?
OR with probabilities $p_1,p_2$ for two events is
$$\frac{\frac{p_1}{1-p_1}}{\frac{p_2}{1-p_2}}$$
Like if one gets an OR of, say, $2.25$, then does this mean that the nominator event is 225% more likely than the denominator event? What does "odds" mean?
Comment: It might help to give you a concrete answer if you said what the events and groups are. As it stands all we can say is that the odds of something in one group are 2.25 times the odds of the something in some other group.
Comment: @mdewey Well in this case I get 2.25 if I take the odds ratio of the chance of getting skin cancer when taking beta carotene ($p_1$) and not taking ($p_2$).
Comment: So whatever the odds were of getting skin cancer in the beta carotene group (ie the ratio of those who did to those who did not) it was 2.25 times the odds in the group not getting it.
Comment: @mdewey But is odds the same as "2.25 more likely to"?
Comment: If you measure likeliness in terms of odds then yes, if you measure likeliness in terms of risk then no you need the risk ratio unless you are interested in absolute risk in which case you need the risk difference.
Comment: @mdewey But what does "odds" mean?
Answer: It's betting odds of 2.25 to 1 (same as 9 to 4). In contrast what you describe is the relative risk ratio.
Comment: Yea so what does 2.25 to 1 mean?
Comment: In 9 out of 13 cases the first proportion is expected to be bigger in 4 out of 13 the second proportion. What that means in terms of relative risk ratios depends on the proportions we ate talking about.
Answer: The odds of an event are the ratio of the number of times it occurred to the number of time it did not. We can distinguish this from the risk which is the number of times it occurred as a proportion of the total number of trials. In order to compare the likeliness of an event in two groups we can either take the ratio of the odds in those two groups (which is the formula you quote), take the ratio of the risks, or take the difference in risk. All of these are in wide use. Each of them defines a way of interpreting the English phrase 'more likely'.


Tensorflow: Single GPU vs. Multi GPU

Question: Will it be better to use one good GPU like the Titan X with 12 GB Memory or two GTX 1080 with 8 GB Memory each to compute large CNNs.
Both will cost about the same.
Nvidia suggests the Titan X here, but why? What are the advantages having one GPU with much Memory than having two smaller GPUs which have more Memory when combined?
Comment: Most things in Tensorflow only use 1 GPU, you need to engineer things specially to use 2
Comment: Thank you! I should have read https://www.tensorflow.org/tutorials/using_gpu#using_multiple_gpus
Answer: So from my personal experience is tensorflow tends to only use one GPU. That might not always the case, but for the algorithms I'm running it seems to use only one.
Personally I would go for two gtx 1080 ti's or one gtx 1080ti. The cost for the titan for one more GB or gddr and a slightly faster bus doesn't make since.
I would go for two just so you can run multiple experiments at once.
8 Nvidia tesala's running tensorflow.

Tensorflow: Single GPU vs. Multi GPU


How does PCA gives centers for the Kmeans algorithm in scikit learn

Question: I'm looking at this example code given on Scikit Kmeans digit example
There is the following code in this script :
<code># in this case the seeding of the centers is deterministic, hence we run the
# kmeans algorithm only once with n_init=1
pca = PCA(n_components=n_digits).fit(data)
bench_k_means(KMeans(init=pca.components_, n_clusters=n_digits, n_init=1),
          name="PCA-based",
          data=data)
</code>
Why are the eigen vectors used as initial centers and is there any intuition for this?
Comment: also try http://stats.stackexchange.com/
Answer: There is a stackexchange link here, and also some discussion on the PCA wikipedia.
There is also an informative mailing list discussion  about the creation of this example.
All of these threads point back to this paper among others. In a brief, this paper says that there is a strong relationship between the subspace found by SVD (as seen in PCA) and the optimal cluster centers we seek in K-means, along with associated proofs. The key sentence comes in the lower right of the first page - "We prove that principal
components are actually the continuous solution of the cluster membership indicators in the K-means clustering method, i.e., the PCA dimension reduction automatically performs data clustering according to the K-means objective function".
What this amounts to is that SVD/PCA eigenvectors should be very good initializers for K-Means. The authors of this paper actually take things a step further, and project the data into the eigenspace for both of their experiments, then cluster there.
Comment: What this in my opinion demonstrates is that k-means doesn't actually discover *structure* in the data (i.e. clustering). It's more a projection technique (like PCA).
Comment: albeit a more flexible one. Orthogonality constraints lead to uninterpretable vectors quickly.
Comment: Thank you Kyle for the explanation. I was not aware of the mailing lists archive, that is an excellent pointer.


Asking a branching series of questions

Question: I want to be able to ask users a question, and, depending on their answer, go to some other question. Webforms does not handle this well, since it seems to assume that the user answers most of the questions. I'm looking for something like
What price range do you want?

If between $100 and $200, go to question 2
If between $200 and $300, go to question 3
If between $300 and $400, go to question 4

And so on.
At the end, I need to be able to see all of their answers in order to perform a
DB query, so their responses will have to be "accumulated" as they go. I don't
strictly need to have their answers stored in the DB, but it would be nice. I'm
fine with programming in a hackish special case with hardcoded targets, but a
clicky-button interface would be preferable, if such a module exists.
I feel like there should be some relatively-easy way of doing this, perhaps with
the Form API and fields on nodes, but I can't quite put my finger on how it
would be done.
Comment: I'm evaluating the options. Don't worry, I'll accept an answer once I'm done.
Answer: Look at webform conditional or update your webform to >4.0-alpha2.
I have done a 2-way or 3-way branch on a form with a few sub-pages of questions for each branch. It's kind of a pain but its very doable.
Webform results will show you only the answers they had to complete depending on conditions and validations.
Comment: I tried webform 4.0-alpha2, and the conditionals were too limited to solve my problem.
Answer: Try this module dquarks. Using this you can create surveys and quiz. Hope this helps.
Answer: You may take a look at the Field Conditional State module.

With field conditional state module you can set #state form field attribute based on value selected in other form fields.
Answer: I ended up going with the Mforms module. It has the flexibility to create more complicated flows beyond surveys and quizzes. Its included example module contains enough to get started.

Asking a branching series of questions


Probability of Observting a specific sample?

Question: I am reviewing the basic concepts of Calculus, Linear Algebra & Probability and Statistics for Graduate Courses. I learned many of these courses back in high school quite a while back and they are a bit rusty. While revising Probability part I came across one particular question that is confusing to me and I couldn't find relevant information.
So the question is that a coin x is tossed 5 times and following sample is observed: {1, 1, 0, 1, 0} where 1 = head ; 0 = tail
The question related to this data are firstly find the probability of observing this data. This part is simple since all possible spaces can be 2^5 = 32 and this particular one has a probability of 1/32 right? (assuming that the question is asking for this particular setting and not that there should be three heads and 2 tails). Any comments?
The second part is what is confusing it says that since the probability distribution is equal we got above probability, but if the probability of getting heads was not 0.5 the probability observed in previous question would change and I am supposed to find a distribution that maximizes the probability of the sample. I am confused? Does probability distribution effect the observed sample's probability? If so then how can this be solved? Can anyone point me to some material that I can review to refresh my knowledge?
Answer: This can be solved by setting variable $p$ as probability on e.g. a head.
Then the probability of observing this data is $p^3(1-p)^2$ and you are asked to find the maximal value of this polynomial in $p$ under the extra condition that $p\in[0,1]$.
So at this point we somehow leave "probability theory" and enter "calculus".
Can you do that yourself?
Comment: Oh I see! So the way I was thinking about the first part it was actually wrong. I should be thinking in terms of your given polynomial. Thank you! You have pointed me the right direction.
Comment: Nice to hear. You are welcome.


Finding arc length parametrization of a cycloid

Question: Find an arc length parametrization of the cycloid with parametrization
r(t)= .
I took the derivative and found the speed to be sqrt(2(1-cost))but now I'm unsure how to integrate that to get s. How do you integrate sqrt(2(1-cost))?
Comment: Ask more precisely: Which function you have to integrate?
Comment: https://proofwiki.org/wiki/Length_of_Arc_of_Cycloid
Answer: Try this one
$(1-\cos t) = 2 \sin ^2(\frac{t}{2})$
Answer: Hint: If ${\bf r}: I \to \Bbb R^n$ is a curve, it's arclength function is given by: $$s(t) = \int_{t_0}^t \|{\bf r}'(\xi)\|\,{\rm d}\xi.$$
If $0 \in I$, we usually take $t_0 = 0$, for simplicity. Solve the integral, and you will have $s$ as a function of $t$. Solve for $t$ in terms of $s$.


Javascript - Invoking a function if there is a fragment in the url

Question: I have a carousel with various magazines and on click on each of the magazines in the carousel, I scroll down on the page to the section where I show the details of the magazine that was clicked. I pass the magazine details and create the elements according to the passed values.
This is the html:
        @foreach($issues as $issue)

first()->magazine->id }}'
              data-name='{{ $issue->first()->magazine->name }}'
              data-summary='{{ $issue->first()->magazine->summary ?: '' }}'
              data-magazine-image='{{ $issue->first()->magazine->image ?: '' }}'
              data-issue-image='{{ $issue->first()->image  }}'
            >
              first()->image  }}">

{{ $issue->first()->magazine->name }}

                  Se magasinet

        @endforeach

So, then on click I call the function that calls the function that creates magazine details section:
$(function() {
  $('.js-magazine-selector').click(function(){
    var img = $(this),
        id = img.data('id'),
        name = img.data('name'),
        summary = img.data('summary'),
        magazineImage = img.data('magazine-image'),
        issueImage = img.data('issue-image'),
        page = img.data('page');

    $('#back').addClass('not-allowed');
    $('#forward').removeClass('not-allowed');

    showMagazineDetail(id, name, summary, magazineImage, issueImage, page);

  });
});

And this is the function that creates magazine details section:
var showMagazineDetail = function showMagazineDetail(id, name, summary, issueImage, magazineImage, page){
    images = [];
    nextImage = 0;
    imagesIndex = 0;
    loadedImages = [];
    scrollPoint = document.height;

    window.location.hash = name;
    $(".magazine-section").css('visibility', 'visible');

    $('#name').text(name);
    $('#summary').text(summary);
    $('#cover-image').attr({"src" : '/imagecache/cover/' + magazineImage, "alt" : name});

    if (issueImage != '') {
      $('#issueImage').show();
      $('#issueImage').attr({"src" : '/imagecache/medium/' + issueImage, "alt" : name});
    }else {
      $('#issueImage').hide();
      $('#issueImage').attr({"src" : '', "alt" : name});
    }

    // $('body').css('overflow-y', 'scroll');

    $('html,body').animate({
       scrollTop: $(".magazine-detail").offset().top - 80
    });

    $.getJSON("issue/images",
        { id: id },
        function(result){
          if (result.length < 2){
              $('.magazine-preview-nav').hide();
          } else {
              $('.magazine-preview-nav').show();
          }
          $.each(result, function(i, value){
              images.push(value);
          });
          preload();
    });
};

That works fine, but I need to add a feature that when a user passes the url with the hash fragment of the magazine name, that it invokes the function same as it would on clicking the magazine in the carousel. I just don't know how to pass other parameters that are needed for creating the magazine detail section, so I was thinking of creating a function where if there is a hash fragment on page load to find the element in the carousel with the same name as the one in the hash fragment and then execute the click function on it, but not sure how to do that, and if that is the best way to do it?
Answer: You can do something like this, on function ready, get the hash, select an element and then call the function like you said:
$(document).ready(function () {
  var name = window.location.hash.substr(1);

  if (name != '') {
    var selectedMagazine = $('[data-name="' + name + '"]'),
        id = selectedMagazine.data('id'),
        name = selectedMagazine.data('name'),
        summary = selectedMagazine.data('summary'),
        magazineImage = selectedMagazine.data('magazine-image'),
        issueImage = selectedMagazine.data('issue-image'),
        page = selectedMagazine.data('page');

    showMagazineDetail(id, slug, name, summary, magazineImage, issueImage, page);
  }
});

If there is any more efficient way of doing it I am not sure, but this will do the job for what you asked for.


drush "The drush command 'self-update' could not be found."

Question: I'm trying to figure out why many drush commands aren't working.
drush theme list also reports:

The drush command 'theme list' could not be found.  Run drush
  cache-clear drush to clear the commandfile cache if you have
  installed new extensions.

I'm mildly certain I installed drush via brew install drush.
Any tips?
Comment: drush pm-list --type=theme works but I don't see why drush theme list doesn't ... https://drupal.org/node/530780
Comment: Ah, I didn't realize http://www.drush.org/ hadn't been updated for awhile. That's too bad.
Answer: self-update was removed a while ago. I'm not sure what the official replacement is, but a simple git pull; composer install does the job for me with the latest version (run it in the drush lib folder, probably in /usr/local/Cellar if it was brewed).
theme list (and theme) don't appear in the list of available commands (although self-update does, ironically). Maybe you're looking for
drush pm-list --type=theme

Comment: I'm with Clive, (git's the best way to download and manage your drush instance) but I've never needed to run composer install. I `git clone from https://github.com/drush-ops/drush`. Then I can checkout any version of drush (sometimes functions don't work as expected) with `git checkout tags/7.x-5.9` or whichever is best.


Share same post and page data between 2 different wordpress site(different url)

Question: I have made 3 real estate demo website(with different design and layout) but due to single website hosting server I want to run all 2 demo with same database.
Comment: Sharing a database, could be difficult since the urls are set in it.
But something like multisite should be able to do it. - read here : http://www.wpbeginner.com/glossary/multisite/
Answer: I am not confirm but hope this will work for you.
Install 2 website with single db ( 2 copies ).
Create new table in you'rs db. Call it wp_options2 and copy everithing from wp_options inside new table
in seccond install in wp-config.php, before if ( !defined('ABSPATH') ) add define( 'M7_OPTIONS_TABLE', 'wp_options2' );
in seccond install go to wp-includes/wp-db.php on line 951 and add code:
if ( isset( $tables['options'] ) && defined( 'M7_OPTIONS_TABLE' ) )
                $tables['options'] = M7_OPTIONS_TABLE;

( these code should be added in public function tables function, before if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) ) )
Now you should be able to change theme, plugins atc. for seccond theme, but all posts, taxonomies atc. will be dubbled in both websites ( wich is bad for seo bdw. )
if on seccond install you'll have some problems with links, add
define('WP_HOME','http://seccond_website.com');
define('WP_SITEURL','http://seccond_website.com');

in your wp-config.php file .....
Comment: Thanks a lot @jasbir


Upload and get images in custom module with custom block form

Question: I need some help
I need create slider on the front page with custom block.
I create custom module, create custom block and form.
How i can get images after upload in form for use in twig-template??
For example i create some block with input[type]=textfield, and use this values in twig-template. How can i use images??
its my code for 'textfield'
<code><?php

namespace Drupal\dot\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a 'SocialLinksBlock' block.
 *
 * @Block(
 *   id = "social_links_block",
 *   admin_label = @Translation("Social Links Block"),
 * )
 */

class SocialLinksBlock extends BlockBase implements BlockPluginInterface {
    /**
     * {@inheritdoc}
     */
    public function blockForm($form, FormStateInterface $form_state) {
        $form = parent::blockForm($form, $form_state);
        // Retrieve existing configuration for this block.
        $config = $this->getConfiguration();
        // Add a form field to the existing block configuration form.
        $form['youtube'] = array(
            '#type' => 'textfield',
            '#title' => t('Youtube link:'),
        );
        $form['google'] = array(
            '#type' => 'textfield',
            '#title' => t('Google link:'),
        );
        $form['twitter'] = array(
            '#type' => 'textfield',
            '#title'=> t('Twitter link:'),
        );
        $form['facebook'] = array(
            '#type' => 'textfield',
            '#title'=> t('Facebook link:'),
        );
        return $form;
    }

    public function blockSubmit($form, FormStateInterface $form_state) {
        // Save our custom settings when the form is submitted.
        $this->setConfigurationValue('youtube', $form_state->getValue('youtube'));
        $this->setConfigurationValue('google', $form_state->getValue('google'));
        $this->setConfigurationValue('twitter', $form_state->getValue('twitter'));
        $this->setConfigurationValue('facebook', $form_state->getValue('facebook'));
    }
    /**
     * {@inheritdoc}
     */
    public function build() {
        $config = $this->getConfiguration();
        $youtube = isset($config['youtube']) ? $config['youtube'] : '';
        $google = isset($config['google']) ? $config['google'] : '';
        $twitter = isset($config['twitter']) ? $config['twitter'] : '';
        $facebook = isset($config['facebook']) ? $config['facebook'] : '';

        return array(
            '#theme' => 'block--sociallinksblock',
            '#youtube' => $youtube,
            '#google' => $google,
            '#twitter' => $twitter,
            '#facebook' => $facebook,
        );
    }
}
</code>
Answer: In Drupal there is not much help available to store images in configuration. You would need to do most things yourself, like build the form fields, submit and store the images and take care how they should be rendered.
But Drupal has a great infrastructure to store images in content. So the better approach is probably not to use a block plugin, but to create a custom block type, which is content like a node. Here you can add the image fields  and configure how they should be rendered in ui.


Add/Delete item on ListView containing EditText with ArrayAdapter

Question: I have a problem using CustomAdaptor.
On a add/edit recipe I want to display a list with the ingredient and the amount and possibility to delete this item with a button.
As you can see on the pictures, at first the list is only one item (without delete button because visibility = INVISIBLE in order to oblige to give at least one ingredient) and I have a button below the list to add other blank item but the other need to keep editing text.
Firstly, when I clicked on this button I lose all edited text.
Secondly, I don't see who to manage to delete an item (I tried this solution).
I hope someone has already solved this thing and could help me :)

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_above="@id/btn_save">

        <LinearLayout
            android:id="@+id/block_ingredient"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/listview_ingredient"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <Button
                android:id="@+id/btn_add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="+" />

        </LinearLayout>

    </ScrollView>

    <Button
        android:id="@+id/btn_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="SAVE" />

</RelativeLayout>

list_ingredient.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/quantite"
        android:layout_width="0dp"
        android:layout_weight="45"
        android:layout_height="wrap_content"
        android:hint="Quantite" />

    <EditText
        android:id="@+id/ingredient"
        android:layout_width="0dp"
        android:layout_weight="45"
        android:layout_height="wrap_content"
        android:hint="Ingredient" />

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="0dp"
        android:layout_weight="10"
        android:layout_height="wrap_content"
        android:text="X" />
</LinearLayout>

Item.java
public class Item {
    private String Quantite;
    private String Ingredient;

    // --- CONSTRUCTORS ---
    public Item() {};
    public Item(String Quantite, String Ingredient) {
        this.Quantite = Quantite;
        this.Ingredient = Ingredient;
    }

    // --- GETTER ---
    public String getQuantite() { return Quantite; }
    public String getIngredient() { return Ingredient; }

    // --- SETTER ---
    public void setQuantite(String Quantite) { this.Quantite = Quantite; }
    public void setIngredient(String Ingredient) { this.Ingredient = Ingredient; }
}

IngredientAdapter.java
public class IngredientAdapter extends ArrayAdapter {

    private Context context;
    private ArrayList<Item> listItem;
    private static LayoutInflater inflater = null;
    private View v;
    private ViewHolder viewHolder;

    public IngredientAdapter(Context context, ArrayList<Item> listItem) {
        super(context, 0, listItem);
        this.context = context;
        this.listItem = listItem;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override public int getCount() { return listItem.size(); }

    @Override public Object getItem(int i) { return null; }

    public ArrayList<Item> getItems() {
        return new ArrayList<Item>(listItem);
    }

    @Override public View getView(int i, View convertView, ViewGroup parent) {
        View finalView = convertView;
        final int position = i;
        Item item = listItem.get(position);
        if (convertView == null) {
            finalView = inflater.inflate(R.layout.list_ingredient, null);
            viewHolder = new ViewHolder();
            viewHolder.Quantite = finalView.findViewById(R.id.quantite);
            viewHolder.Ingredient = finalView.findViewById(R.id.ingredient);
            viewHolder.Btn_delete = finalView.findViewById(R.id.btn_delete);
            finalView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.Quantite.setText(item.getQuantite());
        viewHolder.Ingredient.setText(item.getIngredient());
        if (listItem.size() < 2) {
            viewHolder.Btn_delete.setVisibility(View.INVISIBLE);
        }

        viewHolder.Btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Integer position = (Integer) view.getTag();
                listItem.remove(position);
                notifyDataSetChanged();
            }
        });

        return finalView;
    }

    public ArrayList<Item> getAll() { return listItem; }

    public class ViewHolder {
        EditText Quantite;
        EditText Ingredient;
        Button Btn_delete;
    }

    public void setItems(ArrayList<Item> listItem) {
        this.listItem = listItem;
    }
}

MainActivity.java
public class MainActivity extends AppCompatActivity {

    private ListView listview_ingredient;
    private Button btn_add;
    private Button btn_save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview_ingredient = findViewById(R.id.listview_ingredient);
        btn_add = findViewById(R.id.btn_add);
        btn_save = findViewById(R.id.btn_save);

        final ArrayList<Item> IngredientList = new ArrayList<Item>();
        if (IngredientList.size() == 0) {
            IngredientList.add(new Item());
        }

        final IngredientAdapter ingredientAdapter = new IngredientAdapter(this, IngredientList);
        listview_ingredient.setAdapter(ingredientAdapter);

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Ajouter un item vide
                IngredientList.add(new Item());
                final IngredientAdapter ingredientAdapter = new IngredientAdapter(MainActivity.this, IngredientList);
                listview_ingredient.setAdapter(ingredientAdapter);

            }
        });

        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Sauvegarder les réponses

            }
        });

    }
}

I know the list show only one item with scrollbar but I already have a code to correct, it's just more readeable without.
Comment: It has been a very long time since I last used `ListView`, but I believe that you need to call the method `notifyDataSetChanged()` on the adapter object instead of replacing it every time you hit the "Add" button. (BTW I moved on to `RecyclerView`-- In all sincerity I would suggest that you consider adapting you code to the `RecyclerView`, also).
Comment: Thank you Barns for your answer. notifyDataSetChanged() is not working I think because the edit text is in the adapter object and not in the list. I try to change the list on getView() of the adapter with setOnTextChanged but is also not working. I don't try RecyclerView so I will.
Comment: As you suggest I moved my code to RecyclerView and I can do all what I want like edit my list with the new text, delete item and hide button if the list contains only one element. Thank you again for this way I now can see the difference between CustomAdapter and Recycler View.
Answer: As Barns suggest, I moved to RecyclerView and used notifyDataSetChanged() when I add new Item.
If it's helped others I had my changes below and I add this link because it's good tutorial to RecyclerView with EditText : https://demonuts.com/android-recyclerview-with-edittext/
For the above code I change this things :
activity_main.xml

only changed ListView to RecyclerView
MainActivity.java

when I call the Adapter :
ingredientAdapter = new IngredientAdapter(this, IngredientList);
listview_ingredient.setAdapter(ingredientAdapter);
listview_ingredient.setLayoutManager(new LinearLayoutManager(getApplicationContext()) {
    @Override public boolean canScrollVertically() {
        return false;
    }
});

On click add button :
IngredientList.add(new Item());
ingredientAdapter.notifyDataSetChanged();

and the Adapter become
public class IngredientAdapter extends RecyclerView.Adapter<IngredientAdapter.ViewHolder> {

    private Context context;
    public static ArrayList<Item> IngredientList;
    private static LayoutInflater inflater = null;
    private ViewHolder viewHolder;

    // --- CONSTRUCTOR ---
    public IngredientAdapter(Context context, ArrayList<Item> listItem) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.IngredientList = listItem;
    }

    // --- ADAPTER ---
    @Override public IngredientAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.list_ingredient, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    // --- SET TEXT TO THE ELEMENT OF THE LIST
    @Override public void onBindViewHolder(final IngredientAdapter.ViewHolder holder, final int position) {
        holder.Quantite.setText(IngredientList.get(position).getQuantite());
        holder.Ingredient.setText(IngredientList.get(position).getIngredient());
        if (getItemCount() == 1) {
            holder.Delete.setVisibility(View.INVISIBLE);
        } else {
            holder.Delete.setVisibility(View.VISIBLE);
        }
    }

    @Override public int getItemCount() {
        return IngredientList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        protected EditText Quantite;
        protected EditText Ingredient;
        protected Button Delete;

        public ViewHolder(View itemView) {
            super(itemView);
            Quantite = (EditText) itemView.findViewById(R.id.quantite);
            Ingredient = (EditText) itemView.findViewById(R.id.ingredient);
            Delete = (Button) itemView.findViewById(R.id.btn_delete);

            Quantite.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    IngredientList.get(getAdapterPosition()).setQuantite(Quantite.getText().toString());
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

            Ingredient.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    IngredientList.get(getAdapterPosition()).setIngredient(Ingredient.getText().toString());
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

            Delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    IngredientList.remove(getAdapterPosition());
//                    notifyItemRemoved(getAdapterPosition());
                    notifyDataSetChanged();
                }
            });

        }
    }
}

On delete Item you can change notifyDataSetChanged() to notifyItemRemoved() but since I have condition on list size to hide or display delete button I have to "restart" the adapter.


Why is expected group difference necessary in power analysis?

Question: Why do we need the expected group statistic (i.e. mean) difference when calculating the sample size?
The point of power analysis is to find the right sample size so that I can detect a difference. If I just picked a random but reasonable sample size and calculated the difference in means and let that be the expected difference in means to do power-analysis, wouldn't that number be wrong?
Is the expected difference that one uses to calculate the sample size needed to achieve a particular power the true population level difference (n=N) or the sample difference in means?
(n
Comment: Are you suggesting that the determination of sample size requires no knowledge of the applied subject matter and/or the objectives and that just someone with the right software is all that's necessary?
Comment: No, I was just thinking maybe it was part of a optimization scheme that if one started with a small but easy sample size and then obtained the difference in means and then iteratively used the power analysis formulas, they would converge at the right sample size. I thought math people should be able to derive the truth independently of other fields.
Comment: It requires subject matter knowledge which is NOT an intrinsic feature of a set of numbers.  In fact a single set of data could be used by different folks with different objectives or the same folks with different objectives at different times.
Answer:
Is the expected difference that one uses to calculate the sample size
  needed to achieve a particular power the true population level
  difference (n=N) or the sample difference in means? (n)

It's the population difference.
Think of a statistical hypothesis test as an attempt to find something - that something being evidence against the null hypothesis.
The sample size is how hard you are looking. The bigger the sample size, the harder you look (or longer you spend looking).
The effect size (the expected group statistic) is the size of the thing you are looking for. If I hid a football inside your house, it's going to be easier to find than a tennis ball, which will be easier than a golf ball, which will be easier than a marble.
Picking an appropriate effect size is at least partially arbitrary, but you should at least try to determine an effect size that would be interesting or useful.  Picking it by choosing an arbitrary value means that you might as well just select an arbitrary value for the sample size.


Multiple node access rules for same node?

Question: I'm using the Domain Access module to restrict content to specific domains. It uses the node_access table to do this.
I also want to make a single node hidden behind a login area. The problem is that when I add my node access rules to do this it overwrites all the Domain Access rules which mean that all the nodes appear on the same domain.
Is there a way to allow more than one module to use node access rules?
My node access code looks like this:
define('MYSITE_REALM', 'press');
define('MYSITE_GRANT_ID_PUBLIC', 0);
define('MYSITE_GRANT_ID_PRESS', 1);
define('MYSITE_PRESS_SECTION_NID', 554);
define('MYSITE_PRESS_USER', 'press');

/**
 * Implements hook_node_grants().
 */
function mysite_node_grants($account, $op) {
  if ($op == 'view') {
    $grants = array();

    if ($account->name == MYSITE_PRESS_USER) {
      $grants[MYSITE_REALM] = array(
        MYSITE_GRANT_ID_PUBLIC,
        MYSITE_GRANT_ID_PRESS,
      );
    }
        else {

      // everyone else
      $grants[MYSITE_REALM] = array(
        MYSITE_GRANT_ID_PUBLIC,
      );
    }

    return $grants;
  }
}

/**
 * Implements hook_access_records().
 */
function mysite_node_access_records($node) {
  $grants[] = array();

  if ($node->nid == MYSITE_PRESS_SECTION_NID) {
    $grants[] = array(
      'realm' => MYSITE_REALM,
      'gid' => MYSITE_GRANT_ID_PRESS,
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
      'priority' => 0,
    );
  }
    else {
    // everything else is public
    $grants[] = array(
        'realm' => MYSITE_REALM,
      'gid' => MYSITE_GRANT_ID_PUBLIC,
        'grant_view' => 1,
        'grant_update' => 0,
        'grant_delete' => 0,
        'priority' => 1,
    );
  }

  return $grants;
}

function mysite_user_login(&$edit, $account) {
  if ($account->name == MYSITE_PRESS_USER) {
    drupal_goto('node/' . MYSITE_PRESS_SECTION_NID);
  }
    else {
    drupal_goto('');
  }
}

Answer:
Is there a way to allow more than one module to use node access rules?

The problem with hook_node_access_records() and hook_node_grants() is that, independently from the weight of the modules, once a module returns 1 for "grant_view," "grant_update," or "grant_delete" that permission is allowed to the user. If a module returns 1 for one of those operations, and another module returns 0, the access is allowed, independently from the priority given to the records returned from the modules for those records.
In Drupal 7 it is also possible to use hook_node_access(), which is able to report if the user has access or not to the node, or if the hook implementation let other implementations  decide if the user has access to the node (using the value NODE_ACCESS_IGNORE).
In Node access rights it is explained how the node access is checked.

In determining access rights for a node, node_access() first checks whether the user has the "bypass node access" permission. Such users have unrestricted access to all nodes. User 1 will always pass this check.
Next, all implementations of hook_node_access() will be called. Each implementation may explicitly allow, explicitly deny, or ignore the access request. If at least one module says to deny the request, it will be rejected. If no modules deny the request and at least one says to allow it, the request will be permitted.
If all modules ignore the access request, then the node_access table is used to determine access. All node access modules are queried using hook_node_grants() to assemble a list of "grant IDs" for the user. This list is compared against the table. If any row contains the node ID in question (or 0, which stands for "all nodes"), one of the grant IDs returned, and a value of TRUE for the operation in question, then access is granted. Note that this table is a list of grants; any matching row is sufficient to grant access to the node.
Comment: Fantastic exactly what I was looking for.


mysqlbinlog: unknown option '--ssl'

Question: I am running FreeBSD 7. running MySQL 5.1.36 from ports.
I am trying to check some data in the MySQL binary log files, and I get the following error:
# mysqlbinlog mysql-bin.000021
mysqlbinlog: unknown option '--ssl'

In addition, I can't even view the help for this command:
# mysqlbinlog --help
mysqlbinlog: unknown option '--ssl'

Why does mysqlbinlog not know about the option ssl?
This server was compiled with SSL, and ssl is activated in the my.cnf configuration file. We use SSL for remote connections, but I am running this command on the local host.
Answer: I recently ran into the same problem with MySQL 5.1.41 on Windows.  The mysqlbinlog program doesn't support the --ssl option, but I couldn't figure out where it was reading that option.  Even when I commented out the SSL options under [mysqld] in my.ini, mysqlbinlog was still reading that option somewhere.
Eventually I found that my.ini had a [client] section that was specifying the --ssl option:
[client]
ssl
ssl-ca=C:/certificates/MySQLCerts/ca-cert.pem
port=3306

I commented out those ssl options and then mysqlbinlog worked:
[client]
#ssl
#ssl-ca=C:/certificates/MySQLCerts/ca-cert.pem
port=3306

I did not have to restart MySQL for the changes to take effect.
Comment: Thanks for finding the root cause. Too bad that the `[client]` options don't work with `mysqlbinlog`. I need those options for the regular `mysql` commandline tool.
Answer: As a workaround to this problem, I figured out that I can skip the defaults:
mysqlbinlog --no-defaults

mysqlbinlog: unknown option '--ssl'


Can string formatting be used only in print() function and statements in Python?

Question: iterator, i is "Roster ID 2006 has been deleted"

  if re.match("Roster ID 2006 has been deleted",  i): //works

  if re.match("Roster ID %s has been deleted" % i,  i): //does not seem to work

Can I not use string formatting anywhere else except print functions and print statements?
Comment: I suspect that if you tried `print "Roster ID %s has been deleted" % i` at that point in your code, it wouldn't do what you expect either. Print it and see what you get.
Comment: so can i or can i not do it?
Comment: You can. You're just doing it wrong.
Comment: Did you actually _try_ `print` in this situation? Then you'd seen what is actually wrong...
Answer: Yes, you can use string formating outside print. Your example does not work beacuse i is the string you are looking for. It should be a year, e.g. 2006
i = "Roster ID 2006 has been deleted"
print(re.match("Roster ID 2006 has been deleted",  i))
print(re.match("Roster ID %d has been deleted" % 2006,  i))

In both cases we get the following :
<_sre.SRE_Match object; span=(0, 31), match='Roster ID 2006 has been deleted'>
<_sre.SRE_Match object; span=(0, 31), match='Roster ID 2006 has been deleted'>

But you are doing this:
re.match("Roster ID %s has been deleted" % i,  i)

which is same as:
re.match("Roster ID Roster ID 2006 has been deleted has been deleted",  i)
# thus you are getting None as a result of the match.

Answer: You can; but doing "Roster ID %s has been deleted" % i will only produce the same thing as in the other line - i.e. "Roster ID 2006 has been deleted" - if i is the string "2006" or something that converts to that string. Since your "working" statement is if re.match("Roster ID 2006 has been deleted",  i):, it appears that i is actually some long string that you expect to contain "Roster ID 2006 has been deleted". Be sure you have the arguments in the right order:
>>> help(re.match)
Help on function match in module re:

match(pattern, string, flags=0)
    Try to apply the pattern at the start of the string, returning
    a match object, or None if no match was found.