Difficult to comprehend markov chain and its characteristics

Question: If $Y_n$ is a sequence of independent random variables with $P(Y_n=0)=2/3,P(Y_n=1)=1/6,P(Y_n=2)=1/6$ and $X_n$ with $X_0=0$ is defined as
$$X_{n+1}= \left\{
  \begin{array}{lr}
    X_n-Y_n & : &X_n=3\\
    X_n-1+Y_n &: &1\leq X_n\leq2\\
    Y_n & : &X_n=0
  \end{array}
\right.$$
I don't understand this markov chain. What is its state space and what is the transition matrix? Also: how do I find its invariant distribution when I have the transition matrix?
Answer: The state space is $\{0,1,2,3\}$. When at state $0$, the next state has the distribution $\mathcal{Y}$ which is the distribution of $Y_n$. When at state $1$, the next state also has the distribution $\mathcal{Y}$, while at state $2$, it has the distribution $\mathcal{Y}+1$. Finally, at state $3$, it has distribution $3-\mathcal{Y}$. All in all, we obtain the following transition matrix (depending on your convention, this might be the transpose of the transition matrix):
$$
\begin{pmatrix}
2/3 & 2/3 & ? & ? \\
1/6 & 1/6 & ? & ? \\
1/6 & 1/6 & ? & ? \\
0 & 0 & ? & ?
\end{pmatrix}
$$
I'll let you figure out the other half of the matrix. Given the transition matrix, the invariant distribution is an eigenvector corresponding to the eigenvalue $1$ (and is additionally a distribution). Sometimes the invariant distribution is not unique, though in this case it is.
Comment: @Did I meant that sometimes there is more than one, if the chain is not ergodic.
Answer: First work out the state space. It's built into the setup that the state space contains $\{ 0,1,2,3 \}$, and in fact it doesn't contain anything else, as you can check by checking that if $X_n \in \{ 0,1,2,3 \}$ then no matter what $Y_n$ is, you will have $X_{n+1} \in \{ 0,1,2,3 \}$.
From there just build the transition matrix case by case. (Here I use the convention that $P_{ij}$ is the probability to go from $i$ to $j$; sometimes the reverse convention is used.) The first row will be the transition probabilities for when $X_n$ is currently $0$. These are just the probabilities for $Y_n$, i.e. $2/3,1/6,1/6,0$ (since $Y_n$ is never $3$). The second row is the transition probabilities for when $X_n$ is currently $1$. And so on.
To find the invariant distribution, you have to solve the linear system $\pi = \pi P$ where $P$ is the transition probability matrix and $\pi$ is the invariant distribution (written as a row vector), and then normalize it into a probability distribution. This can be done by row reduction; numerically it is usually better to use an eigenvalue routine (since singular matrices are "bad" numerically speaking).


Can Newtonsoft (json.net) Deserialize be made lazy in F#?

Question: Consider the following code, which uses FSharp.Data to request data from a web resource
let resp = Http.RequestStream(url, headers, query)
use rdr = new StreamReader(resp.ResponseStream)
use jrdr = new JsonTextReader(rdr)
let serializer = new JsonSerializer()
let myArray = serializer.Deserialize<someType[]>(jrdr).Value

myArray is an array of someType. Arrays are eagerly evaluated so if I request a large amount of data, I will consume a large amount of RAM up front.
What if I ask json.net to give me a seq instead?
let resp = Http.RequestStream(url, headers, query)
use rdr = new StreamReader(resp.ResponseStream)
use jrdr = new JsonTextReader(rdr)
let serializer = new JsonSerializer()
let mySeq = serializer.Deserialize<someType seq>(jrdr).Value

If I iterate through mySeq and write it to a text file, is everything pulled form the stream and deserialized lazily? Or does the act of asking json.net to deserialize force everything to be eagerly evaluated at that point?
UPDATE
Following on from the accepted answer of dbc, a functional lazy function would be something like the following
let jsonSeqFromStream<'T>(stream:Stream) = seq{
    let serializer = JsonSerializer.CreateDefault()
    use rdr = new StreamReader(stream, Encoding.UTF8, true, 4096, true)
    use jrdr = new JsonTextReader(rdr, CloseInput = false)
    let rec resSeq inArray = seq{
        if jrdr.Read() then
            match jrdr.TokenType with
            |JsonToken.Comment -> yield! resSeq inArray
            |JsonToken.StartArray when not inArray -> yield! resSeq true
            |JsonToken.EndArray when inArray -> yield! resSeq false
            |_ ->
                let resObj = serializer.Deserialize<'T>(jrdr)
                yield resObj
                yield! resSeq inArray
        else
            ()
    }
    yield! resSeq false
}

Answer: Json.NET deserialization of a sequence can be made lazy, but it is not so automatically.  Instead, you will have to adapt one of the answers from Parsing large json file in .NET or Newtonsoft JSon Deserialize into Primitive type to f#.
To confirm that deserialization of a sequence is not lazy by default, define the following function:
let jsonFromStream<'T>(stream : Stream) =
    Console.WriteLine(typeof<'T>) // Print incoming type for debugging purpose
    let serializer = JsonSerializer.CreateDefault()
    use rdr = new StreamReader(stream, Encoding.UTF8, true, 4096, true)
    use jrdr = new JsonTextReader(rdr, CloseInput = false)
    let res = serializer.Deserialize<'T>(jrdr)
    Console.WriteLine(res.GetType()) // Print outgoing type for debugging purpose
    res

Then if we have some stream stream containing a JSON array of objects someType, and call the method like so:
let mySeq = jsonFromStream<someType seq>(stream)

Then the following debug output is generated:

System.Collections.Generic.IEnumerable`1[Oti4jegh9906+someType]
System.Collections.Generic.List`1[Oti4jegh9906+someType]

As you can see, from the .Net point of view, calling JsonSerializer.Deserialize<T>() with someType seq is just the same as calling it with IEnumerable<someType> from c#, and in such a case Json.NET materializes the result and returns it as a List<someType>.
Demo fiddle #1 here.
To parse the JSON array as a lazy sequence, you will need to manually create a seq function that iterates through the JSON with JsonReader.Read() and deserializes and yields each array entry:
let jsonSeqFromStream<'T>(stream : Stream) =
    seq {
        // Adapted from this answer https://stackoverflow.com/a/35298655
        // To https://stackoverflow.com/questions/35295220/newtonsoft-json-deserialize-into-primitive-type
        let serializer = JsonSerializer.CreateDefault()
        use rdr = new StreamReader(stream, Encoding.UTF8, true, 4096, true)
        use jrdr = new JsonTextReader(rdr, CloseInput = false)
        let inArray = ref false
        while jrdr.Read() do
            if (jrdr.TokenType = JsonToken.Comment) then
                ()
            elif (jrdr.TokenType = JsonToken.StartArray && not !inArray) then
                inArray := true
            elif (jrdr.TokenType = JsonToken.EndArray && !inArray) then
                inArray := false
            else
                let res = serializer.Deserialize<'T>(jrdr)
                yield res
    }

(Since tracking whether we are parsing array value(s) is stateful, this doesn't look very functional.  Maybe it could be done better?)
The return of this function could be used as follows, e.g.:
let mySeq = jsonSeqFromStream<someType>(stream)

mySeq |> Seq.iter (fun (s) -> printfn "%s" (JsonConvert.SerializeObject(s)))

Demo fiddle #2 here.
Comment: Thanks. I updated my question with a non mutating version of your solution. Look Ok?
Comment: Never mind, it doesn't work. It only returns the first element in the sequence. I'll leave it in the question anyway.
Comment: Never mind the never mind, I fixed the bug. Functional version works.


Array item assignment gives unexpected results

Question: Background: I have an exercise which asks me to create a function which compares 2 int arrays using the backtracking technique. The function should return 0 if the arrays are different and 1 if they are the same. The size of the arrays, the method of filling them and the output of the program are not specified in the question so I took the liberty of working them out my own way.
By using a for I made a simple fill function which fills out the two arrays in a simple way so if the user inputs s the result should be
A[0]=B[0]=0
A[1]=B[1]=1
...
A[50]=B[50]=50

and if he inputs d it should be the same but
B[i]=A[i]+1

The problem:
Instead of A[0]=0 it ends up being A[0]=50 (and A[0]=51 in the d case) which makes the whole function return 0 in every case. I have tried numerous things, but I can't get it to work properly
Here's the code:
#include <stdio.h>
#include <stdlib.h>

void fill(int a, int A[],int B[])
{
     int i,j;

    if (a)
    {
        for(i=0;i<=50;i++)
        {
        A[i]=i;
        B[i]=A[i];
        }
    }
    else
    {
        for(i=0;i<=50;i++)
        {
            A[i]=i;
            B[i]=i+1;
        }
    }
    A[0]=0;
    for (j=0;j<=50;j++)
        printf("\nka %d %d %d",j, A[j],B[j]); //the purpose of this is to check the state of the two arrays after filling them, it's how I spotted the problem, it will be deleted in the final form

}

int compare(int i, int A[],int B[])
{
    int a,b;

    a=A[i];
    b=B[i];
    printf("j %d %d\n", a,b);
    if (B[i+1]!='\0')
    {
        if (A[i]==B[i])
        {
            compare (i+1,A,B);
        }
        else
        {
            return 0;
        }
    }
    else
        return 1;

}

int main()
{
    int A[50], B[50], i=0;
    char s;

    printf("Do you want the arrays to be the same or different?\n");
    printf("Input 's' or 'd': ");

    scanf("%c", &s);

    switch(s)
    {
        case 's':
        case 'S':
            fill(1,A,B);
            break;

        case 'd':
        case 'D':
            fill(0,A,B);

            break;

        default:
            printf("Sorry incorrect input, please input 's' or 'd'");
            return 0;
            break;
    }

    if (compare(i,A,B))
       printf("They are the same");
    else
       printf("They are different");

return 0;
}

Comment: You have *undefined behavior* because you index the array out of bounds. Look at those loops again, especially the condition, and think about what the maximum index can be for an array of 50 elements.
Comment: Please create a [Complete, Minimal, Verifiable Example](http://stackoverflow.com/help/mcve)
Comment: Your arrays are declared to have 50 elements. But you access A[50], which is the fifty first element; you can only access A[i] for 0<=i<50, and similarly for B.
Comment: @JonathanMee; Actually it is.
Comment: @haccks You and I have very different interpretations of "minimal".
Comment: For starters, you should use for(i=0;i<50;i++) . Not i<=50.
Comment: @TartanLlama; True. But, SO has a complete guideline for "minimal" as given in the link in first comment.
Answer: You need to correct your both the functions fill and compare. In function fill you are accessing arrays out of bounds which will invoke undefined behavior. Loop should be iterated from 0 to 50 (excluding 50)
for(i=0;i<50;i++) { ... }

The function compare should be like
// Pass the size of array to the function.
int compare(int i, int A[],int B[], int n)
{
    if (i != n)
    {
        if (A[i]==B[i])
        {
            return compare (i+1, A, B, n);
        }
        else
        {
            return 0;
        }
    }
    else
        return 1;
}

Answer: The issue is in the for loop. It is looping for 51 elements.
for (i=0;i<=50;i++)

It should loop for 50 elements
for (i=0;i<50;i++)

Similarly for the loop for j, in the fill function.
There is also an issue in the compare function. The exit check is
if (B[i+1]!='\0')

This means that the input array must have the last element as '\0'. Which is not happening in your input array.
You have to either pass strings to this function, or modify this function to take care of the general array case.


Prove the inequality $\sum_{n=1}^\infty \left(\int_E \cos 2nxdx\right)^2\le \pi |E|$.

Question: I am trying to prove this inequality but I am not sure where does the coefficient $\pi$ come from.
Desired Inequality: $$\sum_{n=1}^\infty \left(\int_E \cos 2nxdx\right)^2\le \pi |E|,$$ where $E$ is a subset of $[0,2\pi]$ and its Lebesgue measure $|E|\ge\pi$.
I know when $n\to\infty$, the integrand $\cos2nx$ will have sufficiently small period and it will oscillate very rapidly in $E$, which forces the integral $\left(\int_E \cos 2nxdx\right)^2\to 0$. So I am good with the problem of convergence of the series. But I am not sure how to proceed calculating the actual upper bound of the series. In particular, no clues on where does the coefficient $\pi$ come from.
Thanks for any help.
Answer: Hint: Let $f(x)=\chi_E(x)$ for $x\in[0,2\pi]$, i.e., $f(x)=1$ for $x\in E$ and $f(x)=0$ for $x\in[0,2\pi]\setminus E$, then by Parseval's identity we have
$$\int_0^{2\pi}|f(x)|^2\,dx=2\pi\sum_{n\in\mathbb Z}|c_n|^2,\tag{1}$$
where $c_n=\frac1{2\pi}\int_0^{2\pi}f(x)e^{-inx}\,dx$ for $n\in\mathbb Z$. Here we can see the appearence of $\pi$.
The details are as follows:

Let $a_n=\int_0^{2\pi}f(x)\cos(nx)\,dx=\int_E \cos(nx)\,dx$ and $b_n=\int_0^{2\pi}f(x)\sin(nx)\,dx=\int_E \sin(nx)\,dx$, then $c_n=\frac1{2\pi}(a_n-ib_n)$ for all $n\in\mathbb Z$. Hence by $(1)$, $$\sum_{n\in\mathbb Z}(a_n^2+b_n^2)=2\pi |E|.$$ Notice that $a_{n}=a_{-n}$ for all $n\in\mathbb Z$, we have $$\sum_{n=1}^\infty a_n^2\leq \frac12\sum_{n\in\mathbb Z}a_n^2\leq \pi|E|.$$ Therefore, $$\sum_{n=1}^\infty \left(\int_E \cos (2nx)dx\right)^2=\sum_{n=1}^\infty a_{2n}^2\le \pi |E|.$$
Comment: Thanks for the hint and details! By the way, your avatar is cute :)
Comment: @mathslearner You're welcome. I'm very glad to help. That's my girlfriend. Thanks for your compliment :)


Como usar o input radio com input hidden

Question: Estou querendo gravar dois campos de uma tabela simultaneamente, seria o campo "gale_fotos" e o "gale_status" e estou usando o código abaixo, porém ele somente grava o campo "gale_status" como Não.
Como faço para que ele grave no Campo "gale_status" o SIM, quando clicado na opção SIM.

    <?php
    include '../conexao.php';
    $codigo = $_POST['codigo'];
    $gale_fotos = $_POST['gale_fotos'];
    $gale_status = $_POST['gale_status'];

    $query = mysql_query("SELECT * FROM menu");
    $res = mysql_fetch_array($query);

    if(isset($_POST['gale'])){

    $gale_fotos = $_POST['gale_fotos'];
    $gale_status = $_POST['gale_status'];
    $update = mysql_query("UPDATE menu SET gale_fotos = '$gale_fotos', gale_status = '$gale_status'");

    if($update == ''){
    echo "";
    }else{
    echo "<meta http-equiv='refresh' content='0; URL= menu_halitar_link.php'>
    ";
    }}?>

    <form name="gale" action="" method="POST" enctype="multipart/form-data">
    Habilitar o Link Galeria de Fotos?

    Sim
    <input type='radio' name='gale_fotos' value='
<a href="<?php echo $res['dominio'];?>galeria.php" class="nav1">Galeria de Fotos

' />
    <input type='hidden' name='gale_status' value='Sim' />

    Não
    <input type="radio" name="gale_fotos" value="" />
    <input type="hidden" name="gale_status" value="Não" />

    <input type="submit" name="gale" value="Atualizar" />
    </form>

Desde já agradeço a atenção de todos.
Answer: Com o evento onclick do javascript, atribua um value (document.getElementById('campoOculto').value='valor') para o input hidden (identificado por um ID específico) de acordo com o radio selecionado.
Sim
<input type='radio' name='gale_fotos' value='...'
onClick="document.getElementById('campoOculto').value='Sim'">
Não
<input type="radio" name="gale_fotos" value=''
onClick="document.getElementById('campoOculto').value='Não'"/>

<input id="campoOculto" type="hidden" name="gale_status" value="" />

Answer: Eu fiz uma implementação hoje para checkbox, mas que também funciona para o que você precisa com radio, dá uma olhada:
HTML:
<input id="chkCamp1" name="rad" type="radio" data-check="1" value="elemento 1"> elemento 1
<input id="chkCamp2" name="rad" type="radio" data-check="2" value="elemento 2"> elemento 2
<input type="text" data-view>
<input type="hidden" data-hide>

E o JavaScript:
$(function() {
    $('[data-check]').on('click', function() {
        var myInput = {valor: $(this).val(), id: $(this).data('check')};
        $('[data-view]').val(myInput.valor);
        $('[data-hide]').val(myInput.valor);
    });
});

Ele funcionando:
http://jsfiddle.net/ivanferrer/9wzsu3wt/5/
Comment: Ivan, primeiro muito obrigado pela sua atenção ao meu problema. Mas eu tentei com a sua dica e não deu certo, ele continua pegando o Status Não quando seleciono o Sim. Eu consegui fazer com que ele pegue o status correto, mas só fazendo dois form, um para cada Input Radio e hidden, mas fica com dois botoes de atualizar.
Vou continuar tentando, conseguindo publico a solução. Mas se alguém  puder me dar uma ajuda ficarei agradecido.
Comment: `if(isset($_POST['gale'])){
$gale_fotos = $_POST['gale_fotos'];
$update = mysql_query("UPDATE menu SET gale_fotos = $gale_fotos'")or die(mysql_error());

if($gale_fotos != 'galeria.php" class="nav1">Galeria de Fotos'){
 $update = mysql_query("UPDATE menu SET gale_status = 'Sim'");
 }else{
 $update = mysql_query("UPDATE menu SET gale_status = 'Não'") ;
 }`

Eu fiz essa alteração, mas pega somente uma das opções. Ele não deveria fazer a comparação e gravar de acordo com essa comparação? Pô galera, por favor...HEELPP.....
Comment: Isso ocorre porque o value retornado do campo gale_status no PHP é sempre o do segundo input hidden do HTML. Experimente trocar os values dos inputs hidden e verás que sempre retornará SIM.


in product title on new product type drupal commerce kickstart

Question: I created a custom product variation called Switch Plate and on the product displays it is showing Product Name on the title I installed from the 2.0 version of Commerce Kickstart it only shows on my product not on the 2 generated products.
Answer: Go to admin/structure/types, and click "Manage display" for your content type, which is a product display for your new product variation.
Choose the "Product list" display, and make sure the title's format is set as Default.
Comment: Thank you that got it but I still had to look them but its gone now
Comment: I did this, however I am no longer able to link to product from product list. Is this normal?
Answer: There is a recent issue on this on the Commerce Kickstart's issue page; there is no solution yet, though.


Defining properties of a class

Question: can somebody tell me the difference between
public class Vendor
{
    public string VendorName { get; set; }
}

and
public class Vendor
{
    private string vendorName = string.Empty;

    public string VendorName
    {
        get { return vendorName; }
        set { vendorName = value; }
    }
}

Is there any benefit of using a private variable? Doing so is only wastage of time and lines? There are no manipulations done to the property within the class.
Thanks
Answer: If this is all you are doing, there is no benefit, IMO.
The significant difference between these blocks of code is that the first defaults to null and the second defaults to string.Empty.
Other than that, I would choose auto properties (option 1) every time.  It is something that was added in C# 3.0.
Answer: None.  I have found that the auto-propeties cleans up the code quite nicely; especially in classes with larger properties.
EDIT
One think to keep in mind is that if you use a value to specify nulls (i.e. when persisting to a DB; if a DateTime equals DateTime.MinValue, save a NULL to the database), you'll need to declare this in the constructor.
Sample example
instead of
public class Vendor
{
    private DateTime deletedDate = DateTime.MinValue;

    public DateTime DeletedDate
    {
        get { return deletedDate ; }
        set { deletedDate = value; }
    }
}

you'd need something like this:
public class Vendor
{
    public DateTime DeletedDate { get; set; }

    public Vendor()
    {
        DeletedDate = DateTime.MinValue;
    }
}

Answer: C# 3 introduced auto-implemented properties, which makes your first snippet possible. The compiler is going to create your backing field for you, which would make the compiled code equivalent to the compiled code of your second snippet. If you're working in a <= 2.0 code base, you'll be using the second snippet.
The benefit of using a private variable would be if you performed some sort of validation when the property is set, such as restricting integers to positive values or rejecting null strings. If you do not need such behavior or simply wish to handle those activities elsewhere and the property is just a fetch and set, then use the auto-implemented version.
Answer: Given your samples, the two are semantically the same (other than the starting value of null for the first and string.Empty for the second, and the fact that the backing variable is not visible to your class in the auto property version). So, no, there is no specific benefit to using a fully-declared property, other than the ability to set an initial value in the declaration (as you do in the example).
When possible, I always prefer auto properties because of the reduction in redundant code (which is why they're there, after all). It's easy enough to set a default value in the constructor, if one is needed.

Defining properties of a class


Given a MPEG DASH .mpd URL, is that possible to down all media segments through youtube_dl?

Question: I'm looking for a MPEG DASH downloader and youtube_dl just hit on me.
Given a .mpd URL, is that possible to use youtube_dl to download all media segments then?
Answer: To download all video and audio segments and mux them into a single file, call youtube-dl thus:
youtube-dl -f bestvideo+bestaudio http://URL/TO/manifest.mpd

The option -f <id1>[,<id2>]... is used to select which stream (or streams) of segments to save. The -f bestvideo+bestaudio in this example makes youtube-dl save only the best video and audio streams. See format selection syntax for details and more advanced format selectors. In order to discover the available streams, use youtube-dl -F http://URL/TO/manifest.mpd.
In order to mux (merge) video and audio streams into a single file, you must have FFmpeg or libav installed in your system. Otherwise, youtube-dl will produce a separate file for each stream (in my example, one for audio and one for video).
Without explicit format selection, the default is -f bestvideo+bestaudio/best. Youtube-dl will automatically select the best video and audio, and if there are no separate video-only or audio-only streams, the best stream that contains both video and audio is selected.
youtube-dl http://URL/TO/manifest.mpd

This default is used since version 2015.04.26. Before that (or if -o - is specified, making youtube-dl write output to stdout), the default was -f best, ignoring the video-only and audio-only streams. You may want to specify -f best explicitly when separate video and audio have worse quality than a stream that has both.
Comment: Is it possible to specify a local .mpd file?
Comment: In my case, only manually specifying the bestvideo+bestaudio switch resulted in actually downloading the video.
Comment: I added more details on the default format selector. Which version of youtube-dl did you use, @JeroenBaert?
Comment: @Palec : I don't remember, but it's fixed in the latest version now :)


BigInsights Federation for Netezza

Question: When trying to setup BigInsights Federation to Netezza following the guideline http://www.ibm.com/developerworks/library/ba-federation-biginsights/index.html
After setting up all the env vars, when trying to create a wrapper to Netezza, I get the following error:

CREATE WRAPPER NETZ LIBRARY 'libdb2rcodbc.so' OPTIONS(MODULE'/opt/ibm/home/bigsql/resources/lib64/libnzodbc.so');
  SQL Exception(s) Encountered:
  [State: 42724][Code: -10013]: The specified library "/opt/ibm/home/bigsql/resources/lib64/libnzodbc.so" could not be loaded.. SQLCODE=-10013, SQLSTATE=42724, DRIVER=3.67.33

Has anyone encountered this.  I have followed the recommendations also in the link http://www-01.ibm.com/support/docview.wss?uid=swg21154462
and am still getting the same issue.
Comment: So, is the file `/opt/ibm/home/bigsql/resources/lib64/libnzodbc.so` present, executable, and of the correct bitness for the platform and DB2 instance?
Answer: Usually you need to check the following to make sure the setup works (this also applies to any data source utilizes ODBC wrapper):
1. Setup environment variables
The rule of thumb is you want to put Netezza odbc driver at the very beginning of each environment variable:
DJX_ODBC_LIBRARY_PATH=/opt/ibm/home/bigsql/resources/lib64/:/opt/oemclient/odbc71/64/lib/:/opt/oemclient/odbc60/64/lib

DB2LIBPATH=/opt/ibm/home/bigsql/resources/lib64/:/opt/oemclient/odbc71/64/lib/:/home/iidev20/sqllib/lib

LIBPATH=/opt/ibm/home/bigsql/resources/lib64/:/opt/oemclient/odbc71/64/lib/:/home/iidev20/sqllib/lib:/opt/oemclient/odbc60/64/lib

ODBCINI=/home/iidev20/bin/odbc.ini

DB2LIBPATH=/opt/ibm/home/bigsql/resources/lib64/:/opt/oemclient/odbc71/64/lib/:/home/iidev20/sqllib/lib:/opt/csdlclient/oracle/lib:/opt/oemclient/odbc60/64/lib:/opt/csdlclient/informix/64/lib

LD_LIBRARY_PATH=/opt/ibm/home/bigsql/resources/lib64/:/opt/oemclient/odbc71/64/lib/:/home/iidev20/sqllib/lib:/opt/oemclient/odbc60/64/lib

2. Setup odbc.ini corresponding DSN
[ODBC]
IANAAppCodePage=4
InstallDir=/opt/ibm/home/bigsql/resources/lib64/
TraceDll=
TraceFile=odbctrace.out
UseCursorLib=0

3. Run db2set
db2set DB2_DJ_INI=/home/iidev20/sqllib/cfg/db2dj.ini
db2set DB2LIBPATH=/opt/ibm/home/bigsql/resources/lib64/:/opt/oemclient/odbc71/64/lib/:/opt/oemclient/odbc71/64/

4. Modify db2dj.ini
  DJX_ODBC_LIBRARY_PATH=/opt/ibm/home/bigsql/resources/lib64/:/opt/oemclient/odbc 71/64/lib/:/opt/oemclient/odbc60/64/lib

DB2LIBPATH=/opt/ibm/home/bigsql/resources/lib64/:/opt/oemclient/odbc71/64/lib/:/home/iidev20/sqllib/lib

LIBPATH=/opt/ibm/home/bigsql/resources/lib64/:/opt/oemclient/odbc71/64/lib/:/home/iidev20/sqllib/lib:/opt/oemclient/odbc60/64/lib

ODBCINI=/home/iidev20/bin/odbc.ini

DB2LIBPATH=/opt/ibm/home/bigsql/resources/lib64/:/opt/oemclient/odbc71/64/lib/:/home/iidev20/sqllib/lib:/opt/csdlclient/oracle/lib:/opt/oemclient/odbc60/64/lib:/opt/csdlclient/informix/64/lib

LD_LIBRARY_PATH=/opt/ibm/home/bigsql/resources/lib64/:/opt/oemclient/odbc71/64/lib/:/home/iidev20/sqllib/lib:/opt/oemclient/odbc60/64/lib

5. Restart to make configuration effective
Answer: What about using Fluid Query instead ? I have been using it on a POC using VM's and it works great. It will let you connect to BI, DB2, Oracle, and other NZ servers.

BigInsights Federation for Netezza


Optarg and Command Line Arguments

Question: I am getting command line arguments from the user.
I then do switch cases for the commands, for example:
    case 'f':
         *file_name = optarg;
         break;

I am unsure if I need to malloc for the pointer since I don't fully understand optarg.
This is how file_name is declared:
char **file_name;

Should I do
int length = strlen(optarg); // This gives a warning about types when compiling.

Then malloc for the string length + 1?
How should the malloc be done for this kind of problem? Remember the user is typing in the file_name into **argv.
EDIT: This is how I am calling this function and still getting segmentation fault.
int main(int argc, char **argv)
{
   char **file_name;
   parser(argc, argvm file_name);
}

void parser(int argc, char **argv, char **file_name)
{
  // Switch cases.
}

Answer: 'optarg' is just a pointer to an element in argv[]. So, it's safe to not allocate memory and copy the value which 'optarg' points to.
Suppose your programm is called with the following arguments:
myapp -a "hello" -b "world"

and your code is:
#include <stdio.h>
#include <getopt.h>

void parse_options(int argc, char* argv[], char ** first_arg, char ** second_arg)
{
  const char* opt_string = "a:b:";
  int opt = -1;
  opt = getopt(argc, argv, opt_string);
  while (opt != -1) {
    switch(opt) {
      case 'a':
        *first_arg = optarg; /* points to argv[2]="hello" */
        break;
      case 'b':
        *second_arg = optarg; /* points to argv[4]="world" */
        break;
      default:
        break;
      }
    opt = getopt(argc, argv, opt_string);
  }
}

int main(int argc, char* argv[])
{
  char* first = 0;
  char* second = 0;
  parse_options(argc, argv, &first, &second);
  printf("first=%s, second=%s\n", first, second);
  return 0;
}

My output:
freebsd% gcc -Wall main.c
freebsd% ./a.out -a hello -b world
first=hello, second=world

Comment: I keep getting Segmentation fault on this line file_name = optarg; Does it matter if I am doing this inside a function? (I will post the code above)
Comment: My logic is that I need to pass a pointer to the file_name pointer in order to receive the file name back in main.
Comment: I made some working example (see edited text), hope this helps.
Comment: Yep it did. I was calling the function but not giving it the address of file_name. &file_name. Thank you.
Answer: You say you have:
char **file_name;
...
switch (opt)
{
case 'f':
     *file_name = optarg;
     break;
...
}

The code crashes because you've not allocated space or initialized the variable file_name.
What you need to do depends on what you want to happen.  Most usually, you would change the definition of file_name to:
char *file_name = 0;
...
switch (opt)
{
case 'f':
     file_name = optarg;
     break;
...
}

This allows you to detect, after the loop/switch, whether a file name was provided, and you can provide a default or report an error if not. You can detect in the case whether a file name was provided previously and object if it was.
The alternative scheme is that you want to allow multiple uses of -f on the command line.  Then you need to build up an array of pointers, keeping a count of them.  You might write:
char **file_name = 0;
size_t num_files = 0;
size_t max_files = 0;
...
switch (opt)
{
case 'f':
     if (num_files == max_files)
     {
         size_t new_files = (max_files + 2) * 2;
         void  *new_space = realloc(file_name, new_files * sizeof(*file_name));
         if (new_space == 0)
             ...report out of memory error...
         file_name = new_space;
         max_files = new_files;
     }
     file_name[num_files++] = optarg;
     break;
...
}

This uses the odd property of realloc() that if the pointer passed in is NULL, it simulates malloc().  Alternatively, you can do an initial malloc() allocation outside the loop and do (the same) realloc() inside the loop.  The calculation on the size allocates 4 entries the first time around, then 12, then 28, etc.  If you're worried about the over-allocation, you can use realloc() again after the loop is done to resize to the correct size, but it likely won't affect things much.
After the loop, you have a list of files which you might process with:
for (size_t i = 0; i < num_files; i++)
    process_file(file_name[i]);

Optarg and Command Line Arguments


Draw rectangles in a circular pattern in turtle graphics

Question: To make use of the turtle methods and functionalities, we need to import turtle. "turtle"
comes packed with the standard Python package and need not be installed externally.
The roadmap for executing a turtle program follows 3 steps:
a- Import the turtle module.
b- Create a turtle to control.
c- Draw around using the turtle methods.
i need to make this
Instructions:

You should use the shown colors for the rectangles.
You can choose the side's length/ width for the rectangle, keeping the general view
of the output like the given diagram.
Make sure to properly set the starting position (x & y) of your drawing, to maintain
the above diagram (at the center).

NOTE!!
here is my code:
from turtle import *

# Set up the screen and turtle
screen = Screen()
t = Turtle()
t.speed(1)  # You can adjust the speed as needed

# Define colors
color_fill = "yellow"
color_border = "blue"
border_size = 5
gap_size = 10
rectangle_width = 50  # Adjust the width as needed
rectangle_height = 100  # Adjust the height as needed
circle_radius = 50  # Adjust the radius for the circular space

# Function to draw a colored rectangle with a border
def draw_rectangle_with_border(fill_color, border_color, border_size, width, height):
    # Draw the border
    t.pencolor(border_color)
    t.pensize(border_size)
    t.penup()
    t.goto(-width / 2, -height / 2)
    t.pendown()
    for _ in range(2):
        t.forward(width)
        t.left(90)
        t.forward(height)
        t.left(90)

    # Draw the fill
    t.fillcolor(fill_color)
    t.begin_fill()
    for _ in range(2):
        t.forward(width)
        t.left(90)
        t.forward(height)
        t.left(90)
    t.end_fill()

# Set the starting position
t.penup()
t.goto(0, -rectangle_height / 2)

# Draw the central circular space
t.pencolor("white")  # Set the color to match the background
t.fillcolor("white")
t.penup()
t.goto(0, -circle_radius)
t.pendown()
t.begin_fill()
t.circle(circle_radius)
t.end_fill()

# Calculate the total number of rectangles to form a complete circle
num_rectangles = 8
angle = 360 / num_rectangles

# Draw the circular pattern of rectangles around the central circular space
for _ in range(num_rectangles):
    draw_rectangle_with_border(color_fill, color_border, border_size, rectangle_width, rectangle_height)
    t.penup()
    t.goto(0, -rectangle_height / 2)
    t.left(angle)
    t.forward(gap_size)

# Close the window on click
screen.exitonclick()

and here is the output:
the output i get
and i am want to have this output :
the wanted output
Comment: The "NOTE!!" suggests using `from turtle import *` is poor advice. Sure, it saves a couple keystrokes, but it also leads to tons of potential bugs by dumping 100+ functions in the global namespace, where they can easily be overwritten by your local variables and create confusion between functional and instance modes. Also, don't forget to ask a specific question about your attempt. Your output seems like it just needs a pen up/move forward/pen down/draw box/pen up/move back sequence, more or less.
Comment: @ggorlen turtle is used to teach programming, not be used to engineer huge long-lasting applications, and different principles apply. "Typing less" is important for beginners, namespace pollution is not.
Comment: Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
Comment: @julaine It's not a matter of engineering, it's a matter of bug avoidance. When a library intended for beginners has as many footguns as Py turtle does, it's extremely frustrating for beginners. Using a namespace helps avoid hours of wasted effort. Two characters more of typing per line, plus a one-time initializer invocation seems worth the tradeoff for 99% of students, in my view. At least if a teacher does opt for the wildcard import, they should communicate the risks of it so the student can watch out for typical aliasing bugs and instance mode/functional interface confusion.
Comment: @julaine Here's another possible compromise: don't use the wildcard import, but import each necessary function explicitly. Still more typing, but realistically, they'll probably only need a half-dozen functions, and make sure `Turtle` isn't one of the imports to "disable" the instance mode. This could be provided to the student as a starter file with the necessary functions pre-imported. Something like `from turtle import forward, left, right, penup, pendown, exitonclick, goto, setheading`. All commands are now obvious and collisions less likely. Abbreviated commands can be used.
Answer: Your rectangles look pretty good, although you don't need to draw the fill and outline separately. All that's missing is moving forward and backward from the start point to the corner of each square:
from turtle import Screen, Turtle

def draw_rectangle_with_border(t, width, height):
    t.pendown()
    t.begin_fill()

    for _ in range(2):
        t.forward(height)
        t.left(90)
        t.forward(width)
        t.left(90)

    t.end_fill()
    t.penup()

def draw_rectangles_in_circle(t):
    color_fill = "yellow"
    color_border = "blue"
    border_size = 9
    rectangle_width = 60
    rectangle_height = 90
    circle_radius = 110
    num_rectangles = 8
    angle = 360 / num_rectangles
    t.pencolor(color_border)
    t.pensize(border_size)
    t.fillcolor(color_fill)
    t.penup()

    for _ in range(num_rectangles):
        t.forward(circle_radius)
        draw_rectangle_with_border(t, rectangle_width, rectangle_height)
        t.backward(circle_radius)
        t.left(angle)

if __name__ == "__main__":
    turtle = Turtle()
    draw_rectangles_in_circle(turtle)
    Screen().exitonclick()

Now, the actual image has a little bit of overlap from square to square, so you could do a mini-turn to make that happen and adjust to taste:
# ...
    t.penup()
    t.right(5)

    for _ in range(num_rectangles):
        t.forward(circle_radius)
        t.left(5)
        draw_rectangle_with_border(t, rectangle_width, rectangle_height)
        t.right(5)
        t.backward(circle_radius)
        t.left(angle)
# ...

Note that the from turtle import * advice is questionable. This adds over 100 methods into the global namespace, making it easy to encounter aliasing bugs and confusion between instance and functional interfaces. You're only using Screen and Turtle from turtle, so it's easy to import what you need explicitly, as shown above.
Comment: Thank you so much for your invaluable help!  Your code provided the perfect solution to my challenge, and I really appreciate the effort you put into explaining and optimizing it. The adjustments you made not only resolved my issue but also enhanced the visual appeal of my project. Your expertise has made a significant impact, and I'm grateful for your generosity in sharing your knowledge. Thanks again!
Answer: As always, @ggorlen's answer is excellent (+1), particularly the advice on how to import turtle.  With from turtle import * we see issues where programmers don't understand which are turtle instance methods and which are screen instance methods.  The explicit import reduces this problem.
Of course, in the interest of "better living through stamping", I've an alternate solution:
from turtle import Screen, Turtle

COLOR_FILL = 'yellow'
COLOR_BORDER = 'blue'
BORDER_SIZE = 4

NUMBER_RECTANGLES = 8
RECTANGLE_WIDTH = 30
RECTANGLE_HEIGHT = 50
CIRCLE_RADIUS = 30

OFFSET_FROM_PERFECT = 15  # in degrees (0 = perfect)
CURSOR_SIZE = 20

screen = Screen()
turtle = Turtle()
turtle.speed('fastest')
turtle.shape('square')
turtle.penup()

turtle.color(COLOR_BORDER, COLOR_FILL)
turtle.shapesize(RECTANGLE_WIDTH / CURSOR_SIZE, RECTANGLE_HEIGHT / CURSOR_SIZE, BORDER_SIZE)

turtle.sety(-CIRCLE_RADIUS)  # center image on current x, y
turtle.circle(CIRCLE_RADIUS, extent=OFFSET_FROM_PERFECT)

for _ in range(NUMBER_RECTANGLES):
    turtle.right(90 + OFFSET_FROM_PERFECT)
    turtle.forward(RECTANGLE_HEIGHT)
    turtle.stamp()
    turtle.backward(RECTANGLE_HEIGHT)
    turtle.left(90 + OFFSET_FROM_PERFECT)

    turtle.circle(CIRCLE_RADIUS, extent=360/NUMBER_RECTANGLES)

turtle.hideturtle()
screen.exitonclick()


Alternative to using a triple pointer

Question: I have a pointer to a structure in my code. I want to return an array of such pointers in an API. Something like
Status GetData(MyStructure*** data, int*lengthOfArray)
  {
     *data = malloc(sizeof(MyStructure*)* arbitaryNumber));
     for(i=0....)
     {
       (*data)[i] = pointerToMyStructure;
     }
  }

Is there an alternative to this? My API has to have a return type Status. So the only way forward I see at the moment is to turn into a 3 star programmer
Comment: Before you start "fixing" this by hiding pointer types inside typedefs, *don't*. What you have, though it takes a little gray matter to picture, speaks well. You have a dynamic pointer sequence (`**`) that must be passed as an in/out (`*`). You're code looks correct to do this. Just be clear when documenting how it works. If you have the freedom to change the functional interface, [you have other options](http://stackoverflow.com/a/23414948/1322972) that are a better fit.
Comment: It's the way it is and it's ok.
Comment: How do you plan to use `lengthOfArray`?
Comment: lengthOfArray gets filled with arbitaryNumber, in other words it tells the API caller, the length of the array it just got
Answer: Since you are returning the length of the array as well, and because the array of struct pointers and the length are related, you could build a special struct to get both items from your API together, like this:
typedef struct {
    MyStructure **data;
    int length;
} GetDataArg_t;

Now you can change your API like this:
Status GetData(GetDataArg_t *args) {
    args->length = arbitaryNumbdr;
    args->data = malloc(sizeof(MyStructure*)* arbitaryNumbdr));
    for(i=0....) {
        args->data[i] = pointerToMytructure;
    }
}

The call of your API would look like this:
GetDataArg_t args;
if (GetData(&args) == STATUS_SUCCESS) {
    ...
}

Comment: +1 pinning the tightly-related data together in this fashion is a strong move. If at all feasible to change the functional interface, this is worth pursuing.

Alternative to using a triple pointer


Is Hunspell thread safe?

Question: Is the Hunspell spelling library thread-safe?
Answer: The answer is NO,
A simple multi-threaded test application revealed that Hunspell uses per-instance resources for the spelling process, so only one thread can use it at any time - use locks/work queue/or instanciate a per-thread Hunspell instance.
Comment: You might the following article useful: [Spell Check, Hyphenation, and Thesaurus for .NET with C# and VB Samples - Part 2: Multi Threading](http://www.codeproject.com/KB/recipes/spell-check-asp-net-web.aspx).
Comment: And use the latest NHunspell versions from sourceforge, because there are som multithreading bugs in the older versions.
Comment: The current comments for this answer refer to NHunspell, where as the question refers to Hunspell. The different HunSpell implementations have different owners and vary in their implementation.

Is Hunspell thread safe?


What is the dual of the disc algebra viewed as a Banach space?

Question: Let $A$ be the disc algebra, i.e.,
$A=\{f\in C(\bar{U}):f \text{ is holomorphic in }U\}$, where $U$ is the unit disc in the complex plane. The norm considered is the supremum norm. Are there any established results about what its dual is?
Similarly, is there anything known about the dual of $C'([0,1])$, the space of continuously differentiable functions? The norm considered here is $\|f\|=\|f\|_{\infty}+\|f'\|_{\infty}.$
I would be grateful for some references on this, if any. Thank you.
Comment: Just an observation: for every compact $K \subset U$ and $n\ge 0$ the map $f \mapsto f^{(n)}_{|K}$ is continuous from $A$ to $C(K)$, because of Cauchy estimates. So one gets a whole bunch of functionals on $A$ involving derivatives of functions from  $A$ restricted to compact subsets.
Answer: The space $C'([0,1])$ is the direct sum of the subspace $\mathbb{C}\cdot 1$ of constant functions and the subspace $K$ of functions $f$ such that $f(0)=0$.  Furthermore, $K$ is isomorphic to $C([0,1])$ via $f\mapsto f'$.  It follows by the Riesz representation theorem that the dual of $C'([0,1])$ can be described as a direct sum $\mathbb{C}\oplus M([0,1])$, where $M([0,1])$ is the space of measures on $[0,1]$.  Explicitly, every functional on $C'([0,1])$ can be written uniquely in the form $f\mapsto cf(0)+\int f' d\mu$, for some $c\in\mathbb{C}$ and some complex Borel measure $\mu$ on $[0,1]$.
Comment: Neat. I find it  instructive to write a functional coming from $C[0,1]$, $f\mapsto \int f d \nu$ in the form given above.


How to determine root multiplicity from ONLY the graph?

Question: If you were given the graph of a function, without the function's equation, is there a way to determine exact multiplicity (not just parity) of the roots of the function?
Comment: Is this restricted somehow? Otherwise $x^{2012}$ and $x^{2014}$ might be hard to distinguish...
Comment: By graph do you mean a plot of the function on a piece of paper?
Comment: Plotting the function in the Cartesian Plane.
Comment: A polynomial or any function in general?
Comment: Mathematicians often use the term graph to mean the set of points $\{(x,f(x))\}$, in which case you know everything about the function. Also, it is a little ambiguous to talk about the multiplicity, for example, take $f(x) = x$ when $x \ge 0$ and $f(x) = x^2$ when $x <0$. Presumably you are talking about polynomials?
Comment: Exact multiplicity? I'd say no. For example, plot 4 functions, $y = x^2, y=x^3, y = x^4$ and $y = x^5$. Can you say anything about multiplicity of the root $x = 0$?
Comment: Yes, Polynomials. And relatively simple ones, think high school precalc.

I'm leaning towards you can't determine the exact multiplicty of the a zero/root _solely_ by observing the graph...just the parity of the multiplicity of that zero.
Answer: Well, naslundx is correct that we can determine what the equation of a polynomial is if we had a perfectly drawn graph. (We can then use that equation to figure out the multiplicity of the root.)
But if by "determine from the graph" you mean, just by looking at a hand-drawn figure, then no, there is no way. draks makes a good comment that illustrates why; I have better than 20-20 vision, but just by glancing at the graphs of $x^{2012}$ and $x^{2014}$, I could not determine which function was being graphed (unless the scale was huge). In fact, if the scale were small, I probably couldn't even tell the difference between $x^6$ and $x^{10}$ (or $(x+4)^5$ and $(x+4)^7$ etc.). As a result, I could not determine the multiplicity of the root from the graph alone, without the equation.
Comment: Yes, I agree.  It wouldn't necessarily be a hand drawn graph, but one which you are not guaranteed the exactness of the other points.  Thank you!
Comment: Yeah, no problem, and good point about it not necessarily being a hand-drawn graph.
Comment: nice example...
Comment: Agreed. Those first two numbers look quite nice at around this time; when I first read your comment to the question, it reminded me of how some Putnam problems are made. (Eh, by the way, when I first wrote this answer, I was thinking of adding more examples, and so after you made this comment, I edited my answer.)
Answer: If we know the function $f$ is a polynomial, and that we can take arbitrarily many exact points along the graph, we can.
From the plot we can pick $n$ points $(x_1, y_1), (x_2, y_2), \dots, (x_n, y_n)$ and using a Vandermonde matrix we can solve for all the coefficients, assuming $\deg f = n$.
From there we can 'easily' factorize (since we know the roots from the plot) to find the multiplicity of all roots.
Of course, this all depends on being able to find more or less exact values for the coordinates of the points.
Comment: I assumed that if you have the graph, you can use points along the graph (not just the x/y intercepts). In that case, if you can solve systems of equations, this method works.
If all you have are the x/y intercepts, my answer would be no.
Comment: My question would be for a high school precalculus class observing zeros, multiplicity and end behavior.

And all you are given is the graph, the only points being the x/y intercepts.
Comment: The only points you are given are the x/y intercepts, can't be sure about the rest.

Thank you!
Answer: Yes. Kind of. You can distinguish at least between multiple and simple roots.
The thing is that if a polynomial has a root $a$ of multiplicity $n$, then its derivative has that same root with multiplicity $n-1$. In particular, if (and I think only if) $n>1$ (it's a multiple root), then the derivative is zero at $a$. So, multiple roots are zeros where the graph is flat. A root is simple iff the polynomial "cuts" the $x$-axis.
As an example that should already be familiar to you, a quadratic has a multiple root if and only if it "kisses" $x$-axis.
Also, I believe the first two derivatives of a curve are zero at a point if and only if the curve is flat at that point, but not a local extremum (ie, like $x^3$ at $0$). So you can even tell the difference between roots of multiplicity $2$ and $>2$.


Combinations of multiset

Question: I am having a really hard time to come up with a result for this problem:
In how many ways can you give 3 board games, 2 movie tickets and 4 toys to your 3 friends, assuming that everyone gets at least something? The games, tickets and toys are undistinguishable
I think that the formula I should be using is: C(r + k -1, r) where r = 3 (for 3 kids), but how do I incorporate the difference, if I give ex. 2 games 1 movie ticket to 1 person and then something else to the other 2?
Thank you so much!
T
Comment: You can accept my answer using the checkmark to the left of it if it answers your question. ["What should I do when someone answers my question?"](http://math.stackexchange.com/help/someone-answers) - I would always advise waiting a few hours after asking before accepting just to see if anyone else answers.
Answer: I think your best bet is to use inclusion-exclusion to rule out the chance of someone being left out of the distribution. So essentially we start by ignoring the restriction that everyone gets something, and divide out the three types of goods anyway:
$$P_3 = \binom{3+2}2\binom{2+2}2\binom{4+2}2 = \binom 52\binom 42\binom 62 = 10\cdot 6\cdot 15 = 900$$
But then someone might get left out; so we calculate the number of way to divide between two selected people, which is, similarly,
$$P_2 = \binom{3+1}1\binom{2+1}1\binom{4+1}1 = \binom 41\binom 31\binom 51 =  4\cdot3\cdot 5 = 60$$
And when we subtract that off, we will have also oversubtracted the options available when everything goes to one selected person, which is:
$$P_1 = \binom 30\binom 20\binom 40 =1$$
of course.
So for the answer desired, note that we can choose two  people $3$ ways and one person $3$ ways, so we need the calculation
$$R = P_3 - 3P_2 + 3P_1 = 900 - 180 + 3 = 723 \text{ options}$$

Combinations of multiset


How to count number of rows in a sas table using x command

Question: Does anyone know how to count number of rows in a SAS table using x command, I need to achieve this through unix. I tried wc -l but it gave me a different result than what proc sql count(*) gives me.
Comment: use x command to start sas batch script that will proc sql count(*)
Comment: It is not clear to me what you mean. Please describe the problem more in detail and add an context, so that people can think about other strategies to tackle it.
Comment: To use the `X` command you need to be running SAS.  So just use SAS code to count the number of OBSERVATIONS in the DATASET.
Answer: Lee has got the right idea here - SAS datasets are stored in a proprietary binary format, in which line breaks are not necessarily row separators, so you cannot use tools like wc to get an accurate row count. Using SAS itself is one option, or you could potentially use other tools like the python pandas.read_sas module to load the table if you don't have SAS installed on your unix server.
Writing a script to do this for you is outside the scope of this answer, so have a go at writing one yourself and post a more specific question if you get stuck.
Comment: Thanks, this explains why wc and sql count (*) have different results. I'd give the python code a go.


Vocal free-time song: legato question, and slur ("portamento" vs "glissando") specific case

Question: I am an absolute beginner to music theory, and I am starting to study some songs which are free time (ie no time signature).
Looking at this score:

tentatively transcribed from this tune:

I was wondering:

if the "legato" sign towards the beginning is meaningful at all here, being a free-time score?

Wouldn't make more sense to just use a longer note, for the "de" syllable?

Also, listening to the corresponding song (see link above), I was wondering if those glissando notations (the wavy lines) are justified?

Wouldn't a "portamento" or "slur" notation be more proper in this case? I tried to jot down this score, to show what I mean (sorry if it is "not there" yet):

It seems among musician the "glissando" notation is more known, but I would like to know this from the pure vocal music point of view.
Note: This song in particular is from about 1980s, and belong to the Indian tradition of sacred songs (the words are in Bengali).
Answer: Given the recording, it seems clear that the gliss markings are intended to convey "portamento with vibrato" -- this seems like a reasonable way to notate it, especially if there were footnotes or similar indicating that this is the effect.
To me, the notation of "de-ba-ta" looks good; the "ba" syllable is a pick-up note leading into the ta syllable.  Although this is in free time, I'd think of it as: de is on the beat, ba is off of the beat (on an "and") and ta is on the beat.  Notating this dotted-half, quarter, dotted half would not capture what is done in the recording where the "ba" is significantly shorter than, e.g. the ji-ban syllables at the beginning.
Comment: Thank you so much, that is quite helpful. Just to clarify, would it still be all right to have portamento markings, instead of the gliss markings, if adding proper footnotes specifying "portamento with vibrato"? I am asking because a professional classical singer I know (ie performing regularly) was a little confused by the meaning of the gliss markings in this case, while the portamento or slur marking was conveying the concept better to him.
Comment: For the de we have a half note _tied_ (this is not a legato slur even though the symbology is the same) to an eighth note, indicating that the de is sustained until the ba syllable, which occurs off of the beat.
Answer: While "portamento" and "glissando" certainly mean different things, in everyday musical usage a  rather large number of people use 'gliss' for a portamento sound.  Sometimes you just have to decide which is a better fit to the piece of music in question, taking in mind the time period when it was composed as well as the 'mood' of the piece.
In this particular sound sample, it sounds rather more like a heavy vibrato rather than either a gliss or portamento to the following pitch.  YMMV  :-)
Comment: Thank you for pointing to the 'vibrato' option. What would you think of the legato notation between "de" and "ba" syllables, at the beginning? Is that necessary at all, being that free-time music?
Answer: The curve sign is not a "legato slur", but a "tie". Even if the song is written in free time, it doesn't mean that the song has no rhythmic structure. "ba" of "de-ba-ta" is off beat and prepares for the "ta". And "de" is still part of the same word.
Comment: Thank you for the clarification. I will vote up your answer, as quite helpful, but I still not have enough score. I will do it when I have, though. Thanks.


For $T\in \mathcal L(V)$, we have $\text{adj}(T)T=(\det T)I$.

Question: Let $V$ be an $n$-dimensional vector space over a field of characteristic $0$. For a linear operator $T\in \mathcal L(V)$, we know that $\bigwedge^n T=(\det T)I$, where $I:V\to V$ is the identity map.
Further, from this answer we can define the adjugate of $T$ as $\bigwedge^{n-1}T^t:\bigwedge^{n-1}V^*\to \bigwedge^{n-1}V^*$, where $T^t$ is the transpose of $T$. We write $T^\sharp$ as a shorthand for $\bigwedge^{n-1}T^t$.
The Question: It is a well-known formula that if $M$ is an $n\times n$ matrix with entries from a field $F$, then
$$\text{adj}(M)M=M(\text{adj}(M))=(\det M)I_n$$
where $\text{adj}(M)$ is the adjugate of $M$.
I am trying to formulate this fact in the language of linear maps rather than matrices.
The problem is that it does not mean anything to take the product of $T^\sharp$ with $T$. We just need to make a connection between $T^\sharp$ , $T$, and $\bigwedge^n T^t$.
Comment: These questions you've been asking are neat.  Is there a particular text you've been working through?
Comment: @Omnomnomnom
I am not following any book. Just that I have for sometime been thinking about exterior algebras. I came across exterior powers of vector spaces in a differential geometry course and realized that a lot of matrix facts can be said in a neat invariant way using this concept. I have always liked and searched for invariant and coordinate approaches whenever possible
Comment: correction: coordinate*free* approaches.
Answer: So let's assume that $V$ has a non-degenerate bilinear form $\langle\cdot,\cdot\rangle$ with a basis $e_1,\dots,e_n$ such that $\langle e_i,e_j\rangle = \delta_{ij}$, the Kronecker delta.  Let $*$ denote the Hodge star operator.  Note that we have the formula
$$ \langle x,y\rangle = *((*x)\wedge y) .$$
Let's identify any operator on $V$ with its matrix representation.  Then we have the following identity:
$$ \langle Tx,y\rangle = \langle x,T^T y\rangle \quad (x,y \in V) .$$
Now we extend $T$ to all of $\Lambda(V)$ in the standard way by the formula $T(x\wedge y) = Tx \wedge Ty$.  Then we have the identities
$$ \text{adj}(T)^T x = *(T(*x)) \quad (x \in V) ,$$
$$ \det(T) = *(T(*1)) ,$$
noting that $*1 = e_1\wedge e_2 \wedge \cdots \wedge e_n$.
Then for all $x,y \in V$, we have
$$ \langle \text{adj}(T)^T x,Ty\rangle = *(T(*x) \wedge Ty) = *(T((*x)\wedge y)) = \det(T) \langle x,y\rangle .$$
Since $\langle\cdot,\cdot\rangle$ is non-degenerate, we have
$$ \det(T) I = (\text{adj}(T)^T)^T \cdot T = \text{adj}(T) \cdot T .$$
Comment: It is a bit unsatisfactory that we need to put an inner product structure. Do you know if a way which does not require an inner product?
Comment: I have been wondering if there is some kind of Hodge star operator that takes $\Lambda^{(k)}(V)$ to $\Lambda^{(n-k)}(V^*)$ without using an inner product.  I don't yet have an answer, and I have never seen anything like this before.
Comment: But also, once you have picked a basis, you no longer need the inner product structure, because you can define the star operation directly using the basis elements.  Also, if you want to define it over fields of characteristic greater than zero, it's not obvious to me what the inner product would even mean.
Comment: But now that I look at https://en.wikipedia.org/wiki/Hodge_dual, specifically at https://en.wikipedia.org/wiki/Hodge_dual#Formal_definition_of_the_Hodge_star_of_k-vectors, I see that the definition requires a non-degenerate bilinear form.  So over the complex numbers, this would not be the standard inner product, which is sesqui-linear.
Comment: So instead, start with a basis, and make https://en.wikipedia.org/wiki/Hodge_dual#Computation_of_the_Hodge_star the definition.  And of course, once you have picked a basis, you also have a bilinear form for free.
Comment: There is a natural pairing of $\Lambda^k V^*$ and $\Lambda^k V$ defined by $\langle \phi^1\wedge\cdots\wedge \phi^k,\ v_1\wedge\cdots\wedge v_k\rangle = \det[\phi^i(v_j)]$. I had a feeling that this pairing could be used to achieve the same things as done by the Hodge star. As for an operator that takes a member $X\in \Lambda^k V$ to a member $\Lambda^{n-k}V^*$, I can think of contracting a fixed volume form, say $\omega$, on $V$ by $X$. We would then have $\langle i_X\omega, Y\rangle = \langle \omega, X\wedge Y\rangle$ for all $Y\in \Lambda^{n-k} V$.
Comment: @caffeinemachine I think you should see if you can make it work.  Can you send me a message if it works out?  (My work is catching up to me, so I have to go and do other things.)
Comment: Sure. Thanks for the help!
Comment: @StephenMontgomery-Smith I have given below a proof that uses neither a basis nor an innter product (in case you are interested in this).
Answer: I'll propose to you another (slightly different, but isomorphic) definition of the adjugate (classical adjoint). Im borrowing from section 8 of http://people.reed.edu/~jerry/332/27exterior.pdf .
Let $f:V\rightarrow V$ (with $n$ the dimension of $V$). We have a canonical isomorphism $\phi:V=\wedge^1 V\rightarrow\mathrm{Hom}(\wedge^{n-1} V,\wedge^n V)$ induced by the Wedge product. Let the adjugate $\mathrm{adj}(f):V\rightarrow V$ of $f$ be obtained from $\mathrm{Hom}(\wedge^{n-1} f,\wedge^{n} V)$ via $\phi$, i.e. $\phi\circ \mathrm{adj}(f)=\mathrm{Hom}(\wedge^{n-1} f,\wedge^{n} V)\circ\phi$. It is then easy to check that $\mathrm{adj}(f)\circ f=\det(f)\mathrm{id}_V$: simply check $\phi((\mathrm{adj}(f)\circ f)v)=\phi(\det(f)v)$ for every $v\in V$.
Comment: That's a slick idea!


virtualbox full disk though enough free space

Question: Guest Ubuntu:
df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              15G   14G  116M 100% /
disk analyzer:
14GB used 13.3 free 900MB
/ 100% 6,6GB
7 GB free space are missing
can someone help?
Comment: not really a programming question - more likely to get a response on superuser.
Answer: Ok i found out that the files were still lying in the other location of the root trash which has not been shown.
By removing them from there the space was given free.
Hope it helps if anyone encounters the same error.


How to use setState to update the property of an object inside an array the right way in class component?

Question: I have an array of objects in the state with the following structure:
const arrayOfTests = [
    {
        id: 1,
        name: "test1",
        description: "test description"
    },
    {
        id: 2,
        name: "test2",
        description: "test description"
    },
    {
        id: 3,
        name: "test3",
        description: "test description"
    }
]

Lets say I find object with id of 3 like this:
let object3 = arrayOfTests.find((element) => {
    return element.id === 3;
})

And then I edit it like this:
object3.description = "new description"

I'm kinda confused how to use setState to replace/edit the old object 3, with the new updated one.
Comment: you could also use https://github.com/immerjs/immer to simplify this
Answer: Use the map() method as shown below.
Inside the callback function, check if the condition element.id === 3 is true; if it is, return a new object. Otherwise, return the current element as it is.
let newState = arrayOfTests.map((element) => {
    if (element.id === 3) {
       return { ...element, description: "new description" };
    }

    return element;
});

Pass the array returned by the map() method to the state setter function.
this.setState({ state: newState });

Answer: setState((prevState) => ({
  ...prevState,
  arrayOfTests: prevState.arrayOfTests.map((item) =>
    item.id === 3 ? { ...item, description: "new description" } : item
  ),
}));

Answer: this.setState({
  arrayOfTests: arrayOfTests.map(item => {
    if (item.id === 3) {
      return {
        ...item,
        description: 'new description'
      }
    }
    return item
  })
})

Answer: const [test, setTest] = useState([
        {
            id: 1,
            name: "test1",
            description: "test description"
        },
        {
            id: 2,
            name: "test2",
            description: "test description"
        },
        {
            id: 3,
            name: "test3",
            description: "test description"
        }
    ]);

    const handleUpdate = (index) => {
        const some_array = test;
        var newState =  some_array[index];
        newState.description =  "new description";
        some_array[index] = newState;

        setTest(some_array);
        console.log(test);
    }

return(
      {test.length > 0 && test.map((data, index)=>{ return(handleUpdate(index)}>{data.description})
       })
}
)


How to configure crawler extension to exclude hidden fields from tt_news?

Question: I use this config for my crawler config for tt_news:
tx_crawler.crawlerCfg.paramSets {
    items = &tx_ttnews[tt_news]=[_TABLE:tt_news;_PID:6;]
    items {
        baseUrl = http://www.example.com
        cHash = 1
        procInstrFilter = tx_indexedsearch_reindex
    }
}

Resulting the crawler to index also hidden news entries.
Answer: You can expand the config with a where clause:
tx_crawler.crawlerCfg.paramSets {
        items = &tx_ttnews[tt_news]=[_TABLE:tt_news;_PID:6;_WHERE: and hidden = 0]
        items {
            baseUrl = http://www.example.com
            cHash = 1
            procInstrFilter = tx_indexedsearch_reindex
        }
   }

Now all the records with hidden = 1 are not crawled. Its not documented, but in the source:
https://github.com/TYPO3-extensions/crawler/blob/master/class.tx_crawler_lib.php#L752


Why do we approximate the joint in ELBO if we already have access to it?

Question: I realized in variational inference, our goal is to approximate $p(z|x)$ with $q(z)$. So we minimize $KL(q(z) || p(z|x)) = \mathbb{E}_{z \sim q} log\frac{q(z)}{p(z|x)}$. We then manipulate, through some simple steps, to arrive at:
$$ = \mathbb{E}_{z \sim q} \left[ log \frac{q(z)}{p(z,x)}\right] + logp(x)$$
and then the objective becomes optimizing $q$ such that it is close to the joint... but don't we already have access to the full functional form of the joint? What's the point of approximating the joint if we already have access to it?
TLDR We want to minimize $KL(q(z) || p(z|x)) = \mathbb{E}_{z \sim q} log\frac{q(z)}{p(z|x)}$ but we don't have access to the posterior so we can't. We then decide to minimize $KL(q(z) || p(z,x)) = \mathbb{E}_{z \sim q} log\frac{q(z)}{p(z,x)}$ since we have access to the joint. My question, is why even approximate if we already have the joint?
Comment: Which of the expressions that you write down is supposed to be the full functional form of the joint probability?
Comment: I believe we have access to the full functional form of the joint, p(z, x), since we as the model builders specify p(z) as well as p(x|z). E.g. p(z) is a 2 variate normal with specified mean/cov, and p(x|z) is a normal with mean z[0] and var z[1]. We can just plug in the prior into the likelihood to get its full functional form, and then multiply by the prior to get the full functional form of the joint. Right?
Comment: Can you explicitly write out the form of the density for $p(x,z)$?
Comment: We have $z0, z1 \sim N(\mathbf{\mu}, \mathbf{\Sigma})$. First, sample a particular pair from this, call it $z0_i, z1_i$. Then $p(x_i, z_i)$ (where $z_i = \begin{bmatrix} z0_i \\ z1_i\end{bmatrix}$) can be written as $p(z_i) * p(x_i | z_i) = c_1*exp\left( -.5(z_i-\mu)^\top \Sigma^{-1}(z_i-\mu)\right) * c_2 *e^{\frac{-(x_i-z0_i)^2}{2z1_i^2}}$. I just wrote the normalizing constants as $c_1$ and $c_2$ but hopefully its clear what I mean. We have access to mu and sigma (we specify the prior on z) and can sample a $z_i$ vector, and have access to $x_i$ (we have data afterall). Is this right?
Comment: If this is your model, then is seems like you're not parameterizing a neural network — the usual VAE usage
Comment: Ah I see, so VI and the ELBO are only relevant for more complex models, involving things like neural nets?
Comment: Yeah, your core observation is correct — you only need an approximation when the thing you're computing is intractable. But if you can be exact for little/no cost, then go for it!
Comment: Sorry for the late follow up, but can you elaborate on intractability? Let's go with the example of a VAE which I'm familiar with and its a common example of VI. What exactly is the intractable part of the joint? In principle when you instantiate the encoder and decoder, they have parameters so they can compute things (namely a latent distribution and data distribution respectively). Where does intractability fit in?
Comment: The normal distribution model you've proposed here seems to be the one you're most familiar with & it is not intractable. The intractable problems arise from the latent variables $z$ and model parameters $\theta$, which do not have simple conjugate form in a convolutional neural network. I recommend reading Carl Doersch, "Tutorial on Variational Autoencoders"
and the original VAE papers for more detail.
Answer: As you stated, we have:
$$
KL(q(z) || p(z|x)) = \mathbb{E}_{z \sim q} \left[ \log \frac{q(z)}{p(z,x)}\right] + \log p(x),
$$
where $-\mathbb{E}_{z \sim q} \left[ \log \frac{q(z)}{p(z,x)}\right]$ is also called the ELBO. And the goal is to minimize $KL(q(z) || p(z|x))$. And you are right, since the log evidence is constant, minimizing the RHS means maximizing the ELBO, which in turn means approximating $p(x, z)$ with $q(z)$.
But note that, although we do know the functional form of the joint $p(x, z) = p_\theta(x, z)$, we usually don't know the value for its parameter $\theta$. The same goes for $q(z)$: the functional form for $q(z) = q_\phi(z)$ is usually known (because it is chosen to be simple), but the parameter $\phi$ is not known. So the task is to find the parameters $\theta$ and $\phi$ by maximizing the ELBO.
E.g., think of some state space model, where you want to use variational inference to obtain the posterior of the state space $z$ given the observations $x$. You know that the state space model is defined by some parameters $\theta$ but those are not known to you. Next, you presume some function $q_\phi(z|x)$ that should approximate $p_\theta(z|x)$. This $q_\phi$, too, depends on some yet unknown parameters $\phi$ that need to be inferred (otherwise there would be nothing left to do since you then already know your approximation $q$). Another popular example would be the VAE where the parameters of the densities $p_\theta(x|z)$ and $q_\phi(z|x)$ like e.g. mean and covariance, are given by (deep) neural networks of prescribed architecture and the parameters $\theta$ and $\phi$ are the weights of the neural networks that need to be learned.
Comment: Thank you for the detailed response! Regarding your first point: Why exactly does it not mean we are approximating p(x, z) with q? We want to minimize the RHS, we don't have access to the evidence so we simply minimize the expectation term, which by definition, is the KL between the variational and joint. Isn't our final step to minimize KL between q and the joint? Doesn't this mean we apx the joint with q? Regarding the second point: What models would you not have access to the functional form? I'm thimking of an HMM, we specify p(z) and the likelihood, can't we multiply and have the joint?
Comment: @Addison I edited the answer.
Comment: Thanks for the update @frank, I'm a bit confused on how we wouldn't have access to the parameter $\theta$. If we have access to the functional form of the joint, this means we can write it as a formula e.g. if p(z) is N(0, 1) and p(x|z) is N(z, 1), then $p(x,z) = N(0, 1) * N(z, 1)$ right? Is the issue that we have to sample from the prior to get a $z$ which then dictates the likelihood? Or maybe I'm missing the point somehow or am not 100% certain on whats meant by the parameters of the joint here
Comment: @Addison Again, I added more information to the answer.


Why there are no icons for the buttons in itemselector?

Question: I am trying to use the Ext.ux.form.ItemSelector,but no icons are on the add/remove/top/down buttons!
Code below:
Ext.create('Ext.ux.form.ItemSelector', {
        renderTo: Ext.getBody(),
        name: 'channelRange',
        store: Ext.create('Ext.data.Store', {
            fields: ['display', 'value'],
            data: [{
                display: '22',
                value: 2
            }, {
                display: '33',
                value: 3
            }]
        }),
        buttons: ['add', 'remove'],//No icons on the buttons! Why?
        //imagePath: '../ux/images/',
        displayField: 'display',
        valueField: 'value',
        value : [ 3 ],
        allowBlank: false
    });

See the result here, the buttons between the 2 multiselect have no icon!
Answer: Yes it is an issue with ItemSelector. You will not see any icon on buttons after any kind of BUILD.
I was able to resolve this issue permanently via overriding below CSS. Actually if you debug the code you will see that it is trying to location these button icons on different location.
// Do these overrides in any of your CSS file.
.x-form-itemselector-top {
    background-image: url(images/itemselector/top.gif) !important;
}
.x-form-itemselector-up {
    background-image: url(images/itemselector/up.gif) !important;
}
.x-form-itemselector-add {
    background-image: url(images/itemselector/right.gif) !important;
}
.x-form-itemselector-remove {
    background-image: url(images/itemselector/left.gif) !important;
}
.x-form-itemselector-down {
    background-image: url(images/itemselector/down.gif) !important;
}
.x-form-itemselector-bottom {
    background-image: url(images/itemselector/bottom.gif) !important;
}

NOTE: Also make a sure you have these icons on given path  (images/itemselector/). In my case I copied these icons from "ui-client\ext\packages\ux\classic\resources\images\itemselector" to "ui-client\resources\images\itemselector". Here "ui-client" is my application root folder.
Hope above provided information will help.
Answer: Solution from my project:
grid.tbar.push({
    itemId  : 'create',
    iconCls : 'iconAdd',
    text : 'Add'
}

and config in some .css file:
.iconAdd {background: url(/resources/img/action-add.png) no-repeat !important;}


Diferenças entre getClass().getResourceAsStream() e getClass().getClassLoader().getResourceAsStream()

Question: Na necessidade de carregar um recurso em meu projeto me deparei com estes dois métodos. Da escolha entre um dos dois surgiram algumas dúvidas sobre as quais gostaria de compartilhar aqui.

Qual a motivação ou em que cenário cada um deve ser usado?
Existe alguma vantagem de um relação ao outro? Se sim, qual seria esta?
Comment: Boa pergunta, +1!
Answer: A diferença entre como o valor passado como argumento é interpretado é bem sutil.
Basicamente, você tem dois métodos diferentes: ClassLoader.getResourceAsStream() e Class.getResourceAsStream(). Estes dois métodos irão localizar o recurso de maneira diferente.
Class.getResourceAsStream()
Em Class.getResourceAsStream(caminho), o caminho é interpretado como um caminho local para o pacote da classe que você está o método.
Por exemplo, chamando String.class.getResourceAsStream("file.txt") será procurado um arquivo em seu classpath no seguinte local: java/lang/file.txt.
Se o caminho começa com um /, então ele será considerado um caminho absoluto e começará a procurar a partir da raiz do classpath.
Então, chamando String.class.getResourceAsStream("/file.txt") o arquivo será procurado no seguinte local: ./myfile.txt.
ClassLoader.getResourceAsStream(caminho)
Já em ClassLoader.getResourceAsStream(caminho), a implementação considerará que todos os caminhos sejam absolutos.
Então, chamando String.class.getClassLoader().getResourceAsString("myfile.txt") e String.getClassLoader().getResourceAsString("/myfile.txt"), ambos procurarão pelo mesmo arquivo no classpath, no seguinte local: ./myfile.txt.

Existe alguma vantagem de um relação ao outro? Se sim, qual seria
esta?

Bom, depende muito dos seus requisitos, detalhes da sua aplicação, inicialmente, o mais óbvio é que, para carregar arquivos dentro do mesmo pacote da sua classe, você deve utilizar: Class.getResourceAsStream().
Recomendo a leitura do seguinte artigo, é bem interessante: Smartly load your properties


Solve for the positive values of $x$ in $\ln(e^{\frac{1}{2}}+\sin x) = \frac{1}{2}$

Question: I tried:
$$\ln(e^{\frac{1}{2}}+\sin x) = \frac{1}{2} \Leftrightarrow \\
e^{\frac{1}{2}}+\sin x = e^{\frac{1}{2}} \Leftrightarrow \\
0 = \sin x \Leftrightarrow \\
x = \arcsin(0) +2k\pi \lor x = \pi-\arcsin(0)+2k\pi \Leftrightarrow \\
x = 2k\pi \lor x = \pi+2k\pi$$
$$k \in \mathbb{Z}$$
However, my book says the solution is:
$$x = k\pi$$
Did I do something wrong or is my book's solution just the simplyfied version of mine?
Comment: you solution is the same that the book, you have divided $x$ in your solution in the set of odd multiples of $\pi$ and the even multiples of $\pi$. Together they are the multiples of $\pi$.
Comment: Take a look at the graph of $y=\sin x$. You will see that $\sin x=0$ at every integer multiple of $\pi$.
Answer: If,
\begin{align*}
\sin\theta&=x\\
\Rightarrow \sin^{-1}x&=n\pi+(-1)^n\theta\hspace{25pt}\text{ where, }n\in\mathbb Z
\end{align*}
This is the General Value of $\sin^{-1}x$, and the Principal Value of $\sin^{-1}x$ is $\theta$ only.
Comment: Interesting and simpler than what I have been using, thanks.
Answer: The values of $x$ for which $\sin x=0$ are $0,\,\pm\pi,\,\pm2\pi,\, \pm 3\pi, \ldots,$ i.e. $k\pi$ for $k\in
\mathbb Z.$
Other than that, your solution looks good.


Bloquear páginas usando login e session

Question: Estou a desenvolver um site que tem um painel administrativo.
Esse painel tem um login, o meu problema é que eu consigo entrar dentro do painel através do link, ou seja não está seguro, qualquer pessoa é capaz de entrar se souber o nome dos ficheiros. Exemplo:
http://localhost/om/admin/Painel.php

Se eu fizer isto entra me no site e consigo realizar tudo sem ser administrador, pra corrigir isto sei que pode ser feito com session, mas não sei como fazer.
Não tenho qualquer código, apenas tenho um ficheiro chamado _config.php que está a ser chamado em todos os ficheiros do painel.
Comment: Aconselho antes de criar uma dúvida  dar uma pesquisada no StackOverFlow pois o mesmo já possui várias respostas para este problema, veja uma resposta minha por exemplo: http://pt.stackoverflow.com/questions/70758/acesso-somente-via-login-e-senha-inibindo-acesso-direto-via-url/70765#70765
Answer: No arquivo que verifica as informações do usuário, armazene o ID do usuário em uma sessão.
<?php
session_start(); // Inicia a sessão

// Pega os dados do usuário
$stmt = $con->prepare("SELECT usuarioId FROM usuarios WHERE login = ? AND senha = ?");
$stmt->bind_param('ss', $login, $senha);
$stmt->execute()
$res = $stmt->get_result();

// Verifica se encontrou o usuário
if ($res->num_rows){
    $row = $res->fetch_array(MYSQLI_ASSOC);
    $_SESSION['user'] = $row['usuarioId']; // Marca a global para verificar se o usuário está logado.
    header('Location: http://localhost/sitema/inicio.php'); // Página do sistema
    exit; // Encerra a execução do script
} else {
   // Se não encontrou o usuário, manda de volta para o form de login
   header('Location: http://localhost/sitema/login.php'); // Página do sistema
   exit; // Encerra a execução do script
}

Crie o arquivo: restrito.php
<?php
session_start();
// Se o usuário não está logado, manda para página de login.
if (!isset($_SESSION['user'])) header("Location: http://localhost/sistema/login.php");
exit; // Encerra a execução do script

Inclua o arquivo no inicio de todas as páginas que você quer bloquear para quem não esteja logado.
Atenção: O arquivo restrito.php deve ser a primeira expressão da sua página, antes de qualquer outra coisa. Exemplo:
<?php require_once('caminho/para/restrito.php');?><DOCTYPE html>...

Não pode ter nem mesmo um  (espaço) em branco antes.
Comment: dá tudo o unico problema é que ao fazer login com os dados certos aparece na mesma a mensagem que nao tenho permissao
Comment: A sua consulta ao banco está trazendo os dados certo? Caso sim, você registrou ali nas variáveis `$_SESSION` depois da consulta que o usuário está logado? Poste o seu código aqui para vermos o que está dando errado, o arquivo que faz a consulta, o arquivo de restrição e um arquivo de página. Assim podemos analisar melhor o problema.
Comment: O código que tenho é o que está acima, o problema é que a coluna para fazer o login nao deve de estar preparada para arrays, nao da para fazer outra maneira mais simples sem ser a que esta em cima, esse array
Comment: Dei uma simplificada ali no código, se ainda estiver com dificuldades deixe-nos saber. A forma de fazer é usando Session mesmo, poderia usar Cookies mas acredito que é mais complicado, e não necessário. A variável `$_SESSION`, nada mais é do que um `array` global, para a utilização delas você sempre tem que declarar o `session_start()` no início do código, tem que ser sempre a primeira coisa (com raras exceções).
Comment: isto diz que tenho um ciclo de redireccionamento
Comment: verifica se o arquivo `restrito.php` está sendo incluído na página de login, no caso a página que tem o formulário para login. Ali não pode ser restrito, senão o usuário nunca vai conseguir entrar no sistema.
Comment: é assim eu tenho 2 ficheiros( um que tem o form do login) e outro ficheiro que tem a ligaçao do login a base de dados as verificações e isso tudo, onde tenho de meter isto : 0){
    $_SESSION['user'] = $resultado[0]['usuarioId'];
} é nesse ficheiro do login que ta a fazer as verificaçoes ... á base de dados certo?
Comment: Isso, correto, e se não encontrar o usuário tu redireciona pro form de novo. Se encontrar tu redireciona pra página do sistema.
Comment: Atualizei o código ali, com alguns detalhes que ficou faltando, também comentei ele pra tu entender melhor o processo, veja se ajuda.
Answer: Este problema é bem comum e cabe a você definir quem pode acessar a pagina ou não.
O exemplo do @Carlos ilustra bem como solucionar o problema.
A lógica por tras disso é:
Todos os usuários tem acesso a todas as páginas de sua aplicação, portanto você deve definir um nivel de acesso a cada um. Neste exemplo acima o sugerido foi testar se o usuário já fez login anteriormente e permitir o acesso. Caso não tenha feito login ele da uma mensagem de erro.
Muitos web-sites redirecionam o usuário para a tela de login novamente caso o usuario não tenha permissão.
Lhe recomendo ler a função header do php.
Com esta função você pode redirecionar o usuário a outra página.
Observações importantes:

Lembre-se que quando for utilizar sessão você sempre deve chamar a função session_start() antes de trabalhar com a variavel super global $_SESSION;
Quando for utilizar a função header para redirecionar um usuário você deve tomar o cuidado para não mandar nenhuma saida (output) para o navegador, seja ela um espaço, um enter, ou uma tag HTML qualquer.
Answer: Tomei a liberdade de criar este início de classe que vai de encontro ao indicado.
desta forma no futuro será incluída toda a lógica necessária para o teu projeto adicionando um método a classe.
É um inicio....
class Auth {

    public static function handleLogin(){
        session_start();
        if (!isset($_SESSION['user_logged_in'])) {

            header('location: ' . URL);
            exit();
        }
    }
}

No início de cada página basta colocar...
Auth::handleLogin

Para poder funcionar tem de ser iniciada variável URL por exemplo...
define('URL', 'http://localhost/');

E assim que as credenciais sejam validadas deverá :
$_SESSION['user_logged_in']=true;

Comment: gostaria de falar consigo pessoalmente, seria possivel?
Comment: Coloque a sua questão... tenho todo o prazer em Ajudar! Se gostou da resposta não se esqueça de votar... É assim que está comunidade funciona.
Comment: Eu sou novo nisto, e estas respostas todas me deixaram confuso
Comment: Opa, @cloud, a resposta do chambelix é boa, você pode usar no lugar do arquivo `restrito.php`. Apesar de que pra quem está iniciando pode ser um pouco confuso. Eu estou fazendo um "mini curso" sobre programação orientada a objetos, criando justamente um painel de administração, se quiser acompanhar no blog [Devcia](http://devcia.com/category/series/phpoo-na-pratica/)
Comment: Tudo bem...por chat room?
Comment: Deixe estar, não o quero incomodar, isto de qualquer das maneiras tem que ir, voces ja me ajudaram bastante eu vou vendo por mim e testando o que da errado e o que da certo, obrigado pessoal
Comment: Tudo bem... Já agora se quiser fazer uma validação específica diga... Queria também recomendar a criação de uma classe para lidar com o Session... Se está a começar leia e entenda as classes em php pois vai ser muito útil.
Comment: o problema é depois aplicar no site ... mas devagar se vai ao longe ^^
Comment: Pelo que apresentei ao aplicar em produção o URL tem de ser o domain... Ou seja substituir o localhost pelo dominio. No entanto para colocar em produção uma versão bem mais melhorada deveria de acontecer.
Answer: Quando efetua o login joga as informações em uma session depois em todas as páginas verifica se existe a session antes de exibir a página caso contrario redireciona para o login.
Obs.: Caso não tenha ficado claro me avisa que complemento.
Comment: é assim eu nao percebo muito de session
o login funciona vai a bd verificar os valores ....
o que eu quero é nao entrar no painel atraves do link :s

Bloquear páginas usando login e session


Combining elements of list of lists by index

Question: Consider the following list of lists:
lst = list(list(c(1,2), c(3,4)),list(c(5,6), c(7,8)),list(c(9,10), c(11,12)))

The list lst contains three lists, each list containing two vectors as elements. I would like to combine the elements of the underlying lists by index. In other words, I would like to merge vector 1 from list 1 with that of list 2 and list 3, and vector 2 from list 1 with that of list 2 and list 3, etc...
This is the result I am looking to achieve:
res = list(c(1,2,5,6,9,10), c(3,4,7,8,11,12))

I know that this can be achieved as follows in case of two separate lists:
mapply(c, lst1, lst2)

However, I am not sure how to replicate the same logic using a list of lists.
Any efficient way to achieve that? Please keep in mind that in reality, lst is a list of 5000 lists, and each underlying list contains a large number of vectors.
Thanks!
Comment: The problem is that in reality, each of the individual lists contain wayyy more vectors, and not only 2. It wouldn't be efficient/feasible to write the same equation indefinitely for each vector.
Comment: Maybe  `sapply(1:length(lst) function(j)list[[j]][[1]])`  and similarly but ending in `[[2]]` will suffice?
Answer: You can do:
do.call(Map, c(c, lst))

Comment: `Map` is like `mapply` with `SIMPLIFY = FALSE`. I was suggesting you try `do.call(Map, c(cbind, lst))` but it's hard to tell exactly without an example.
Comment: Nice. I guess ours are basically identical, but `Map` sure is cleaner.
Comment: I am not sure why I am getting the following error when applying it to my practical problem: `Error in match.fun(f) : '100' is not a function, character or symbol` ...
Comment: maybe you have a variable `c` thar has a value of `100`. You could replace `c` above by `base::c`.
Comment: Great thanks. Would this work if the underlying lists have elements as matrices, and not vectors?
Comment: depends what you want to do with matrices, you might want to replace `c` by `cbind` or `rbind`.
Comment: I have never worked with Map before. Could you please show me how you would adapt it to achieve that? I have tried changing to `cbind` but it is not doing the trick..
Comment: @Mariam - in that case you may be better off learning what `cbind` and `c` and `mapply` actually do.  It sounds like you're a bit new to the paradigms under which `R` works.
Comment: @CarlWitthoft I actually am very familiar with those methods, and I have modified the above formula using cbind and couldn't achieve the proper result
Answer: You're on the right track:
do.call(function(...) mapply(c,...,SIMPLIFY = FALSE),args = lst)
[[1]]
[1]  1  2  5  6  9 10

[[2]]
[1]  3  4  7  8 11 12

Comment: Great thanks! How about if each underlying list contains matrices and not vector, and I would want to merge the matrices next to each others. How can I adapt your formula to doing that?
Answer: I was looking for something along the lines of the OP's question... but with a list of data frames instead of vectors. In that case, slightly modifying @joran's answer above gives the desired result. Consider:
mylist <-
  lapply(1:2, function(y){
    df1 <- data.frame(a=y, b=y^2, c=y^3)
    df2 <- data.frame(d=y, e=y+1)
    return(list(df1=df1, df2=df2))
    })

You can then re-combine the sub-list elements into separate data frames based on their common indexes:
mylist2 <- do.call(function(...) Map("rbind", ...), mylist)


É realmente necessário usar métodos para mutator e acessor (setter e getter) no PHP? E o desempenho?

Question: Venho percebendo que a maioria de outras bibliotecas utilizam os métodos setters e getters  (doravante denominados de mutator e accessor), para alterar a propriedade de algumas classe.
Por exemplo:
class User
{
    protected $name;

    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    public function getName()
    {
        return $this->name;
    }
}

Esse é um exemplo clássico. Eu tenho uma classe que representa o usuário, e a mesma possui uma propriedade chamada name, que representa o nome do usuário. Para alterar o valor de name, precisamo usar User::setName, e para obtê-lo, User::getName.
Por um lado podemos pensar que seria mais simples fazer algo como:
$user = new User;

$user->name = 'Wallace';

Concordo que essa maneira seria a mais simples. Porém há casos onde eu reconheço que é necessária a criação de uma forma de validar como esse nome pode ser inserido ou tipo da variável.
Exemplo:
public function setName($name)

{
    if (is_string($name) && count(explode(' ', $name)) >= 2))
    {
        $this->name = $name;
        return $this;
    }

    throw new InvalidArgumentException('Name is not valid');
}

Olhando por esse lado (e sempre só olho por esse lado), percebo que é útil usar mutators por conta de poder validar os dados que vão ser definidos num determinado objeto (como validação de tipos, ou validações especificas para um atributo).
Outro detalhe é que, se você estiver programando para interfaces, a mesma no PHP não aceita a definição de propriedades obrigatórias num objeto, e sim somente métodos e constantes.
Mas algumas questões que sempre me veem à cabeça é como a de um amigo meu que disse: "PHP não é Java, não é necessário fazer tais coisas".
Então vai aqui algumas questões sobre isso:
Já ouvi também alguns boatos de que encher uma classe de métodos pode reduzir a performance.
Outro detalhe também é que vejo implementações de mutator/acessor com o uso de métodos mágicos __isset, __get e __set, ou ainda __call e __callStatic. Alguns dizem que também há uma questão de redução de desempenho quando os usamos.
Sendo assim:

Quando eu deveria usar ou não mutator/accessor numa classe? Essa forma é utilizada visando padronizar o código, ou visando uma implementação, ou é apenas para imitar o Java? (o último comentário realmente eu já ouvi)
Existe algum caso (real) que eu não precise utilizar (com exemplo, por favor, pois não consigo visualizar, por conta do que citei sobre validações ou possíveis validações futuras)?
Por que as bibliotecas escrita em PHP (e as "pessoas") gostam mais de usar mutator/accessor do que em definir uma propriedade pública para uma classe e defini-la "externamente"?
Comment: O que achas dessas? http://pt.stackoverflow.com/q/44739/101, http://pt.stackoverflow.com/q/43757/101, http://pt.stackoverflow.com/q/25995/101, http://pt.stackoverflow.com/q/33723/101,
Comment: Obrigado Mr @bigown. Agora percebo que a maioria não chama de "mutator/accessor". Pode fechar ;)
Comment: Ou melhor, não fecha ainda. Essa aqui tem citações sobre desempenho, talvez alguém tenha algo a dizer.
Answer: Getters e Setters não foram criados em Java, mas foram popularizados por ela, o uso obrigatório deles faz parte da cultura de hiper abstração da linguagem onde você nunca pode escrever algo que apenas funcione, tem que ter vários níveis de abstração, vários métodos que não fazem nada além de inflar a quantidade de linhas de código para que você possa sentir que está programando como um rock star enquanto digita furiosamente aqueles 10 getters e setters, que sua IDE gera automaticamente mas você gosta de fazer na mão para deixar transeuntes boquiabertos com suas habilidades de codificação .
Zoeiras a parte o motivo do uso você já conhece, você cria métodos de acesso para a propriedade para que caso queira fazer algum processamento antes de recuperar elas (getter) e antes de armazenar-las (setter), isso é definitivamente muito útil...quando faz sentido. O que ocorre na prática é você ver várias classes com dezenas de getters/setters que não fazem nada e nunca vão fazer, afinal talvez você nunca precise processar o atributo na hora de recuperar ou de setar ou ainda isso pode ser tranquilamente feito no construtor, então no fim isso é um padrão que a maioria usa sem pensar nem contestar, não que seja necessariamente ruim.
Quanto a performance existe sim um overhead afinal você troca o acesso direto do atributo pela chamada de um método que faz o acesso, ainda pior no caso de magic methods já que você acaba lidando com os atributos por reflexão. Porém você vai ter que se esforçar muito para isso ter um efeito negativo palpável na sua aplicação, na esmagadora maioria dos casos você deveria estar mais preocupado com outras coisas como o cachear o acesso ao banco ao invés de ficar olhando em micro-otimizações de gettter/setter versus acesso direto ao atributo.
No fim só posso dizer que o uso é questão pessoal, você pode ter uma longa vida feliz como dev (dev+feliz=runtimeerror) sem nunca usar um getter/setter, mas sempre tem casos em que usar eles facilitaria a sua vida.
Comment: +1 pelo "um padrão que a maioria usa sem pensar nem contestar".
Answer: O principal motivo para usar métodos getter e setter é ter algo além de um simples acesso e atribuição na propriedade.
Então se não tiver um processamento, não deve usar? Não é isso, pode ser interessante usar sim, e aí responde a última questão. É a velha resposta da abstração. Quando faz isto fica garantido que pode trocar a implementação do acesso/atribuição quando quiser. Pode por ou tirar processamento, afinal o acesso sempre será feito pelo método. Se deixar o acesso direto pela variável, não pode trocar isto sem afetar o código que consomem a propriedade.
Afinal não existe problema na computação que não possa ser resolvido com um nível extra de indireção. (O que é indireção?)
Também é interessante para o caso citado da interface. Aliás esse é o motivo de existir interface, abstrair a implementação.
Então não é só para imitar o Java, se vai imitar código do Java, mas pode não usar nada disto se preferir escrever código normal de PHP.
Precisar usar? Não precisa, ele pode facilitar a manutenção futura. Se tiver um caso onde seja certo que a implementação não pode ser trocada, não precisa usar nunca.
Certamente há um custo maior em chamar um método, mas já sabe, em PHP a diferença é pequena. Tem que medir para ver se está atrapalhando.
Aconselho ler Métodos Getters e Setters e todos os links constantes lá antes de começar usar esse recurso.
Coloquei no GitHub para referência futura.
Comment: O único caso onde vi um framework deixando uma propriedade como `public` foi no laravel, que é uma propriedade que recebe `true` ou `false`. Aí faz sentido não ter que usar método (apesar de alguns que fariam questão de criar um setter para boolean) :D
Comment: Não sei. Depende. Pode ser gambi. Não tenho como saber. Só perguntando para quem fez.


No voice while making external calls - NAT configuration - Asterisk 1.8.13

Question: As we were trying to setup our Asterisk server, we went on a huge problem: The inability to make call to or from external devices connected to the server. In fact, I can dial and answer the call on both side, but I can't hear anything.
I've browsed hours of content on the Internet, looking for tutorials on how to correctly setup the NAT with sip.conf, but without any effective results.
Here's our sip.conf
<code>[general]
bindaddr=0.0.0.0
localnet=192.168.1.0/255.255.255.0
externhost=mydynhost.dyndns.org
externrefresh=10
nat=yes
register => username:****@montreal.voip.ms:5060
registertimeout=15
registerattempts=0

[voipms]
canreinvite=no
context=voipms
host=montreal.voip.ms
secret=****
type=peer
defaultuser=username
disallow=all
allow=ulaw
fromuser=username
trustrpid=yes
sendrpid=yes
insecure=invite
nat=yes

[1000]
type=friend
context=phones
host=dynamic
defaultuser=1000
secret=****

[1001]
type=friend
context=phones
host=dynamic
defaultuser=1001
secret=****
</code>
extensions.conf
<code>[phones]
include => internal
include => voipms

[internal]
exten => _1XXX,1,Answer()
exten => _1XXX,n,Dial(SIP/${EXTEN})
exten => _1XXX,n,Hangup()

[voipms]
include => voipms-inbound
include => voipms-outbound

[voipms-outbound]
exten => _1NXXNXXXXXX,1,Dial(SIP/${EXTEN}@voipms)
exten => _1NXXNXXXXXX,n,Hangup()
exten => _NXXNXXXXXX,1,Dial(SIP/1${EXTEN}@voipms)
exten => _NXXNXXXXXX,n,Hangup()
exten => _011.,1,Dial(SIP/${EXTEN}@voipms)
exten => _011.,n,Hangup()
exten => _00.,1,Dial(SIP/${EXTEN}@voipms)
exten => _00.,n,Hangup()

[voipms-inbound]
exten => somenumber,1,Answer()
</code>
In rtp.conf the following values are set:

rtpstart=10001
rtpend=20000

As you can see, we don't have a static IP address and we are using a dynamic DNS service to refresh it when it changes.
My question is simple, how can we make our current setup working with other devices not in our private network? Note that I only want to contact people using a 4 digit extension number. These extensions will allow "local" users to call themselves without using the VoIP.ms service. I'll test external calls after when the NAT will be working.
If you need more information, please just ask!
Thanks!
Dave
EDIT: Also, if it can help. I'm testing my calls with X-Lite (http://www.counterpath.com/x-lite-download.html) and my Android 4.2.2 device using the embedded SIP client available in the Phone.apk application.
Comment: I am not an telcom person. But... I don't see "allow" on your phones in sip.conf. Have you tried adding allow=ulaw on 1000 and 1001?
Comment: Your hosts are on the inside, correct? You have a dynamic hostname on the outside, so that should mean you have a public IP and don't need NAT translation (as you're actually not behind NAT)..
Comment: @ Edwin: It seems like the "allow" keyword authorize the use of specific codecs. http://www.voip-info.org/wiki/view/Asterisk+config+sip.conf.
@ NickW: Not all my clients are on the "inside". Our Asterisk server is setup in a private network ranged at 192.168.1.0/24. All our Internet activities are redirected by a router. Port forwarding were done. I've opened 5060 UDP and 10001-20000 UDP to the appropriate server hosting Asterisk.
Comment: I've had trouble with no audio over the outside trunk before, and specifying the codec has worked for me.  It's worth a shot.
Comment: @Edwin: I tried it once again, but without positive results. I've noted that codecs seems to work correctly when making calls behind my NAT. Without the disallow=all and allow=ulaw, internal calls are working flawlessly.
Answer: After asking directly to the community of forums.asterisk.org, the member david55 help me out to find my solution.
Click here to access it
Answer: This works for me, http://community.freepbx.org/t/i-am-unable-to-hear-callers-voice-but-the-caller-able-to-hear-my-voice/11994/3
I set localnet and externaddr param in sip.conf and works perfecly
Comment: please, add more information beyond the link.