Number of Automorphisms of a given Graph

Question: Given a small graph, how can you manually calculate the number of automorphisms? I thought of seeing the number of nodes of a particular degree and permuting among them, but aren't there other factors to consider, like the node being part of a cycle, etc? For example, this graph:

Suppose b had two more edges going to two different nodes, then you can't map b to g, even though they have the same degree. So how do you do this?
Comment: A long time ago I wrote some code in SML to calculate graph automorphisms. If you like I can try to find it and send it to you.
Comment: Isn't this an NP problem? Of what I know, there is a polynomial solution only if the vertex degrees are limited.
Comment: It is NP, but not known to be NP-complete. See http://math.stackexchange.com/questions/190532/what-is-the-significance-of-the-graph-isomorphism-problem/190675#190675
Comment: Ah ... thanks for the link
Answer: It is in general not even simple to check if two graphs are isomorphic.
In your case, we see that $a,b,m$ are the only degree 1 nodes, hence any automorphism must permute them. Next $c$ is characterized by being the only node with degree 1 neighbours, hence $c$ must be a fixed point (which at the same instant shows that $a,b,m$ may be permuted arbitrarily).
You may then note that $d$ can be mapped to any of $d,e,f,g$, but then $e$ must be mapped to the common neighbour of $\phi(d)$ and $\phi(c)=c$.
Finally you can map $f$ to any of the remaining neighbours of $c$ and the rest, i.e. the images of $g, h, k$ are then determined.
We conclude that the automorphism gorup is isomorphic to $S_3\times D_4$.


Pass a MemoryStream object to a FileStream object

Question: I have a MemoryStream object, and i want to write it to a file and pass it as a parameter in the following passFile method. The passFile method currently accepts a FileStream object as a parameter, but is there a way where i could convert the MemoryStream object to a FileStream object? Help
MemoryStream ms = new MemoryStream();

public void passFile (FileStream file){

}

Answer: No, you cannot cast it: MemoryStream and FileStream are siblings in the inheritance hierarchy, they cannot be cast to one another.
If the method cannot be re-written to take a Stream, you could write the content of the MemoryStream into a temporary file, and then open a FileStream based on the content of that file.
Answer: You could use a Stream abstraction, and pass any type of stream as FileStream, MemoryStream, etc...  for sample:
public void passFile(Stream stream)
{
   // process stream
}

Comment: Thanks for your reply. How do i then convert a Stream to a File ? i am looking at the link @dasblinkenlight has given, and still can't figure out what is the `File` object so i could use it in the method.
Comment: Look at the dasblinkenlight link: http://stackoverflow.com/questions/8624071/save-and-load-memorystream-to-from-a-file/8624188#8624188 ... there is a way to convert between them.


criar um arquivo com o conteúdo em C# e salvar em uma pasta no computador

Question: Preciso criar um relatório e salvar esse relatório em uma pasta dentro do disco C: do computador, estou só conseguindo salvar o arquivo sem o conteúdo e com o nome em branco.
O código de salvar:
string folder = System.Configuration.ConfigurationManager.AppSettings["Relatorios"] + usuario;
string arquivo = folder + @"\" + nomearquivo;

if (!Directory.Exists(folder))
{
    Directory.CreateDirectory(arquivo + ".txt " + relatorio);
}

if (!File.Exists(arquivo))
{
    File.Create(arquivo+".txt " + relatorio);
}
else
{
    File.Delete(arquivo);
    File.Create(arquivo + ".txt " + relatorio);
}

Comment: Pode ser um exemplo com qualquer conteúdo?
Comment: pode ser sim...
Comment: Douglas, alguma das respostas solucionou seu problema?
Comment: Foi sim, so nao consigo salvar com o relatorio
Comment: Ok, então você pode marcar a resposta que solucionou o seu problema como correta usando o **✓** do lado esquerdo dela, certo? Sobre o novo problema, você pode abrir uma nova pergunta a qualquer momento se estiver precisando de ajuda.
Comment: muito bom, o problema é que o arquivo não atualiza conforme você adiciona caracteres
Answer: Tem vários probleminhas aí. Dei uma melhorada no seu código num geral.

Use Path.Combine() ao invés de concatenar as strings com a barra invertida (isto é mais uma dica).

Não precisa verificar se o diretório existe antes de Directory.CreateDirectory() por que este método só cria o arquivo se ele não existir.

Não é necessário excluir o arquivo para recriá-lo. O método File.Create() cria um arquivo se este não existir, caso o arquivo com este nome já existia, ele será sobrescrito.

Cuidado com streams abertos.
File.Create() vai retornar um FileStream, este FileStream não está sendo fechado e você precisa tomar cuidado com isto. Por isso eu coloquei Dispose() depois do Create().
Leia sobre isto em: Qual a utilidade do using?, Quais tipos de recursos são liberados em uma declaração "using"?, Devo sempre utilizar Dispose?

Faltou escrever o conteúdo do arquivo. O método Create() recebe apenas o nome do arquivo, você está concatenando o que deveria ser o conteúdo junto com o nome.
No meu exemplo, usei WriteAllLines() para escrevê-lo. Ele recebe como parâmetro um IEnumerable<string> e faz com que cada elemento desta coleção seja uma linha do arquivo. Usei este método porque é muito mais simples e não corre o risco de vazamento de recursos.

O código:
Supondo que relatorio seja uma coleção de string (array, lista, etc.).
List<string> relatorio = /*Fazer algo para carregar o relatório*/;

string folder = System.Configuration.ConfigurationManager.AppSettings["Relatorios"] + usuario;;
string arquivo = Path.Combine(folder, "arquivo.txt");

Directory.CreateDirectory(folder);

File.Create(arquivo).Dispose();

File.WriteAllLines(arquivo, relatorio);

Comment: sua resposta me ajudou numa duvida minha, porem dessa forma o arquivo é criado dentro do servidor, teria como criar na maquina local do cliente ?
Comment: @Desalex Numa aplicação web é impossível. O que você pode fazer é gerar um arquivo no servidor e devolver pro cliente para que o usuário faça download.
Comment: como esse cara fez nesse exemplo desse video ai -> https://www.youtube.com/watch?v=NJRiGGgDEpE eu preciso disso, "fazer o download do arquivo criado" e nao estou conseguindo, fiz igual ele e o meu nao baixa nada, nao acontece nada.
Comment: @Desalex Abre uma pergunta então, cara.
Comment: ja ta aberta, é que ngm respondeu, porem preciso muito disso :(
Comment: @Desalex Procurei no seu perfil e não encontrei, se mandar o link eu tento responder...
Answer: https://msdn.microsoft.com/pt-br/library/system.io.file.createtext(v=vs.110).aspx
 string path = @"c:\temp\MyTest.txt";
    if (!File.Exists(path))
    {
        // Create a file to write to.
        using (StreamWriter sw = File.CreateText(path))
        {
            sw.WriteLine("Hello");
            sw.WriteLine("And");
            sw.WriteLine("Welcome");
        }
    }


derivation of Support Vector Machine

Question: I was watching Andrew Ng's machine learning lecture on SVM. There is one line that puzzles me.
$$x^{(i)} - \gamma^{(i)} \frac{w}{||w||}$$
I dont understand how can the line above give the x-coordinate of point B on the decision boundary. Please can someone explain the linear algebra of this line of equation.
Answer: This equation does not give you "x coordinate of point B", it gives you point $B$
Lets start from the begining
Your have a point $A=:x^{(i)}$. You take normal $w$, which is a vector perpendicular to the decision boundary, you normalize it $\frac{w}{\left | w \right |}$ and get a unit vector in the perpendicular direction (which is parallel to $AB$). As shown on the image,$w$ is directed towards $A$, so $-\frac{w}{\left | w \right |}$ is a unit vector in the opposite direction. Now, if you multiply $-\frac{w}{\left | w \right |}$ and a distance $\gamma^{(i)}$ you get a vector of length  $\gamma^{(i)}$ directed in the opposite direction then $A$ (from $w$). Once you add $x^{(i)}$ and $-\gamma^{(i)}\frac{w}{\left | w \right |}$ you get a point, which is placed in distance $\gamma^{(i)}$ from $A$ towards $w$, which is $A$'s projection on $w$ - labeled with $B$ on the image.

derivation of Support Vector Machine


How do I play: do re mi fa sol la si, on my soprano recorder?

Question: I am confused about there being different lowest notes for different sizes of recorder.
My soprano recorder says descant B.  It is an average sized recorder (Yamaha YRS-24B).
What will the fingering be like if I were to play: do re mi fa sol la si, on it as opposed to on a bigger recorder.
Comment: read this thread and it might help you https://music.stackexchange.com/questions/5374/what-is-a-transposing-instrument
Comment: unless you are using fixed-do solfège there is no difference in fingerings
Comment: `do re mi fa so la ti` is solfege. In some cultures do always refers to C. If that is not what you mean then you should be able to use the same fingering charts for all recorders.
Comment: sure, but solflege isn't really used much.
Comment: what does solfege mean ? forgive me I am a beginner
Comment: Search for a fingering table; most likely one accompanied to recorder.
Comment: @Legorhin So I can play the solfege using the same fingering on recorders on of any size as long as the music playing is transposed or is in solfege format....is this right ??
Comment: @Legorhin you can answer. So I can approve
Comment: OP proably using doremi as fixed do the latin equivalent to cdefg. (si=ti)
Answer: The "B" stands for baroque, which is the fingering system the instrument uses.  The baroque system is the most common fingering system for the recorder, as opposed to the slightly simpler (but generally not as good overall) German system—see the video in this answer:  Is it difficult to transition from German fingering to Baroque fingering?

To play the C major scale (the easiest scale) on a soprano/descant recorder with baroque fingering is relatively straight forward:

There is only one note that has a somewhat odd fingering in this scale and which requires a "forked" fingering.  (The German system makes this particular note easier to finger, but to do this compromises have been made in the design that mess up intonation and various chromatic notes.)

For the larger alto/treble recorder, this same fingering pattern produces an F major scale:

To be able to play both alto and soprano recorders requires learning the fingering for each size of instrument separately.  This can be confusing at first, but (with practice) it's not as hard as you might think.
Comment: @nuggethead
music the smaller and larger recorders is written either
transposed down or up by octaves (or with octave clefs)
if you count that as transposing, but otherwise not transposed.
So you do have learn different fingerings for the C and F instruments.
Comment: I had always thought alto recorders were played as a transposing instrument.
Comment: @nuggethead traditionally recorder parts are written at concert pitch.  See https://en.wikipedia.org/wiki/Alto_recorder.  I expect that this is because they were in use during the Renaissance when most consort music was published without necessarily specifying what instruments were playing.
Comment: Interesting!  I never knew that
Answer: I don't believe that there are B-recorders. Your recorder is certainly down tuned almost a semitone about 435 or 430. So you think it is tuned in B.  Try to push the mouth piece with the rest of the flute.
If you want to play together with other Baroque instruments (recorders or harpsichord) you can just play the C scale as doremi, as they all have lower pitch) but together with a Piano you will always have the problem that you are too low.
Comment: I think Yamaha  24B means B = barock edition with 2 holes for C and D, so you can play Db and Eb. The letter B stands probably for baroque or B edition as Yamaha also sells a recorder with only one hole for C and D, the German fingerings. Could be 24A ;). Mind that the fingering of F is using the index and ring and pinky finger, only the middle finger is open (right hand)
Comment: that's what it said on the recorder B descant.
Comment: The YRS-24B is the Baroque system model, as opposed to the YRS-23; the German system model.
Comment: ... Both of these models are A=440Hz.
Comment: @AlbrechtHügli Recorders have 2 different fingerings Baroque and German and I think the name coding for Yahama is YRS = Yamaha Recorder Series [Number] and the B = Baroque (fingering) "blank"=German. Though contrary to popular believe double holes for c/d (soprano/tenor) or f/g (on alto/bass) exist for both Baroque and German fingering (though for Baroque they are standard for German fingering they are optional) the distinguishing feature is that the size of the 4/5 hole. For Baroque it's 4th small 5th big for German it's 4th big 5th small. Also there are recorders in weird keys ;)
Comment: My bad the S is for Soprano not series as there are also YRA YRT and so on for alto and tenor.


Is git pull really same as git fetch+merge?

Question: I saw so many questions here and on the web which say that git pull is the same thing is doing git fetch and then merging. I was pulling something at work this other day using git pull and it kept saying to me that my local branch is already up to date although there were changes made by other developers. However, when I did a git fetch and then did a git pull, I was able to pull the changes. If git pull already does a git fetch, why did just doing a git pull not work?
I would appreciate the response.
Comment: This doesn't seem reproducible to me.  A `git pull` should have updated whatever branch you were on.  Can you show us exactly what you were doing?
Answer: The short answer is "yes", but the long answer is only mostly "yes".
If your Git version is older than 2.6, your git pull is a simple—well, OK, maybe not so simple, weighing in at over 300 lines—shell script that runs this line:
git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1

The $@ here encompasses most of the arguments people usually type into git pull:
git pull origin master

for instance would run:
git fetch origin master

(A few options are removed from $@ to be passed to the rebase or merge command if appropriate.)
Since Git version 2.6.0, git pull is now coded in C, but it still invokes the same git fetch operation—it's just faster on Windows as it no longer has to get a shell involved.
The part where the answer is only mostly "yes" is that after fetching, the remaining part of git pull is not necessarily git merge.  It can be git rebase, or even in a few corner cases, git read-tree.  For details, see the rest of the script.
Note that if you have more than one remote, and you run git fetch with no additional arguments, Git will use the current branch's configured remote if there is one, or origin if there is no configured remote.  When you run git pull remote branch-name, you are supplying a named remote, which will override the current branch's configured remote (if any).  So that can be a source of differences: if the current branch has no upstream set, git fetch will fetch from origin, but git pull otherremote foo will fetch from remote otherremote.


Windows shortcut (.lnk) parser in Java?

Question: I'm currently using Win32ShellFolderManager2 and ShellFolder.getLinkLocation to resolve windows shortcuts in Java. Unfortunately, if the Java program is running as a service under Vista, getLinkLocation, this does not work. Specifically, I get an exception stating "Could not get shell folder ID list".
Searching the web does turn up mentions of this error message, but always in connection with JFileChooser. I'm not using JFileChooser, I just need to resolve a .lnk file to its destination.
Does anyone know of a 3rd-party parser for .lnk files written in Java I could use?
I've since found unofficial documentation for the .lnk format here, but I'd rather not have to do the work if anyone has done it before, since the format is rather scary.
Answer: Added comments (some explanation as well as credit to each contributor so far),additional check on the file magic, a quick test to see if a given file might be a valid link (without reading all of the bytes), a fix to throw a ParseException with appropriate message instead of ArrayIndexOutOfBoundsException if the file is too small, did some general clean-up.
Source here (if you have any changes, push them right to the GitHub repo/project.
package org.stackoverflowusers.file;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;

/**
 * Represents a Windows shortcut (typically visible to Java only as a '.lnk' file).
 *
 * Retrieved 2011-09-23 from http://stackoverflow.com/questions/309495/windows-shortcut-lnk-parser-in-java/672775#672775
 * Originally called LnkParser
 *
 * Written by: (the stack overflow users, obviously!)
 *   Apache Commons VFS dependency removed by crysxd (why were we using that!?) https://github.com/crysxd
 *   Headerified, refactored and commented by Code Bling http://stackoverflow.com/users/675721/code-bling
 *   Network file support added by Stefan Cordes http://stackoverflow.com/users/81330/stefan-cordes
 *   Adapted by Sam Brightman http://stackoverflow.com/users/2492/sam-brightman
 *   Based on information in 'The Windows Shortcut File Format' by Jesse Hager <email@example.com>
 *   And somewhat based on code from the book 'Swing Hacks: Tips and Tools for Killer GUIs'
 *     by Joshua Marinacci and Chris Adamson
 *     ISBN: 0-596-00907-0
 *     http://www.oreilly.com/catalog/swinghks/
 */
public class WindowsShortcut
{
    private boolean isDirectory;
    private boolean isLocal;
    private String real_file;

    /**
     * Provides a quick test to see if this could be a valid link !
     * If you try to instantiate a new WindowShortcut and the link is not valid,
     * Exceptions may be thrown and Exceptions are extremely slow to generate,
     * therefore any code needing to loop through several files should first check this.
     *
     * @param file the potential link
     * @return true if may be a link, false otherwise
     * @throws IOException if an IOException is thrown while reading from the file
     */
    public static boolean isPotentialValidLink(File file) throws IOException {
        final int minimum_length = 0x64;
        InputStream fis = new FileInputStream(file);
        boolean isPotentiallyValid = false;
        try {
            isPotentiallyValid = file.isFile()
                && file.getName().toLowerCase().endsWith(".lnk")
                && fis.available() >= minimum_length
                && isMagicPresent(getBytes(fis, 32));
        } finally {
            fis.close();
        }
        return isPotentiallyValid;
    }

    public WindowsShortcut(File file) throws IOException, ParseException {
        InputStream in = new FileInputStream(file);
        try {
            parseLink(getBytes(in));
        } finally {
            in.close();
        }
    }

    /**
     * @return the name of the filesystem object pointed to by this shortcut
     */
    public String getRealFilename() {
        return real_file;
    }

    /**
     * Tests if the shortcut points to a local resource.
     * @return true if the 'local' bit is set in this shortcut, false otherwise
     */
    public boolean isLocal() {
        return isLocal;
    }

    /**
     * Tests if the shortcut points to a directory.
     * @return true if the 'directory' bit is set in this shortcut, false otherwise
     */
    public boolean isDirectory() {
        return isDirectory;
    }

    /**
     * Gets all the bytes from an InputStream
     * @param in the InputStream from which to read bytes
     * @return array of all the bytes contained in 'in'
     * @throws IOException if an IOException is encountered while reading the data from the InputStream
     */
    private static byte[] getBytes(InputStream in) throws IOException {
        return getBytes(in, null);
    }

    /**
     * Gets up to max bytes from an InputStream
     * @param in the InputStream from which to read bytes
     * @param max maximum number of bytes to read
     * @return array of all the bytes contained in 'in'
     * @throws IOException if an IOException is encountered while reading the data from the InputStream
     */
    private static byte[] getBytes(InputStream in, Integer max) throws IOException {
        // read the entire file into a byte buffer
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        byte[] buff = new byte[256];
        while (max == null || max > 0) {
            int n = in.read(buff);
            if (n == -1) {
                break;
            }
            bout.write(buff, 0, n);
            if (max != null)
                max -= n;
        }
        in.close();
        return bout.toByteArray();
    }

    private static boolean isMagicPresent(byte[] link) {
        final int magic = 0x0000004C;
        final int magic_offset = 0x00;
        return link.length >= 32 && bytesToDword(link, magic_offset) == magic;
    }

    /**
     * Gobbles up link data by parsing it and storing info in member fields
     * @param link all the bytes from the .lnk file
     */
    private void parseLink(byte[] link) throws ParseException {
        try {
            if (!isMagicPresent(link))
                throw new ParseException("Invalid shortcut; magic is missing", 0);

            // get the flags byte
            byte flags = link[0x14];

            // get the file attributes byte
            final int file_atts_offset = 0x18;
            byte file_atts = link[file_atts_offset];
            byte is_dir_mask = (byte)0x10;
            if ((file_atts & is_dir_mask) > 0) {
                isDirectory = true;
            } else {
                isDirectory = false;
            }

            // if the shell settings are present, skip them
            final int shell_offset = 0x4c;
            final byte has_shell_mask = (byte)0x01;
            int shell_len = 0;
            if ((flags & has_shell_mask) > 0) {
                // the plus 2 accounts for the length marker itself
                shell_len = bytesToWord(link, shell_offset) + 2;
            }

            // get to the file settings
            int file_start = 0x4c + shell_len;

            final int file_location_info_flag_offset_offset = 0x08;
            int file_location_info_flag = link[file_start + file_location_info_flag_offset_offset];
            isLocal = (file_location_info_flag & 2) == 0;
            // get the local volume and local system values
            //final int localVolumeTable_offset_offset = 0x0C;
            final int basename_offset_offset = 0x10;
            final int networkVolumeTable_offset_offset = 0x14;
            final int finalname_offset_offset = 0x18;
            int finalname_offset = link[file_start + finalname_offset_offset] + file_start;
            String finalname = getNullDelimitedString(link, finalname_offset);
            if (isLocal) {
                int basename_offset = link[file_start + basename_offset_offset] + file_start;
                String basename = getNullDelimitedString(link, basename_offset);
                real_file = basename + finalname;
            } else {
                int networkVolumeTable_offset = link[file_start + networkVolumeTable_offset_offset] + file_start;
                int shareName_offset_offset = 0x08;
                int shareName_offset = link[networkVolumeTable_offset + shareName_offset_offset]
                    + networkVolumeTable_offset;
                String shareName = getNullDelimitedString(link, shareName_offset);
                real_file = shareName + "\\" + finalname;
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new ParseException("Could not be parsed, probably not a valid WindowsShortcut", 0);
        }
    }

    private static String getNullDelimitedString(byte[] bytes, int off) {
        int len = 0;
        // count bytes until the null character (0)
        while (true) {
            if (bytes[off + len] == 0) {
                break;
            }
            len++;
        }
        return new String(bytes, off, len);
    }

    /*
     * convert two bytes into a short note, this is little endian because it's
     * for an Intel only OS.
     */
    private static int bytesToWord(byte[] bytes, int off) {
        return ((bytes[off + 1] & 0xff) << 8) | (bytes[off] & 0xff);
    }

    private static int bytesToDword(byte[] bytes, int off) {
        return (bytesToWord(bytes, off + 2) << 16) | bytesToWord(bytes, off);
    }

}

Comment: Been using this for a couple of months now, figured I should share.
Comment: How about creating shortcuts? And icons?
Comment: @TomášZato that would be really great to add in
Comment: @TomášZato did you end up finding anything to create shortcuts, or modify the icons? If so, please share!
Answer: Sam Brightman's solution is for local files only.
I added support for Network files:

Windows shortcut (.lnk) parser in Java?
http://code.google.com/p/8bits/downloads/detail?name=The_Windows_Shortcut_File_Format.pdf
http://www.javafaq.nu/java-example-code-468.html
public class LnkParser {

public LnkParser(File f) throws IOException {
    parse(f);
}

private boolean isDirectory;
private boolean isLocal;

public boolean isDirectory() {
    return isDirectory;
}

private String real_file;

public String getRealFilename() {
    return real_file;
}

private void parse(File f) throws IOException {
    // read the entire file into a byte buffer
    FileInputStream fin = new FileInputStream(f);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    byte[] buff = new byte[256];
    while (true) {
        int n = fin.read(buff);
        if (n == -1) {
            break;
        }
        bout.write(buff, 0, n);
    }
    fin.close();
    byte[] link = bout.toByteArray();

    parseLink(link);
}

private void parseLink(byte[] link) {
    // get the flags byte
    byte flags = link[0x14];

    // get the file attributes byte
    final int file_atts_offset = 0x18;
    byte file_atts = link[file_atts_offset];
    byte is_dir_mask = (byte)0x10;
    if ((file_atts & is_dir_mask) > 0) {
        isDirectory = true;
    } else {
        isDirectory = false;
    }

    // if the shell settings are present, skip them
    final int shell_offset = 0x4c;
    final byte has_shell_mask = (byte)0x01;
    int shell_len = 0;
    if ((flags & has_shell_mask) > 0) {
        // the plus 2 accounts for the length marker itself
        shell_len = bytes2short(link, shell_offset) + 2;
    }

    // get to the file settings
    int file_start = 0x4c + shell_len;

    final int file_location_info_flag_offset_offset = 0x08;
    int file_location_info_flag = link[file_start + file_location_info_flag_offset_offset];
    isLocal = (file_location_info_flag & 2) == 0;
    // get the local volume and local system values
    //final int localVolumeTable_offset_offset = 0x0C;
    final int basename_offset_offset = 0x10;
    final int networkVolumeTable_offset_offset = 0x14;
    final int finalname_offset_offset = 0x18;
    int finalname_offset = link[file_start + finalname_offset_offset] + file_start;
    String finalname = getNullDelimitedString(link, finalname_offset);
    if (isLocal) {
        int basename_offset = link[file_start + basename_offset_offset] + file_start;
        String basename = getNullDelimitedString(link, basename_offset);
        real_file = basename + finalname;
    } else {
        int networkVolumeTable_offset = link[file_start + networkVolumeTable_offset_offset] + file_start;
        int shareName_offset_offset = 0x08;
        int shareName_offset = link[networkVolumeTable_offset + shareName_offset_offset]
                + networkVolumeTable_offset;
        String shareName = getNullDelimitedString(link, shareName_offset);
        real_file = shareName + "\\" + finalname;
    }
}

private static String getNullDelimitedString(byte[] bytes, int off) {
    int len = 0;
    // count bytes until the null character (0)
    while (true) {
        if (bytes[off + len] == 0) {
            break;
        }
        len++;
    }
    return new String(bytes, off, len);
}

/*
 * convert two bytes into a short note, this is little endian because it's
 * for an Intel only OS.
 */
private static int bytes2short(byte[] bytes, int off) {
    return ((bytes[off + 1] & 0xff) << 8) | (bytes[off] & 0xff);
}

/**
 * Returns the value of the instance variable 'isLocal'.
 *
 * @return Returns the isLocal.
 */
public boolean isLocal() {
    return isLocal;
}
}

Comment: thanks for the code, I made of it a static method taking a link path as string and returning path to destination file as string.
Answer:
I can recommend this repository on GitHub:
https://github.com/BlackOverlord666/mslinks
There I've found a simple solution to create shortcuts:
ShellLink.createLink("path/to/existing/file.txt", "path/to/the/future/shortcut.lnk");
If you want to read shortcuts:
File shortcut = ...;
String pathToExistingFile = new ShellLink(shortcut).resolveTarget();

If you want to change the icon of the shortcut, use:
ShellLink sl = ...;
sl.setIconLocation("/path/to/icon/file");

You can edit most properties of the shortcutlink such as working directory, tooltip text, icon, command line arguments, hotkeys, create links to LAN shared files and directories and much more...
Hope this helps you :)
Kind regards
Josua Frank
Comment: This is perfect !!
Comment: @Codebling 's code does work for many usecases and I've been using it for a long time, but it has flaws (in my case the target's name was in Cyrillic, and IMO the code doesn't fetch the target from the right place).
Now this mslinks code is the way to go!
Answer: The code plan9assembler linked to appears to work with minor modification. I think it's just the "& 0xff" to prevent sign extension when bytes are upcast to ints in the bytes2short function that need changing. I've added the functionality described in http://www.i2s-lab.com/Papers/The_Windows_Shortcut_File_Format.pdf to concatenate the "final part of the pathname" even though in practice this doesn't seem to be used in my examples. I've not added any error checking to the header or dealt with network shares. Here's what I'm using now:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class LnkParser {

    public LnkParser(File f) throws Exception {
        parse(f);
    }

    private boolean is_dir;

    public boolean isDirectory() {
        return is_dir;
    }

    private String real_file;

    public String getRealFilename() {
        return real_file;
    }

    private void parse(File f) throws Exception {
        // read the entire file into a byte buffer
        FileInputStream fin = new FileInputStream(f);
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        byte[] buff = new byte[256];
        while (true) {
            int n = fin.read(buff);
            if (n == -1) {
                break;
            }
            bout.write(buff, 0, n);
        }
        fin.close();
        byte[] link = bout.toByteArray();

        // get the flags byte
        byte flags = link[0x14];

        // get the file attributes byte
        final int file_atts_offset = 0x18;
        byte file_atts = link[file_atts_offset];
        byte is_dir_mask = (byte) 0x10;
        if ((file_atts & is_dir_mask) > 0) {
            is_dir = true;
        } else {
            is_dir = false;
        }

        // if the shell settings are present, skip them
        final int shell_offset = 0x4c;
        final byte has_shell_mask = (byte) 0x01;
        int shell_len = 0;
        if ((flags & has_shell_mask) > 0) {
            // the plus 2 accounts for the length marker itself
            shell_len = bytes2short(link, shell_offset) + 2;
        }

        // get to the file settings
        int file_start = 0x4c + shell_len;

        // get the local volume and local system values
        final int basename_offset_offset = 0x10;
        final int finalname_offset_offset = 0x18;
        int basename_offset = link[file_start + basename_offset_offset]
                                + file_start;
        int finalname_offset = link[file_start + finalname_offset_offset]
                                + file_start;
        String basename = getNullDelimitedString(link, basename_offset);
        String finalname = getNullDelimitedString(link, finalname_offset);
        real_file = basename + finalname;
    }

    private static String getNullDelimitedString(byte[] bytes, int off) {
        int len = 0;
        // count bytes until the null character (0)
        while (true) {
            if (bytes[off + len] == 0) {
                break;
            }
            len++;
        }
        return new String(bytes, off, len);
    }

    /*
     * convert two bytes into a short note, this is little endian because it's
     * for an Intel only OS.
     */
    private static int bytes2short(byte[] bytes, int off) {
        return ((bytes[off + 1] & 0xff) << 8) | (bytes[off] & 0xff);
    }
}

Answer: I've also worked( now have no time for that) on '.lnk' in Java. My code is here
It's little messy( some testing trash) but local and network parsing works good. Creating links is implemented too. Please test and send me patches.
Parsing example:
Shortcut scut = Shortcut.loadShortcut(new File("C:\\t.lnk"));
System.out.println(scut.toString());

Creating new link:
Shortcut scut = new Shortcut(new File("C:\\temp"));
OutputStream os = new FileOutputStream("C:\\t.lnk");
os.write(scut.getBytes());
os.flush();
os.close();

Answer: The solution of @Code Bling does not work for me for Files in the User directory.
For Example "C:/Users/Username/Filename.txt".
The reason for that is: in The_Windows_Shortcut_File_Format.pdf
that was mentioned by @Stefan Cordes on page 6 it says that only the first 2 bits are important for volumes info.
All other bits might be filled with random garbage when the first bit of volumes info is "0".
So if it comes to:
isLocal = (file_location_info_flag & 2) == 0;

then file_location_info_flag might be "3".
This file is still local but this line of code assigns false to isLocal.
So i suggest the following adjustment to @Code Bling's code:
isLocal = (file_location_info_flag & 1) == 1;

Comment: I never received a notification for this, because `@User` doesn't work in answers. I understand what you mean. The orignal code assumed that a file was either local or network (not both), but it seems that these are not mutually exclusive. Do you happen to have a sample `.lnk` for this?
Comment: I've made the change
Answer: This short code is really usefull...
But two fixes are needed:

the isPotentialValidLink improved not to load file if name doesn't end with ".lnk"
  public static boolean isPotentialValidLink(final File file) {
    final int minimum_length = 0x64;
    boolean isPotentiallyValid = false;
    if (file.getName().toLowerCase().endsWith(".lnk"))
        try (final InputStream fis = new FileInputStream(file)) {
            isPotentiallyValid = file.isFile() && fis.available() >= minimum_length && isMagicPresent(getBytes(fis, 32));
        } catch (Exception e) {
            // forget it
        }
    return isPotentiallyValid;
  }

the offset has to be computed with 32bits not only a byte...
 final int finalname_offset = bytesToDword(link,file_start + finalname_offset_offset) + file_start;
 final int basename_offset = bytesToDword(link,file_start + basename_offset_offset) + file_start;

Comment: If you submit a pull request on Github, I will merge your changes, otherwise I can make them when I have time!
Comment: I've just merged the `bytesToDword` changes. I did not notice that they weren't in the original PR.
Answer: I found other non-professional technique. getting my job done.
File file=new File("C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\TeamViewer.lnk");//shortcut link
            FileInputStream stream=new FileInputStream(file);
            DataInputStream st=new DataInputStream(stream);
           byte[] bytes=new byte[(int)stream.getChannel().size()];
           stream.read(bytes);
           String data=new String(bytes);
           int i1=data.indexOf("C:\\Program Files");
           int i2=data.indexOf(".exe",i1);
           System.out.println(data.substring(i1, i2+4));

Answer: The given code works well, but has a bug.  A java byte is a signed value from -128 to 127. We want an unsigned value from 0 to 255 to get the correct results.  Just change the bytes2short function as follows:
static int bytes2short(byte[] bytes, int off) {
    int low = (bytes[off]<0 ? bytes[off]+256 : bytes[off]);
    int high = (bytes[off+1]<0 ? bytes[off+1]+256 : bytes[off+1])<<8;
    return 0 | low | high;
}

Windows shortcut (.lnk) parser in Java?


Device-Tree Issues with ROCKPro64 PCIe Endpoint

Question: I am attempting to configure the PCIe x4 connection on my ROCKPro64 (RK3399 processor) development board to act as a PCIe endpoint device. My goal is to get utilize Linux's PCIe Endpoint Framework test driver in a similar manner as described in this tutorial https://docs.nxp.com/bundle/GUID-3FFCCD77-5220-414D-8664-09E6FB1B02C6/page/GUID-1B638C76-E8F9-4FF8-A140-B934DC5055F0.html.
I've been stuck on this issue for more than I would like to admit and any help would be greatly appreciated.
I am currently using a custom Armbian build which has the following build configuration: enabled PCI Endpoint drivers in the kernel, kernel v5.15.y,and Ubuntu Focal minimal command line. I am using a Device Tree Overlay to configure the PCIe endpoint.
/dts-v1/;
/plugin/;
/ {

    fragment@0 {
        target-path = "/pcie@f8000000";
        __overlay__ {
            status = "disabled";
        };
    };

    fragment@1 {
        target-path = "/";
        __overlay__ {
            pcie_ep@f8000000{
                compatible = "rockchip,rk3399-pcie-ep";
                #address-cells = <0x03>;
                #size-cells = <0x02>;
                rockchip,max-outbound-regions = <0x10>;
                clocks = <0x08 0xc5 0x08 0xc4 0x08 0x147 0x08 0xa0>;
                clock-names = "aclk\0aclk-perf\0hclk\0pm";
                max-functions = [08];
                num-lanes = <0x04>;
                reg = <0x00 0xfd000000 0x00 0x1000000 0x00 0x80000000 0x00 0x20000>;
                reg-names = "apb-base\0mem-base";
                resets = <0x08 0x82 0x08 0x83 0x08 0x84 0x08 0x85 0x08 0x86 0x08 0x81 0x08 0x80>;
                reset-names = "core\0mgmt\0mgmt-sticky\0pipe\0pm\0pclk\0aclk";
                phys = <0x15 0x00 0x15 0x01 0x15 0x02 0x15 0x03>;
                phy-names = "pcie-phy-0\0pcie-phy-1\0pcie-phy-2\0pcie-phy-3";
                pinctrl-names = "default";
                status = "okay";
                ep-gpios = <0x16 0x1c 0x00>;
            };
        };
    };
    // --- SNIP ---
};

Without pcie@f8000000 being disabled, the kernel hangs immediately when started (no output whatsoever). The board boots with this dtbo, but when the PCIe Endpoint driver is loaded, it is unable to allocate memory space for MSI interrupts.
See kernel log below:
    [    1.526603] ------------[ cut here ]------------
[    1.526630] WARNING: CPU: 4 PID: 1 at arch/arm64/mm/ioremap.c:46 __ioremap_caller+0xcc/0xf0
[    1.526676] Modules linked in:
[    1.526696] CPU: 4 PID: 1 Comm: swapper/0 Not tainted 5.15.64-rockchip64 #trunk
[    1.526720] Hardware name: Pine64 RockPro64 v2.1 (DT)
[    1.526736] pstate: 80000005 (Nzcv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    1.526759] pc : __ioremap_caller+0xcc/0xf0
[    1.526781] lr : __ioremap_caller+0x54/0xf0
[    1.526801] sp : ffff800009e0bad0
[    1.526814] x29: ffff800009e0bad0 x28: 00000000ffffffff x27: 0000000000020000
[    1.526848] x26: ffff000004135100 x25: 000000000001ffff x24: 0000000000000005
[    1.526879] x23: 0068000000000713 x22: ffff80000884b0d0 x21: 0000000000000000
[    1.526909] x20: 0000000080000000 x19: 0000000000020000 x18: 0000000000000000
[    1.526938] x17: 6f7420746c756166 x16: 6564202c79616c65 x15: 0000ad574eaa2dce
[    1.526968] x14: 000000000000035c x13: 0000000000000000 x12: 0000000000000020
[    1.526997] x11: ffff000004135180 x10: 0000000000000005 x9 : 0000000000000020
[    1.527025] x8 : 00000000f7e00000 x7 : 0000000000000018 x6 : ffff800009d5afd0
[    1.527054] x5 : ffff800009d5afd0 x4 : 0000000000000001 x3 : 0000000000000001
[    1.527082] x2 : 00000000f8000000 x1 : 0000000000000000 x0 : 0000000000000001
[    1.527111] Call trace:
[    1.527123]  __ioremap_caller+0xcc/0xf0
[    1.527145]  __ioremap+0x28/0x58
[    1.527166]  pci_epc_mem_alloc_addr+0xe0/0x168
[    1.527189]  rockchip_pcie_ep_probe+0x1a8/0x248
[    1.527209]  platform_probe+0x68/0xd8
[    1.527230]  really_probe+0xbc/0x428
[    1.527247]  __driver_probe_device+0x114/0x188
[    1.527264]  driver_probe_device+0xb0/0x110
[    1.527281]  __driver_attach+0xbc/0x1c0
[    1.527298]  bus_for_each_dev+0x74/0xc8
[    1.527320]  driver_attach+0x24/0x30
[    1.527336]  bus_add_driver+0x1a8/0x250
[    1.527357]  driver_register+0x64/0x120
[    1.527374]  __platform_driver_register+0x28/0x38
[    1.527394]  rockchip_pcie_ep_driver_init+0x1c/0x28
[    1.527418]  do_one_initcall+0x4c/0x288
[    1.527436]  kernel_init_freeable+0x228/0x2b0
[    1.527454]  kernel_init+0x24/0x128
[    1.527475]  ret_from_fork+0x10/0x20
[    1.527508] ---[ end trace d50b537f42cfef2e ]---
[    1.527530] rockchip-pcie-ep fd000000.pcie_ep: failed to reserve memory space for MSI
[    1.527570] rockchip-pcie-ep: probe of fd000000.pcie_ep failed with error -12
[    1.528218] ------------[ cut here ]------------

I am also seeing an error regarding the rockchip driver not having a release function, but I do not believe this is too critical an issue right now. Could this be causing something unforeseen?
[    1.528218] ------------[ cut here ]------------
[    1.528237] Device 'fd000000.pcie_ep' does not have a release() function, it is broken and must be fixed. See Documentation/core-api/kobject.rst.
[    1.528299] WARNING: CPU: 4 PID: 1 at drivers/base/core.c:2233 device_release+0x80/0x90
[    1.528332] Modules linked in:
[    1.528349] CPU: 4 PID: 1 Comm: swapper/0 Tainted: G        W         5.15.64-rockchip64 #trunk
[    1.528374] Hardware name: Pine64 RockPro64 v2.1 (DT)
[    1.528388] pstate: 80000005 (Nzcv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    1.528411] pc : device_release+0x80/0x90
[    1.528429] lr : device_release+0x80/0x90
[    1.528447] sp : ffff800009e0bab0
[    1.528459] x29: ffff800009e0bab0 x28: 0000000000000000 x27: ffff8000096404a4
[    1.528490] x26: ffff8000096d1960 x25: ffff800009416000 x24: ffff80000971d008
[    1.528519] x23: ffff800009a899e8 x22: 0000000000000000 x21: ffff00000412cd80
[    1.528548] x20: ffff000004130100 x19: ffff000000f32800 x18: 0000000000000010
[    1.528577] x17: 6620656220747375 x16: 6d20646e61206e65 x15: 000000000000010f
[    1.528605] x14: ffff800009e0b7c0 x13: 00000000ffffffea x12: ffff800009b1fd10
[    1.528634] x11: 0000000000000003 x10: ffff800009b07cd0 x9 : ffff800009b07d28
[    1.528663] x8 : 0000000000017fe8 x7 : c0000000ffffefff x6 : 0000000000000001
[    1.528691] x5 : 0000000000057fa8 x4 : 0000000000000000 x3 : 0000000000000001
[    1.528719] x2 : 0000000000000000 x1 : 142e7b8bc84f1100 x0 : 0000000000000000
[    1.528747] Call trace:
[    1.528760]  device_release+0x80/0x90
[    1.528779]  kobject_put+0x7c/0x218
[    1.528799]  device_unregister+0x2c/0x78
[    1.528819]  pci_epc_destroy+0x24/0x40
[    1.528837]  devm_pci_epc_release+0x14/0x20
[    1.528855]  release_nodes+0x64/0x108
[    1.528875]  devres_release_all+0x90/0xf0
[    1.528896]  really_probe+0x1d8/0x428
[    1.528912]  __driver_probe_device+0x114/0x188
[    1.528930]  driver_probe_device+0xb0/0x110
[    1.528947]  __driver_attach+0xbc/0x1c0
[    1.528963]  bus_for_each_dev+0x74/0xc8
[    1.528984]  driver_attach+0x24/0x30
[    1.529000]  bus_add_driver+0x1a8/0x250
[    1.529021]  driver_register+0x64/0x120
[    1.529038]  __platform_driver_register+0x28/0x38
[    1.529058]  rockchip_pcie_ep_driver_init+0x1c/0x28
[    1.529080]  do_one_initcall+0x4c/0x288
[    1.529097]  kernel_init_freeable+0x228/0x2b0
[    1.529116]  kernel_init+0x24/0x128
[    1.529134]  ret_from_fork+0x10/0x20
[    1.529152] ---[ end trace d50b537f42cfef2f ]---

rockchip-pcie-ep.txt binding documentation: https://android.googlesource.com/kernel/common/+/07e6124a1a46/Documentation/devicetree/bindings/pci/rockchip-pcie-ep.txt
Function call in question: https://github.com/torvalds/linux/blob/master/drivers/pci/endpoint/pci-epc-mem.c
void __iomem *pci_epc_mem_alloc_addr(struct pci_epc *epc,
                     phys_addr_t *phys_addr, size_t size)
{
    void __iomem *virt_addr = NULL;
    struct pci_epc_mem *mem;
    unsigned int page_shift;
    size_t align_size;
    int pageno;
    int order;
    int i;

    for (i = 0; i < epc->num_windows; i++) {
        mem = epc->windows[i];
        mutex_lock(&mem->lock);
        align_size = ALIGN(size, mem->window.page_size);
        order = pci_epc_mem_get_order(mem, align_size);

        pageno = bitmap_find_free_region(mem->bitmap, mem->pages,
                         order);
        if (pageno >= 0) {
            page_shift = ilog2(mem->window.page_size);
            *phys_addr = mem->window.phys_base +
                ((phys_addr_t)pageno << page_shift);
            virt_addr = ioremap(*phys_addr, align_size);
            if (!virt_addr) {
                bitmap_release_region(mem->bitmap,
                              pageno, order);
                mutex_unlock(&mem->lock);
                continue;
            }
            mutex_unlock(&mem->lock);
            return virt_addr;
        }
        mutex_unlock(&mem->lock);
    }

    return virt_addr;
}

Answer: I managed to figure it out! It was indeed a Device Tree issue with declaring a valid address for the mem-base register. Below is the device tree overlay which resolved the issue.
/dts-v1/;
/plugin/;
/ {

    fragment@0 {
        target-path = "/pcie@f8000000";
        __overlay__ {
               status = "disabled";
        };
    };

    fragment@1 {
                target-path = "/";
                __overlay__ {
                        pcie_ep0: pcie_ep@f8000000 {
                                compatible = "rockchip,rk3399-pcie-ep";
                                #address-cells = <0x03>;
                                #size-cells = <0x02>;
                                rockchip,max-outbound-regions = <0x10>;
                                max-functions = [08];
                                num-lanes = <0x04>;
                                reg = <0x00 0xfd000000 0x00 0x1000000 0x00 0x00000000 0x00 0x20000>;
                                reg-names = "apb-base\0mem-base";
                                pinctrl-names = "default";
                                pinctrl-0 = <0x17>;
                                phys = <0x15 0x00 0x15 0x01 0x15 0x02 0x15 0x03>;
                                phy-names = "pcie-phy-0\0pcie-phy-1\0pcie-phy-2\0pcie-phy-3";
                                resets = <0x08 0x82 0x08 0x83 0x08 0x84 0x08 0x85 0x08 0x86 0x08 0x81 0x08 0x80>;
                                reset-names = "core\0mgmt\0mgmt-sticky\0pipe\0pm\0pclk\0aclk";
                                clocks = <0x08 0xc5 0x08 0xc4 0x08 0x147 0x08 0xa0>;
                                clock-names = "aclk\0aclk-perf\0hclk\0pm";
                                status = "okay";
                                };
                };
    };
};

Comment: Which PCIe cable did you use?  I'm not sure what sort of cross-over to use.


Allocate 2D array with only one malloc in C

Question: this is my first time asking a question so I might be doing something wrong, I apologize for that.
So, I did a function in C to allocate a 2D array dynamically of void** so i can cast it and use this function to create a 2D array of char or int or else if wanted.
Here is the function :
    void    **ft_new_arr(size_t row, size_t col, size_t size)
    {
        void    **arr_ptr;
        int     *arr_byte;
        size_t    total_size;
        size_t    y;

        if (row == 0 || col == 0 || size == 0)
            return (NULL);
        total_size = (col * sizeof(void*)) + (row * col * size);
        if ((total_size - col * sizeof(void*)) != (row * col * size))
            return (NULL);
        arr_ptr = (void**)malloc(total_size);
        if (arr_ptr == NULL)
            return (NULL);
        memset(arr_ptr, 0, total_size);
        arr_byte = (int *)arr_ptr + col * sizeof(void*);
        y = 0;
        while (y < col)
        {
            arr_ptr[y] = arr_byte + (y * (row * size));
            y++;
        }
        return (arr_ptr);
    }

The only issue I know is that I might be wasting some ram because all the rows will have the same size but I guess its not that bad.
To test it, I did a function that split a string (char *) between whitespaces (0 to 32 in ASCII) and fill my previously allocated array with one word of the string for each column.
And then I did a function just to print my array.
So Here's my big split function :
static int    get_nb_words(char *str)
{
    int    i;
    int    res;

    i = 0;
    res = 0;
    while (str[i])
    {
        while (str[i] && str[i] >= 0 && str[i] <= 32)
            i++;
        if (str[i] && str[i] > 32)
        {
            while (str[i] && str[i] > 32)
                i++;
            res++;
        }
    }
    return (res);
}

static int    get_word_len(char *str)
{
    int    i;
    int    tmp;
    int    res;

    i = 0;
    tmp = 0;
    res = 0;
    while (str[i])
    {
        while (str[i] && str[i] >= 0 && str[i] <= 32)
            i++;
        if (str[i] && str[i] > 32)
        {
            tmp = 0;
            while (str[i] && str[i] > 32)
            {
                i++;
                tmp++;
            }
            if (tmp > res)
                res = tmp;
        }
    }
    return (res);
}

static void    fill_array(char **new_arr, char *str)
{
    int    i;
    int    x;
    int    y;

    i = 0;
    x = 0;
    y = 0;
    while (str[i])
    {
        while (str[i] && str[i] >= 0 && str[i] <= 32)
            i++;
        if (str[i] && str[i] > 32)
        {
            y = 0;
            while (str[i] && str[i] > 32)
            {
                new_arr[x][y] = str[i];
                i++;
                y++;
            }
            new_arr[x][y] = '\0';
            x++;
        }
    }
}

void    ft_printab(char **new_arr, int nb_row)
{
    for (int i = 0; i < nb_row; i++)
        printf("arr[%d] = |%s|\n", i, new_arr[i]);
}

char    **ft_split(char *str)
{
    int        nb_words;
    int        words_len;
    char    **new_arr;

    nb_words = 0;
    words_len = 0;
    if (!str)
        return (NULL);
    nb_words = get_nb_words(str);
    words_len = get_word_len(str);
    if (!nb_words || !words_len)
        return (NULL);
    new_arr = (char **)ft_new_arr(words_len, nb_words, sizeof(char));
    if (!new_arr)
        return (NULL);
    fill_array(new_arr, str);
    ft_printab(new_arr, nb_words);
    return (new_arr);
}

Here is the main btw :
int    main(int ac, char **av)
{
    if (ac != 2)
        return (print_err("Invalid number of arguments", -1));
    char **new_arr;
    new_arr = ft_split(av[1]);
    free(new_arr);
    return (0);
}

I must say that if I don't fill or print the array, I get no error.
when I do, the array is filled like I wanted BUT valgrind gives me a lots of error :
==401691== Memcheck, a memory error detector
==401691== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==401691== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==401691== Command: ./cub3d I\ am\ a\ Fish
==401691==
==401691== Invalid write of size 1
==401691==    at 0x109830: fill_array (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x10999F: ft_split (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x10922A: main (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==  Address 0x4a9f0c0 is 16 bytes inside an unallocated block of size 4,194,096 in arena "client"
==401691==
==401691== Invalid write of size 1
==401691==    at 0x109881: fill_array (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x10999F: ft_split (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x10922A: main (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==  Address 0x4a9f0c1 is 17 bytes inside an unallocated block of size 4,194,096 in arena "client"
==401691==
==401691== Conditional jump or move depends on uninitialised value(s)
==401691==    at 0x484ED19: strlen (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==401691==    by 0x48EADB0: __vfprintf_internal (vfprintf-internal.c:1517)
==401691==    by 0x48D481E: printf (printf.c:33)
==401691==    by 0x1098EF: ft_printab (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x1099B0: ft_split (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x10922A: main (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==
==401691== Conditional jump or move depends on uninitialised value(s)
==401691==    at 0x484ED28: strlen (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==401691==    by 0x48EADB0: __vfprintf_internal (vfprintf-internal.c:1517)
==401691==    by 0x48D481E: printf (printf.c:33)
==401691==    by 0x1098EF: ft_printab (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x1099B0: ft_split (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x10922A: main (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==
==401691== Conditional jump or move depends on uninitialised value(s)
==401691==    at 0x48FF7B7: _IO_new_file_xsputn (fileops.c:1218)
==401691==    by 0x48FF7B7: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1196)
==401691==    by 0x48EB08B: outstring_func (vfprintf-internal.c:239)
==401691==    by 0x48EB08B: __vfprintf_internal (vfprintf-internal.c:1517)
==401691==    by 0x48D481E: printf (printf.c:33)
==401691==    by 0x1098EF: ft_printab (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x1099B0: ft_split (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x10922A: main (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==
==401691== Syscall param write(buf) points to uninitialised byte(s)
==401691==    at 0x4988A37: write (write.c:26)
==401691==    by 0x48FEF6C: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1180)
==401691==    by 0x4900A60: new_do_write (fileops.c:448)
==401691==    by 0x4900A60: _IO_new_do_write (fileops.c:425)
==401691==    by 0x4900A60: _IO_do_write@@GLIBC_2.2.5 (fileops.c:422)
==401691==    by 0x48FF754: _IO_new_file_xsputn (fileops.c:1243)
==401691==    by 0x48FF754: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1196)
==401691==    by 0x48EA049: outstring_func (vfprintf-internal.c:239)
==401691==    by 0x48EA049: __vfprintf_internal (vfprintf-internal.c:1593)
==401691==    by 0x48D481E: printf (printf.c:33)
==401691==    by 0x1098EF: ft_printab (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x1099B0: ft_split (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x10922A: main (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==  Address 0x4a9f0ba is 10 bytes inside a block of size 1,024 alloc'd
==401691==    at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==401691==    by 0x48F2C23: _IO_file_doallocate (filedoalloc.c:101)
==401691==    by 0x4901D5F: _IO_doallocbuf (genops.c:347)
==401691==    by 0x4900FDF: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:744)
==401691==    by 0x48FF754: _IO_new_file_xsputn (fileops.c:1243)
==401691==    by 0x48FF754: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1196)
==401691==    by 0x48E91CC: outstring_func (vfprintf-internal.c:239)
==401691==    by 0x48E91CC: __vfprintf_internal (vfprintf-internal.c:1263)
==401691==    by 0x48D481E: printf (printf.c:33)
==401691==    by 0x1098EF: ft_printab (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x1099B0: ft_split (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==    by 0x10922A: main (in /mnt/nfs/homes/hlevi/42/Ghub/Cub3D/cub3d)
==401691==
arr[0] = |I|
arr[1] = |am|
arr[2] = |a|
arr[3] = |Fish|
==401691==
==401691== HEAP SUMMARY:
==401691==     in use at exit: 0 bytes in 0 blocks
==401691==   total heap usage: 2 allocs, 2 frees, 1,072 bytes allocated
==401691==
==401691== All heap blocks were freed -- no leaks are possible
==401691==
==401691== Use --track-origins=yes to see where uninitialised values come from
==401691== For lists of detected and suppressed errors, rerun with: -s
==401691== ERROR SUMMARY: 36 errors from 6 contexts (suppressed: 0 from 0)

I must be something wrong, but I've tried a lot of stuff and still can't manage to make this work, does anyone have any solution ?
Comment: A pointer to pointer is not a 2D array. That misconception is where all your problems originate. Check out [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays)
Comment: And you can probably replace this whole function with `int(*arr)[col] = malloc( sizeof(int[row][col]) );`
Comment: Why do you have `int *` in `ft_new_arr()` if it is supposed to be generic?
Comment: One problem is that `int *arr_byte` should be `char *arr_byte`. Another problem is that it assumes the array element type is not more strictly aligned than `void *`. To fix that, either add an alignment size parameter or assume the alignment size is the same as the element size. Then if the alignment size is greater than the _Alignof(void *), work out how much padding is required after all the pointers in order to align the elements properly, and add that padding to the total size (and to the size of the start of the memory before the elements).
Comment: Another problem is conversion of incompatible pointer types, such as converting `void **` to `char **` and vice versa.
Answer: Ok, thank's for the answer, what I was doing wrong was declaring int *arr_byte instead of int8_t* or char*.
Thank's to Ian, the padding was wrong !
Now the function works perfectly


This is undefined on Vuejs

Question: I searched and all what I found is that the problem was on arrow functions, but I am not using arrow functions..
<code>  data(){
   return{
     id_list : this.$route.params.list_id,
     people: ['john', 'Sara']
   },

   },created(){
     this.people// this is undefined
   }
</code>
Answer: The problem was in data().
<code>data(){
    return{
      id_list : this.$route.params.list_id,
      people: []
    }
  },
</code>
I was not using $route any more so it was undefined and It broke the vue instance.

This is undefined on Vuejs


Simple Java Script Issue

Question: I am having a terrible time with the first of the following scripts. I'm not sure if the issue is that there are two similar scripts on the page or if my HTML5 code is incorrect. Any help would be appreciated:

And the targeted HTML:

 WHAT WE DO |

 OUR MISSION |

 WHO WE ARE

As stated, the problem is in the first script where my intention is that the active anchor should change back to the default anchor attribute as the others are clicked by the user.
Thanks again.
Answer: Wrap all into document.ready()

Comment: or you could use `$(fn);`
Comment: Thanks, it still was not working so I also changed the link to:  and its all good now.
Comment: Ok, try always to uses the lastest jQuery version. If the plugin does not work is much better search another.
Comment: Well v1.11.1 should be the latest -no? But v1.5.2 seems to load faster and as stated works with this particular script. The 1.11.1 was spinning the page out and not working at all with the script.
Comment: Yeas, maybe the plugin does not support the lastest version :(

Simple Java Script Issue


jacoco offline instrumentation original class path

Question: According to http://www.eclemma.org/jacoco/trunk/doc/offline.html, when doing offline instrumentation, I need to provide the original class files to generate the report.  Exactly how do I do that with Maven?  I know the original class file is saved under generated-classes folder so right now I'm just copying the generated-classes folder over the classes folder when running generating the report but is there a way to specify the original class file path within the POM file?  I couldn't find any examples anywhere. Thanks!
Answer: jacoco-maven-plugin provides instrument that comes in a pair with restore-instrumented-classes. And there is an example of usage in JaCoCo documentation -  http://www.jacoco.org/jacoco/trunk/doc/examples/build/pom-offline.xml


Transforming skewed distribution of dependent variable in linear regression?

Question: As I understand it, the skewness of the response variable in a linear regression does not need to be normal (only the residuals need to be normally distributed). However, I was generally wondering if I can log-transform my response variable so its distribution becomes more normal prior to regression and if this has any benefits/drawbacks.
For context, my response variable y is Medicaid coverage, which has a right-skew in the state I am analyzing. If I log transform this variable using np.log(y + 1), the distribution looks approximately normal. I was wondering if this would be appropriate for a linear regression, or if I don't even need to do this/it is bad practice to do this.
Comment: log(y + 1) is bad practice because it is not scale-invariant; your conclusions should be the same regardless of whether coverage is in dollars or thousands of dollars (or whatever units you are using), but this is not true for this transformation.
Comment: @homoscedasexual Stated again, your conclusions should be the same whether you measured coverage as individuals covered per 100,000 or individuals covered per 1,000. Using log(y + 1) means that your results will change depending on your scale (i.e., because 1 + 4316 is different from 1 + 43.16). Scale invariant means the conclusions do not depend on the scale of the variable (e.g., whether distance is measured in feet, inches, or meters or whether dollars is measured in thousands or millions).
Comment: Log of proportion can sometmes make sense, but you have the same problem with zeros.   Again, generalized linear models with logit link may help here.
Comment: Could you elaborate on what you mean by scale-invariant? @Noah
Comment: If you use generalized linear models with logarithmic link, then zeros are tolerated because the assumption is that the mean function is positive, not that all the data are. On the other hand, if you have lots of zeros, then you're perhaps looking at a zero-inflated response.
Comment: What are the units of measurement of Medicaid coverage?
Comment: Share of individuals covered by Medicaid per 100,000 @NickCox
Comment: @Noah's point holds for any response not a count.
Comment: Ah I see. Suppose instead of coverage per 100,000 I instead used the proportions themselves (0.043 instead of 4300 per 100,000) – would it then be appropriate to do a log-transform since this isn't a count? I know generally using bounded proportions as the response variable in a regression is discouraged though
Answer: In classical regression, there are no normality assumptions about the distribution of predictors in the regression or even the response (Y). All assumptions are based on residuals. It is assumed that the residuals should be approximately normal. Transforming the predictors or outcome might alter the distribution of residuals. Look for the influential observations using Cook's distance to see if any of your extreme values have high influence. Transformations are good whenever u have a meaningful interpretation, for instance, the log transform and reciprocal are often useful depending on context.
Answer: Adding 1 before taking the log is not great because of the reasons identified in the comments.
However, I'm guessing your DV is measured per county or something like that. It's unlikely you have actual 0s. A county in which not one person has Medicaid?  I mean, I suppose it's possible (there are some very small counties) but, in that case, I'd combine neighboring counties.
But why take logs? One reason is if our interest is in multiplicative rather than additive notions of the DV. This frequently happens with money variables. We think about raises, or price increases, or whatever, in % terms, not amount terms. \$5 off on a car is meaningless. \$5 off on a sandwich is amazing.
How about your case? Are you interested in what causes the Medicaid coverage rate to increase by a fixed amount, or a fixed ratio?  Is a change from 10 to 20 the same as one from 20 to 30? Or is it the same as one from 20 to 40?
I don't know, but maybe you do.


Selecting display language using batch script, W2K8 / W2K12

Question: For both Server 2008 Standard R2 and Server Standard 2012, I need to use a command script (not PowerShell) to change the display language. I would prefer a common approach for both types of server and either LIP or MUI - but I'll be happy even if I need distinct solutions for each of these four cases.
Comment: Why not PowerShell?
Comment: As I understand (from other engineers I've asked), the context which my script is called must call a CMD script. My experience is mostly with Unix-like systems, so I'm out of my depth here. But, I _think_ it's something like a CMD process is explicitly given my script to process. (And the whole solution must fit in that single script file.)
Answer: Intlcfg.exe
https://technet.microsoft.com/en-us/library/cc722068%28v=ws.10%29.aspx?f=255&MSPPError=-2147217396
You can run the intlcfg command on an offline Windows image or on a running Windows operating system.
The Intlcfg.exe tool is installed in the Tools folder of the Windows OPK or Windows AIK. (Not installed with windows, you have to download and install)
get the .exe from: http://www.microsoft.com/en-us/download/details.aspx?id=5753
IF this solves your problem, please mark as answered.
Comment: Thanks for this. I was pulled off this task before completing it, so I can't say if it would have been enough, but it did take me much further towards a solution. I was able to "package" your suggestions into my hand-off for others to work on. Hopefully, they will come back here to finalize the conversation. Hopefully...


for loop over list break and continue

Question: To specify the problem correctly :i apologize for the confusion
Having doubts with breaking early from loop .
I have folders - 1995,1996 to 2014 . Each folder has xml files. In some xml files the entry - MaxAmdLetterDate is None.
I want to proceed to the next file in the folder, instead right now with break , the program execution goes to the next folder and skips all files in that folder when it encounters a break
This is the code
import xml.etree.ElementTree as ET
import os
for yrcnt in range(1995,2015):
    old= old="./old/"+ str(yrcnt)
    for dirpath, subdirs, files in os.walk(old): #folder name in which subfolder>files are present
        print files
        for filename in files:
            if filename.endswith(".xml") == True:
                fl=filename
                print fl
                print "-----Reading current file -------"  + str(fl)
                flnm=str(dirpath)+"/"+str(filename)
                tree1 = ET.parse(flnm)                        #db on system
                root1 = tree1.getroot()
                for ch1 in root1.findall('Award'):
                    aid1 = ch1.find('AwardID').text
                    maxdate1=ch1.find('MaxAmdLetterDate').text
                    print "Max amd date : "+str(maxdate1)
            if maxdate1==None:
                break

Help is appreciated .
ORIGINAL POST:
My for loop gets an early exit on encountering a break . Any remedies ?
files=[1,2,3,4,5]
for filename in files:
  print filename
  if filename==3:
     break

o/p:
  1
  2
  3

expected O/p
  1
  2
  4
  5

Thanks !
EDIT: With some comments such as this is expected function of break . Let me elucidate: I want the function to be similar to a range(0,5) , Iam using this as a part of nested loop where I want the loop to go on , and not exit out due to one file .
Comment: Please show expected input and output. Your question doesn't really make sense. This is like asking "my function returns upon reaching the return statement, how do I fix this?"
Comment: what are you expect to get?
Comment: That's what break statements *do by definition...*
Answer: This is the way break works. If you want to iterate over the whole list, simply remove the break (or if you want to do something with the value, use pass as a placeholder).
values = [1, 2, 3, 4, 5]
for value in values:
    if value == 3:
        pass
    print(value)

Output:
1
2
3
4
5

If you just want to a skip a value, use continue keyword.
values = [1, 2, 3, 4, 5]
for value in values:
    if value == 3:
        continue
    print(value)

Output:
1
2
4
5

Answer: Break automatically exits out of your loop. If you do not want to print the filename when it is 3, use an if statement like this:
files=[1,2,3,4,5]
for filename in files:
  if filename!=3:
     print filename

This is the result:
1
2
4
5

for loop over list break and continue


Find the limit of the following expression $\lim_{x\to1}(3 \sqrt[3]{x}-2\sqrt{x})^{\frac{1}{\ln x}}$

Question: $\displaystyle\lim_{x\to1}(3 \sqrt[3]{x}-2\sqrt{x})^{\frac{1}{\ln x}}$
How is this limit taken?
I was able to convert this expression to the following form
$\displaystyle\lim_{x\to1} \exp(\log_x(3 \sqrt[3]{x}-2\sqrt{x}))$
however, this did not clarify
Answer: HINT: Notice that
$$\log_x(3\sqrt[3]{x}-2\sqrt{x})=\frac{1}{3}+\log_x(3-2\sqrt[6]{x})=\frac{1}{3}+\frac{\log(3-2\sqrt[6]{x})}{\ln(x)}$$
And now apply L'Hopital.
Answer: You can calculate the limit using the standard limit $\lim_{y\to 0}(1+y)^{1/y}= e$ and $\lim_{y\to 0}\frac{e^y-1}{y}= 1$ as follows using the substitution $x=e^{6u}$ and considering $u \to 0$:
\begin{eqnarray*} (3\sqrt[3]{x}-2\sqrt{x})^{\frac{1}{\ln x}}
& \stackrel{x=e^{6u}}{=} & \left(3e^{2u} - 2e^{3u}\right)^{\frac 1{6u}} \\
& = & e^{\frac 13}\left(\left(\left(1+2(1-e^u)\right)^{\frac 1{1-e^u}}\right)^{\frac{1-e^u}{u}}\right)^{\frac 16} \\
& \stackrel{u \to 0}{\longrightarrow} & e^{\frac 13}\left(e^2\right)^{-\frac 16} = 1
\end{eqnarray*}


How to carry out repeated time-to-event analysis in SPSS?

Question: Please note that I also posted this same question in an SPSS specific forum, however if anyone here can also help, I would appreciate it.
I have a question regarding repeating time-to-events measurements and after a quick search I really did not find anything that directly answered my question. So here goes...
Context (an example of the model I am trying to create):
I have say 25 patients who have all undergone the same procedure and who meet eligibility criteria for a study. Date of discharge from hospital is "Day 0" and I am tracking multiple events following their discharge that cause these patients to be readmitted. The type of event is not necessarily important at this stage (could be anything from infection, to bleeding, to device failure, etc.), as any event causing readmission is classified as 1=true or 0=false. Patients who then receive a subsequent procedure have reached their end-point are censored from that point on. The issue I am having is that each patient may have multiple readmissions following treatment, thus:
Patient 1: day 0-->6* (readmission), day 7-->25* (readmission), day 26 -->65* (readmission);
Patient 2: day 0-->34* (readmission), day 35-->38* (readmission)
Patient 3: day 0-->50*(readmission), day 51-->53**(subsequent procedure), *Day 54-->60(readmission)*
..etc.

where * notes the day being readmitted to hospital. and text following the 'double asterisks' means any readmissions at this point forth are excluded because they have reached end-point. Incidence per patient-year at say day 30 was just a matter of calculation by hand, but if I wanted to perform some sort of Kaplan-Meier survival type estimate or a Cox Hazard regression for risk of readmission using $x$ and $y$ covariates, is that possible to do in SPSS? Even though with 25 patients, the study may be underpowered to come to any meaningful conclusion with further analysis beyond descriptives, is this possible and how?
Thus far, my previous works have been limited to Kaplan-Meier survival, log-rank analysis, and Cox-regressions with single definite event (i.e., death) that is continuous and non-repeating... so I'm treading into new water.
If anyone has any advice or can point to me to specific reading materials, that would be greatly appreciated.
Comment: Welcome to this site! I've tried to reformat things a bit. We have some editing facilities using [Markdown language](http://stats.stackexchange.com/editing-help). I'll let you correct or reword the part about "text in italics" (which was not apparent in your original version anyway).
Answer: There is a whole theory developed on recurrent events.  For some reason these problems and solutions are not often mentioned in survival analysis or reliability courses or books.  The same can be said for the theory of competing risks.  Anyway let me refer you to the two books i know on this topic.  Wayne Nelson's came out first and was published in the ASA/SIAM series. It was the first or one of the first books in that series.  Here is the link to more information on it at amazon.com:
http://www.amazon.com/Recurrent-Recurrences-Applications-Statistics-probability/dp/0898715229/ref=sr_1_2?s=books&ie=UTF8&qid=1343339216&sr=1-2&keywords=recurrent+events
The second book was by Cook and Lawless and was published by Springer-Verlag:
here is an amazon link to it:
http://www.amazon.com/Statistical-Analysis-Recurrent-Statistics-Biology/dp/1441924159/ref=sr_1_1?s=books&ie=UTF8&qid=1343339260&sr=1-1&keywords=recurrent+events
Comment: Thank you for your suggestions Michael. I ordered the second book used (the prices are starting to remind me of undergraduate textbooks all over again :) ), so look forward to that coming in the mail. In the clinical area of my work, most events are continuous (i.e. the evolution of mitral regurgitation) - but I can see how repeating events over time might be interesting in a clinical context for publication. Pertaining my question, can anyone comment on using this model in SPSS/PASW v 18.0?
Comment: If you order through amaxon I hope you looked at used books often they have very good prices for books in good condition.


Temporarily Changing Keys - Which accidentals to use?

Question: I've was taught that whenever you write a run of notes going up, you should use sharps instead of flats; And whenever you go downwards, you should generally write flats instead of sharps. My question is: Which accidentals I should use when changing keys while moving against the flow of reading?
For example: If I was in the key of C Major and I was writing a downward run of notes and temporarily went into they key of E Major (4 sharps). Should I express it in the form of flats (making it easier to read, but harder to see the key change), or should I write sharps (making it harder to read, but easier to see the key change)?
I originally went with writing flats because it seemed easier to sight read. But if I had chords in the left hand, I'd end up with a lot of sharps in the left hand and a lot of flats in the right.
Comment: I don't think you should mix flats and sharps if you're intending to denote the same thing.  If you're modulating to E then the natural choice would be sharps or a temporary change of key signature.
Answer: To answer your question broadly, you should always use what is going to read easiest for the performer. Putting a scale of flats in E major would be very confusing and unwieldy as E major contains four sharps and musicians are much more accustomed to reading sharps in keys where sharps naturally occur; such as E, A, or B major for example.  Expressing E major as Fb major would be both unnecessary and is technically impossible as it would constitute 8 flats for the key signature; including double flats which aren't used often, except for special circumstances.
That said, the use of accidentals with respect to a key change are greatly affected by what the intended harmonic function is.  Unless you're reading atonal or serial music, notes with accidentals largely have some sort of harmonic function attached to them.  That is why they are called "accidentals" - because they reflect a harmonic or notational intent of a composer that isn't possible with the current key signature.
In this respect, the general rule that you use for writing scales can also be applied to modulating to different keys: use sharps to get to a "sharp" key, use flats to get to a "flat" key.  There are some theoretical exceptions to this of course, but then again, there always are.
If you do that, you should be fine.
Comment: If you reread my answer, you will see that the quote you provided was with respect to the key of E major.  In general, musicians expect to read sharps in the key of E major.  As a composer, I am quite well aware of the reading tendencies of instruments at various levels of proficiency.  Out of courtesy, I would respectfully suggest reading a post twice before commenting.
Comment: Re: "...musicians are much more accustomed to reading sharps..." That's actually not accurate. This is only the case for pianists and string players, and really only true of the inexperienced or mediocre among them. Out of courtesy, I'll leave it to you to revise if you wish.
Answer: If it's a key that uses sharps on the cycle of fifths use sharps. Likewise for flats. Almost every score I've played through has been written this way. Even if it calls for things are a little trickier to read. Also with regards to mixing sharps and flats it's normally for something wierd like the Phrygian major stuff you get in Klezmer.
Answer: The rules you have learned are generally sound advice, but music is now to the point where we performers generally have to be able to deal with those kind of problems pretty fluidly, but fortunately performers typically are at this point. The sharp and flat rule is particularly applicable to chromatic scales, but even this basic form of the rule is broken all the time when you skip down to a chromatically raised leading tone that then leans up to its neighbor.
In your case, especially, if the passage going down is clearly in E major, the player can easily see that ahead of time and will have no problem with it. I would be hard pressed to think of a case where it would be better to write e major in flats. The alternative would be the key of F-flat major in which every note is flat including a Bbb.
If that's not satisfactory, please send or reply with a photo of the passage if you are still unsure and I'd be happy to advise.
Answer: The rule you are citing only holds for chromatic figures. If you have changed keys, then you use accidentals that match the new key.
If you are using a chromatic figure to move to the new key, then follow the rules for a chromatic figures until you finally arrive in the new key, where you then use accidentals that match the key. For example:
Comment: A nice thing about this example is the avoidance of a direct transition from Gb to G#.


Finding the general term of a sequence defined by an integral between 0 and 1

Question: I am looking for the general term of the following sequence defined by
$$
u_n = \int_0^1 F\left(-n,n+\frac{3}{2}; 2, r^2\right) \, \mathrm{d} r \, , \quad(n \ge 0) \, ,
$$
wherein $F$ is the hypergeometric function.
Using computer software algebra e.g. Maple, it is possible to know the terms for specified values of $n$ such as $u_0 = 1 $, $u_1 = 7/12$, $u_2=43/120$ etc.
It would be great if someone here could provide with an idea that helps in determining the expression of $u_n$.
Thanks
R
Comment: put $r^2=t$. Then apply the Euler Transformation with $z=1, c=1/2, d=3/2$ to get a higher order hypergeometric function $_3F_2$...
https://en.wikipedia.org/wiki/Generalized_hypergeometric_function
Comment: Furthermore Dixon's and Saalschütz's formulas will reduce the resulting equation into Gammavalues for a large class of parameters...just play around a litte bit and see what you get
Comment: @tired thanks indeed. By setting $t=r^2$ Maple gives directly the desired result as ${}_3F_2 \left( \frac{1}{2}, -n, n+\frac{3}{2}; \frac{3}{2}, 2;1 \right)$.
Answer: This is not an answer but just the result from a CAS.
$$u_n=\int_0^1\, _2F_1\left(-n,n+\frac{3}{2};2;r^2\right)\,dr=\frac{\pi  \,
   _3\tilde{F}_2\left(-n,\frac{1}{2}-n,\frac{3}{2};2,\frac{3}{2}-n;1\right)}{4 \Gamma
   \left(n+\frac{3}{2}\right)}$$ where appears the regularized generalized hypergeometric function.
$n$ being an integer, the above can simplify to $$u_n=\frac{(-1)^{n+1}}{(2 n-1) (2 n+1)} \,
   _3F_2\left(\frac{3}{2},\frac{1}{2}-n,-n;2,\frac{3}{2}-n;1\right)$$ where appears the generalized hypergeometric function.
Comment: @RobertoFermi. I still use a very old version of Maxima which we adapted for some specific needs.
Comment: Thanks for your quick answer. What CAS have you used? Maple doesn't succeed to evaluate it..


Finsler Metric from page 2 of the book by Chern and Shen.

Question: Physicist here not a mathematician.  I am trying to understand the notation for the Finsler metric in Chern and Shen's book.
The equation is
$$\textbf{g}_y(u,v):=\frac{1}{2} \frac{\partial^2}{\partial s \partial t} \left[ F^2( y + su + tv) \right]_{s=t=0} ~.$$
Normally I would have written something like
$$ \textbf{g}(u,v)= \frac{1}{2}\frac{ \partial^2 F^2}{\partial u\partial v} u v~. $$
If I make substitutions $a=su$ and $b=tv$, evaluation of the top equation gives me
$$ \frac{1}{2}\frac{ \partial^2 F^2(y)}{\partial a\partial b} uv ~.$$
Which is not the middle equation.  Thanks for the help.
Comment: Note that if you don't get a helpful response here, PhysicsStackExchange is another good place to ask.
Answer: The logic of notation in the book is that $u,v $ are vectors and $s,t$ are scalars. So the composition $F^2(y+su+tv)$ is a function of two scalar variables $s,t$. We take second mixed partial of that function.
What you wrote in the second equation may be familiar to you, but I do not recognize "partial derivative with respect to a vector" as a precise mathematical concept.


Seria possível ler o atributo lastModified de um arquivo hospedado no servidor?

Question: Com o código a seguir eu consigo ler (e mostrar na console) o conteúdo de um arquivo (myfile.txt) que se encontra hospedado on-line na mesma pasta do mesmo servidor onde esta a página html com o código em questão.

<!DOCTYPE html>

Porem em lugar de ler o conteúdo do arquivo eu preciso ler somente a data/hora da ultima modificação do referido arquivo. Considerando que eu consigo ler o conteúdo do arquivo em questão imagino que também seria possível ler somente os atributos do tal arquivo mas até agora não encontrei a forma de atingir tal objetivo (cabe mencionar que sou bem inexperiente com JavaScript).
Agradeço qualquer ajuda com o tema.
Answer: Sugiro experimentar o exemplo abaixo:
fetch("/caminho/para/myfile.txt").then(response => response.blob()).then(blob => {
  const file = new File([blob], blob.name);
  console.log(file.lastModifiedDate, file.lastModified);
});

Comment: Sua resposta pode ser melhorada adicionando informações fundamentais. Basta [edit] para adicionar mais detalhes, como citações ou documentação para que outras pessoas possam confirmar que sua resposta está correta. Você pode encontrar mais informações sobre como escrever boas respostas [na Central de Ajuda](/help/how-to-answer).
Comment: @Erikf, muito obrigado pela sua sugestão. Experimentei conforme seu exemplo mas ele retorna sempre a data/hora atual e não  a data/hora da ultima modificação do arquivo. Onde será que estou errando?
Answer: Eu tinha postado a mesma pergunta no Stackoverflow em ingles e um usuario me respondeu com a solução:

<!DOCTYPE html>


Bootstrap Buttons Won't Display Properly

Question: My Problem
I am trying to add 3 big buttons ( Summary, Builder, History ) in the middle of my nav bar. Below is pictured my ideal results. I would like an active one to have no bottom border.

I have tried everything I can think of to get this to work. When I add a button in the spot you would think it should go, here is what I get:

Notice the yellow oval, that is where my button is ending up!! I can't figure out what I am doing wrong.
Here is a link to the page, as to post the code here would be too long:
http://johntesting.azurewebsites.net/workflow-blue/blank-backup.html
Can anyone help me out with these buttons? I have been going crazy trying to figure this out.
Thanks for looking.
John
Comment: have you tired z-index?
Comment: There is no HTML code here and even on the page that you included there are no buttons
Comment: I apologize, there was a problem FTP. I am re-uploading the project now. Must have had a hiccup during upload.
Comment: OK the link is updated. Again, my apologies.   http://johntesting.azurewebsites.net/workflow-blue/blank-backup.html
Answer: Set display:flex; on your '.page_title' div to immediately see the button much closer to where you expect it. The first child element of that div is an h3 which is a block element that fills the entire div, leaving no room for your buttons, resulting in them being pushed to the next line.
Another approach is to set the display property of that h3 to inline-block.
Comment: Thanks JasonB, I am trying out this suggestion now.


Intensity estimation of the marked process.

Question: I observe a sample of the form $=[_1,_2,…,_]=[(_1,_1),(_2,_2),…,(_,_)]$ where each $_$ is an arrival time and  is the amount of money spent by the th customer.
I assume that for each amount $$, the arrival times of the filtered sample $=[_:_>]$ follow a homogeneous Poisson process with the intensity $_x=^{−}$.
How do I estimate $$ and $$? Is it correct that $1/=\frac{1}{}∑^_{=1}_$?
Edit: It is easy to estimate  and  by counting the number of events in each $S_x$ for several values of $x$ and fitting a linear regression to the logarithms of these numbers.
Answer: Assuming that those are all arrivals within some time window of known duration $T$, you can try the maximal likelihood estimate. Under your assumptions, the probability density at the observed event will be proportional to $A^NT^Ne^{-AT}k^Ne^{-k\sum_j x_j}$, so, indeed, $\frac 1k=\frac 1N\sum_j x_j$ is a good choice. By the same logic, $A=N/T$ (which is not at all surprising). Of course, these estimates are meaningful only if the model itself is not too far from the truth, but checking that is a completely different issue.
Comment: What you are saying might be correct. I have hard time understanding the independence of arrival times and the $x$'s. It seems to me that under my assumptions they are not independent.
Comment: @VladimirKrouglov I used the model when you just have independent Poisson processes with intensities $Ake^{-kx}dx$ for customers paying between $x$ and $x+dx$ (passing to the limit as $dx\to 0$ at the end), which fits your description though I agree that it might be not *the only* model that fits your description, in which case the question is ambiguous and we may want to specify the model further first.
Comment: @VladimirKrouglov As I said, it would be nice to think of how to test that the model is not too far from the reality before trying to estimate the parameters from the data, but it is a completely different topic :-)
Comment: I guess the only thing I have is that if I plot the intensity of arrivals of customers with $x>y$ as a function of $y$ - it is very well approximated by the exponential curve, i.e. the $log$ of this function is very close to linear.
Comment: and, by the nature of my problem customers who spend the amount $x>y$ come independently.


Getting data from specific datetime range

Question: I'm trying to write a web application that does time series analysis.
I wrote a function in Python to take a subset of a dataset (a Python dict) that falls within a given datetime range (using Python's datetime.datetime class).
In my web application, I'm doing a calculation that calls this function a few hundred times to do a selection on a dataset of around 10,000 points.  This takes around 25 seconds, which is OK, but not ideal.
I've attached an example of my method running on some sample data.  Is there a better way to achieve the same result with better performance?  Suggestions on better frameworks to use are also appreciated (for example, would it be better to do this with a numpy array, or to abandon Python entirely?).
The function outputs the amount of time elapsed.
from datetime import datetime
from datetime import timedelta

data_dict = {'times':[], 'data':[]}

#Generate sample data
start_datetime = datetime(2014, 8, 23, 15, 17, 17, 392943)
for i in range(10000):
    data_dict['times'].append(start_datetime+timedelta(minutes = 5*i))
    data_dict['data'].append(i)

startTime = datetime.now()

def data_select(data_dict, time_range):
    start = 0
    end = 1
    for x in data_dict['times']:
        if x - time_range[0] < timedelta(seconds = 0):
            start += 1
        if x - time_range[1] <= timedelta(seconds = 0):
            end += 1
    data_dict['times'] = list(data_dict['times'][start:end])
    data_dict['data'] = list(data_dict['data'][start:end])
    return data_dict

#Example function call
data_sub_dict = data_select(data_dict, [datetime(2014, 8, 30, 0, 0, 0, 0), datetime(2014, 9, 5, 0, 0, 0, 0)])

print "Time elapsed: " + str((datetime.now() - startTime))

Answer: Since your data is sorted, you can cheat and use the very useful bisect module. Instead of doing a linear search across the list of data, it checks the middle value, then takes the left or right half -- a lot fewer comparisons. If the output data is right, bisect is about 800x faster for 10k data points.
source
import bisect
from datetime import datetime
from datetime import timedelta

data_dict = {'times':[], 'data':[]}

#Generate sample data
start_datetime = datetime(2014, 8, 23, 15, 17, 17, 392943)
for i in range(10000):
    data_dict['times'].append(start_datetime+timedelta(minutes = 5*i))
    data_dict['data'].append(i)

startTime = datetime.now()

def data_select_search(data_dict, time_range):
    start = 0
    end = 1
    times = data_dict['times']
    for x in times:
        if x - time_range[0] < timedelta(seconds = 0):
            start += 1
        if x - time_range[1] <= timedelta(seconds = 0):
            end += 1
    # print 'search:',start,end
    data_dict['times'] = list(data_dict['times'][start:end])
    data_dict['data'] = list(data_dict['data'][start:end])
    return data_dict

def data_select_bisect(data_dict, time_range):
    times = data_dict['times']
    start = bisect.bisect_left(times, time_range[0])
    end = bisect.bisect_right(times, time_range[1], lo=start) + 1
    # print 'bisect:',start,end
    return dict(
        times=data_dict['times'][start:end],
        data=data_dict['data'][start:end],
        )

drange = [
        datetime(2014, 8, 30, 0, 0, 0, 0),
        datetime(2014, 9, 5, 0, 0, 0, 0)
    ]
data_sub_dict = data_select_search(data_dict.copy(), drange)
_dict2 = data_select_bisect(data_dict.copy(), drange)

import timeit
mysetup = "from __main__ import data_select_bisect, data_select_search, data_dict, drange"
num = 100

print('search:', timeit.timeit(
    "data_select_search(data_dict.copy(), drange)",
    setup=mysetup,
    number=num
    ))
print('bisect:', timeit.timeit(
    "data_select_bisect(data_dict.copy(), drange)",
    setup=mysetup,
    number=num,
    ))

output
('search:', 1.2735650539398193)
('bisect:', 0.0015599727630615234)


Estimating variance of a Poisson variable

Question: The sample variance $S^2$ is an unbiased estimator of the variance $\sigma^2$ of a random variable $X$, and is generally used for this purpose, I believe. But if we assume that $X$ has a Poisson distribution, it seems natural to use the sample mean $\bar{X}$, as $\sigma^2=\lambda=E(X)$. What is the best estimator in this case, and why?
Answer: Since Poisson is a member of the regular exponential family, it follows that $\bar X$ is a complete sufficient statistic for $\lambda$. Since $\bar X$ is also unbiased, it follows by the Lehmann–Scheffé theorem that $\bar X$ is the unique minimum variance unbiased estimator (MVUE) of $\lambda$.
Although $S^2$ is unbiased estimator of $\lambda$. Its variance is:
$\mathrm{Var}(S^2) = \frac λn + \frac {2λ^2}{n−1} > \frac λn = \mathrm{Var}(\bar X)$
In fact, after we found MVUE, we do not need to find other estimators, because it is impossible to find the better one.
Answer: Here's my non mathematical "proof" why the mean estimator must be used.
By definition, you need to know the mean before the variance. That's why the degrees of freedom of the sample variance estimator's distribution is one less than the number of observations: the mean takes away one degree of freedom. Hence, it is impossible to come up with a better estimator of the Poisson's variance than the estimator of its mean, which also happens to be its intensity $\lambda$. So, yes, use the estimator $\hat x=\hat\lambda$ of its mean.
Also, wouldn't it be weird to posit that the variance estimator of Poisson distribution is different than its mean estimator?
Remark: I wouldn't insist on using $\hat\lambda=\bar x$. It's the best from maximum likelihood point of view, and if that's what you want then great, use it. However, my "proof" is not based on your choice of estimator, particularly $\bar x$. My argument works for any estimator: you can't have a better variance estimator than the estimator of its mean regardless of the choice of the $\hat\lambda$
Comment: 1. You *don't* need to know the mean before the variance, since you can compute $s^2_n$ as half the average squared distance between pairs of values across all pairs [without ever knowing the mean](https://en.wikipedia.org/wiki/Variance#Sample_variance). 2. Your "therefore" in the second paragraph doesn't seem to follow -- how exactly do the particular things you said imply that it's impossible to find a better estimator? Did you leave some part of the argument out? 3. The first sentence of your remark in the last paragraph directly contradicts the use of the word "must" in your first sentence
Comment: @Glen_b, I didn't mean the sample variance here, but the variance itself. It's defined using the mean.
Comment: the specific form of the definition is an arbitrary convention that doesn't relate to the point here. You said "*you need to know the mean before the variance*". If you look up from the link to the immediately preceding section, you can see that population variance can be defined the same way as an average pairwise squared distance, *without any reference to the mean*, and without changing its value.  Demonstrably that isn't the case that you "need to know the mean", whether you mean as a population quantity or a sample quantity.  [Your above argument would apply equally to the normal ... ]


owner, tablename, view name oracle

Question: how do I get the owner , table name and all view names created from a table in oracle ? I'm looking at dba_tables and dba_views seperately, but is there a way to join both.
Comment: You would also want to take a look at [(dba | all | user)_dependencies](https://docs.oracle.com/cd/B28359_01/server.111/b28320/statviews_1066.htm#REFRN20053) data dictionary view.
Comment: What do you mean by "view names **created for a table**"? There is no such concept in relational databases. Views are not created "for a table". A view may select from a table (or from several). Do you mean, find all views that select FROM a given table? Then: Be aware that a view may select from another view, and this other view may select from "your" table. Do you also want this more remotely related view to be shown, or only views that select DIRECTLY from your table?
Comment: Thanks for the education. I mean views created from a table.
Comment: Thank you Nicholas! I was able to get the information that I was looking from dba_dependencies view
Answer: You can use this query. Enter the name of your table in upper case.
SELECT referenced_owner OWNER,
  referenced_name TABLE_NAME ,
  NAME VIEW_NAME
FROM all_dependencies
WHERE type          = 'VIEW'
AND referenced_type = 'TABLE'
AND referenced_name = '&table_name';

This would give you output of the form :
OWNER           TABLE_NAME        VIEW_NAME
-------     ----------------- ------------------
HR              DEPARTMENTS       EMP_DETAILS_VIEW
HR              DEPARTMENTS       V_EMPLOYEES
HR              DEPARTMENTS       V_EMP_DEP

Note : As mentioned in one of the comments, "view names created for a table" is not meaningful. It should be mentioned as Views that depend on a given table.

owner, tablename, view name oracle