Replicar string em determinada parte de um texto (javascript/JQuery)

Question: Tenho um texto e gostaria de replicar strings em determinada parte deste usando javascript ou JQuery
Texto = Jo 3:16; 7:1,3; 3.20; Mt 2:3,5,2-7; 3:1; Lc 2:1; 3:12
O que consigo até agora: Jo 3:16; Jo 7:1,3; 3.20; Mt 2:3,5,2-7; Mt 3:1; Lc 2:1; Lc 3:12
Resultado desejado: Jo 3:16; Jo 7:1,3; Jo 3.20; Mt 2:3,5,2-7; Mt 3:1; Lc 2:1; Lc 3:12
Segue abaixo o que tenho conseguido até agora, só consigo adicionar uma sequencia a mais

$(document).ready(function(){

  var str = document.getElementById("resultado").innerHTML;
  var res = str
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
document.getElementById("resultado").innerHTML = res;
//r = document.getElementById("resultado").innerHTML = res;
console.log(r)

});

Jo 3:16; 7.1,3; 3:20; Mt 2:3,5,2-7; 3:1; Lc 2:1; 3:12

Comment: Você pode colocar a string dentro de uma variável, e no texto vc replica essa variável quantas vezes quiser declarando-a nos trechos desejados.
Comment: Eu já consigo colocar em uma variável, o que preciso agora é fazer com que seja distribuído corretamente no texto as strings
Answer: Segue abaixo como resolvi, pode não ser o código ideal mas serviu

$(document).ready(function(){

  var str = document.getElementById("resultado").innerHTML;
  var res = str
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
.replace(/(Jo|Mt|Lc) (([0-9]{1,3}(\.|:)[0-9]{1,3}((,| ,|, |-| e )[0-9]{1,3}){1,})|([0-9]{1,3}(\.|:)[0-9]{1,3}))(;|)( [0-9]{1,3}(\.|:))/ig, "$1 $2; $1$10")
document.getElementById("resultado").innerHTML = res;
//r = document.getElementById("resultado").innerHTML = res; console.log(r);

});

Jo 3:16; 7.1,3; 3:20; Mt 2:3,5,2-7; 3:1; Lc 2:1; 3:12


Response.write and ASP.NET controls

Question: Please see the code below:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Response.Write("

some text

")
        Response.Flush()
        Threading.Thread.Sleep(3000)
        Response.Write("

some text

")
        Response.Flush()
        Threading.Thread.Sleep(3000)
        Response.Write("

some text

")
        Response.Flush()
        Threading.Thread.Sleep(3000)
        Response.Write("

some text

")
        Response.Flush()
        Threading.Thread.Sleep(3000)
    End Sub

The code works as I would expect i.e. a HTML table is generated and written to the screen, then after three seconds another table is generated and added to the screen etc.
I have a webpage that dynamically adds x number of tables (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.table%28v=vs.110%29.aspx) to a webpage.  However, Response.Flush does not seem to work.  I have created the simple code below to explain the problem:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim Table1 As New Table
        Dim r As New TableRow()
        Dim c As New TableCell()
        c.Text = "Test 1"
        r.Cells.Add(c)
        Table1.Rows.Add(r)
        Response.Flush()
        Threading.Thread.Sleep(3000)

        Dim Table2 As New Table
        Dim r2 As New TableRow()
        Dim c2 As New TableCell()
        c.Text = "Test 2"
        r.Cells.Add(c)
        Table1.Rows.Add(r)
    End Sub

Is it possible to flush table1 to the client, then wait a few seconds then flush table2 or do you have to write a string to response.write?
Comment: You are not adding Table1 to the form. It needs to be part of the forms controls collection. Try adding a placeholder to the form, and then add the table to the placeholder.
Comment: @Craig Moore, it is added to a PlaceHolder control in the live app.
Comment: Your question is in regards to understand response.flush or resolve your problem?
Comment: @Dalorzo, both.  Is it possible to use response.flush with a page that contains web controls?
Comment: All the examples I can find are HelloWorld type examples i.e. flush a text item to the screen.
Answer: To use Response.Flush in that way you need to output all the HTML code yourself using Response.Write.
You can't use web controls as they are not rendered until the page is complete. The web controls only exist as objects until the Page_Render stage where the entire page is rendered as HTML code and then sent to the browser.
Comment: Thanks for that +1.  Is there an alternative to response.flush, which allows web controls to be used? Perhaps an asynchronous approach to building a webpage.
Comment: @w0051977: Yes, you can send the page without the content and use AJAX to fetch more content. You can use plain Javascript and fetch HTML from a handler, or use `PageMethod` to call server code from Javascript, or use the AJAX support with update panels.
Comment: Can you post a link before I mark the answer? Perhaps this one: http://msdn.microsoft.com/en-us/library/vstudio/ms178208%28v=vs.100%29.aspx.  I want to make sure I am on the right track.
Comment: @w0051977: Yes, that is one option. Here is a roadmap: http://msdn.microsoft.com/library/bb398822.aspx
Comment: I assume that this works the first time a webpage loads as well as with postbacks?

Response.write and ASP.NET controls


Linux / BSD "quickly" wipe a USB drive?

Question: I'm clearing a drive with dd. It's a USB device and 120GB and it's taking a very long time:
dd if=/dev/zero of=/dev/da0 bs=1M status=progress

I have tried messing with the block size (smaller and larger values bs=4M, bs=8M, etc) but nothing seems to make much of a difference. It's writing at about 7000 kB/s which is painfully slow.
I do not care about "securely" wiping the data I just want it wiped so I can re-establish the partition structure and filesystem from scratch. Is there an alternate way (using standard utilities) that can perform a quick (within a minute or two) wipe for this type of scenario? The device I'm working on is FreeBSD but I think the dd command (and gpart, etc) work similarly between it and Linux.
Comment: *"I do not care about "securely" wiping the data I just want it wiped so I can re-establish the partition structure and filesystem from scratch."*  - Then don't wipe at all, simply skip the wiping and immediately run your favourite partitioning tool and format the new partition(s) with your favourite file system(s).
Comment: Actually, the FreeBSD `dd` has significant differences from the GNU `dd` used on most Linux systems. None of those differences are likely to matter for your use case though.
Comment: The reason this is so slow is that as far as the drive knows it's got to store all those zeros you are writing to it, and also the data that you haven't got to yet. So its shuffling this data around in case you should stop writing zeros and start reading sectors. Cheap thumb drives in particular are very bad at this. https://en.wikipedia.org/wiki/Write_amplification#:~:text=Write%20amplification%20(WA)%20is%20an,amount%20intended%20to%20be%20written.
Answer: The Linux command to remove all disklabel and file systems signatures is:
wipefs -a /dev/sd###

Most modern flash disks and flash USB sticks are supporting TRIM / DISCARD / SCSI UNMAP. For USB  you need to enable it:
# cat /sys/block/sd###/device/scsi_disk/*/provisioning_mode
full
# echo unmap > /sys/block/sd###/device/scsi_disk/*/provisioning_mode
# cat /sys/block/sd###/device/scsi_disk/*/provisioning_mode
unmap
#

If trim is enabled you can destroy all data with:
blkdiscard /dev/sd###

Replace sd### with something like sdx or nvme9n9. You can list all block devices with: lsblk
Answer: The fastest way is to remove all partitions on the drive. This will "remove" also filesystems. And zero first megabyte (boot record and so).
dd if=/dev/zero of=/dev/da0 bs=1M count=1

and you have "brand new" disk.
Comment: well, let me nitpick there: it'll not *remove* the filesystems, it will make them impossible to find by the operating systems. They could quite likely be recovered. But 100% agree, if "securely" wiping isn't necessary, that's sufficient.
Comment: This would not work for GPT, as there is a backup copy of the partition table stored at the end of the disk.
Comment: Leaving aside the GPT issue: my recollection is that one of the differences between fdisk on MS/PC-DOS and fdisk on Linux is that the DOS variant also blanks out the first sector or so of any removed filesystem. Subject, I'd note, to anything the OEM (PC Supplier) has done since originally at least it was their responsibility to build fdisk.
Answer: There's different kinds of USB storage devices.
If your device supports it,
blkdiscard /dev/da0

on Linux, or
trim /dev/da0

on FreeBSD
will tell the drive to just drop all data. That's going to be very quick, as it basically is just telling the wear leveling table that "hey, forget that any of the blocks ever were used for data, and treat them as ready to be nulled and used again".
Even if it's that quick, it will have the nice side effect of "nulling", i.e. it makes the old data irrecoverable (short of opening the hardware, circumventing the storage controller inside, and reading raw data an  taking a huge guess on what block in which order needs to be reassembled).
Comment: This might be the only "great" way, if it can be made to work on usb drives. Advice to null the whole device through like you would with an old school hard disk might a) leave more data intact than you want and b) wear the drive out a lot or even damage it if done repeatedly (I had bottom-drawer usb sticks fail after nulling them a few times!).
Comment: I was referring to usb sticks, not spinning disks, to (not) do this to.
Comment: Ah, now your comment makes way more sense! Let me clean up mine!
Answer:
I do not care about "securely" wiping the data I just want it wiped so I can re-establish the partition structure and filesystem from scratch

Just run mkfs on the partitions.
Comment: This assumes the partition structure is ok, you need to create the partitions before you can run mkfs

Linux / BSD "quickly" wipe a USB drive?


Allow auto import my React library on vscode

Question: I'm trying to do a library of components for React and publish on npm using webpack and babel to compile to Es5.
Almost everything worked, but for some reason, the project that consumes this lib cant auto import their components
I have a project on github with the setup I used:
https://github.com/dattebayorob/react-loading
//webpack.config.js
https://github.com/dattebayorob/react-loading/blob/master/webpack.config.js

//.babelrc
https://github.com/dattebayorob/react-loading/blob/master/.babelrc

//package.json
https://github.com/dattebayorob/react-loading/blob/master/package.json

I'm expecting to import components from my lib with 'CTRL+space' when typing then
Now, I can import from my lib manualy with import { Component } from 'my-react-lib'
Answer: Sometimes, when using Typescript in VSCode, you have to run the Typescript: Restart TS Server command in your command palette for auto import to work after creating new files. It's a bug.
Answer: On dattebayorob/react-loading/index.d.ts try:
export  *  from './src/components'

Answer: In package.json, you have "main": "./index.d.ts", but that's not a valid JS file, as it does not contain actual code, only type definitions.
In a library, usually you need to have an src/index.js file that imports / exports all components and in package.json you add the build artifact as main: "main": "dist/index.js".
Also, don't forget to explicitly specify the files: ["dist"] attribute in package.json so the src folder is not downloaded when your package is installed.
Comment: I am experiencing the same problem as the OP. Even if I put `dist/index.js` and generate that `dist/index.js` during build (I use Rollup instead of Webpack), I am not able to navigate to the library code when I import components from another project. Could you share some resources or useful tutorials that can teach everything someone needs to know when building JS libraries published to NPM? Thank you!


jQuery combine selectors

Question: How can I combine this two selectors:
$("td.listas select:last option:selected")

This doesn't work!
What I'm trying to achieve is:

Get the select input from a td with the class listas
Get the option that is selected

Notes:

In this td, I have two select inputs.

Code working:
    var selectprimeiro = $('td.listas select:first');
    var selectsegundo = $('td.listas select:last option');
    selectsegundo.prop("selected",true);
    var selected = $("td.listas select:last option:selected");
    selected.each(function(){
        selectprimeiro.append("<option value='"+$(this).val()+"'>"+$(this).text()+"</option>");
        selected.remove();
    });

Comment: Btw, your selector does "work": http://jsfiddle.net/4m3jG/1/
Comment: I see only one selector. Anyways, questions about selectors should be complemented with markup and an explanation which elements you want to select, since this is what selectors do: Select elements.
Comment: Are you trying to select the selected option of the last `select` element in a `td` with class `listas`?
Comment: If you have control of the html give each select an id. It'll make things much simpler
Answer: I think you want
var lastSelectedOptionElement = $("td.listas select option:selected").last();

First you get all of the selected options, then you take the last of them. Gratuitous live example | source
You might be tempted to do option:last:selected, but that won't work (unless the last option in the list happens to be selected), because the option would have to be both :last and :selected.

Update:
Re your edit:

In this td, I have two select inputs.

If they are not multi-select select elements, to get the selected option element:
opt = $("td.listas select:first option:selected"); // The first one
// or
opt = $("td.listas select:last option:selected");  // The last one -- this is your original selector

If they are multi-select, combine the two techniques above:
// The *last* selected option in the *first* select list:
opt = $("td.listas select:first option:selected").last();

// The *last* selected option in the *last* select list:
opt = $("td.listas select:last option:selected").last();

// The *first* selected option in the *first* select list:
opt = $("td.listas select:first option:selected").first();

// The *first* selected option in the *last* select list:
opt = $("td.listas select:last option:selected").first();

Comment: Should this not be equivalent the to selector the OP has (as long as it is not multi-select)? Works fine here: http://jsfiddle.net/4m3jG/1/. I think the problem is somewhere else.
Comment: @silentw: It's still unclear: What are you actually trying to get? In your comment you've said you want the select (nothing to do with options), but I'm afraid the phrase *"...so I could prop to select all"* is unclear to me.
Comment: @silentw: Are the multi-select? What is the actual end value you're looking for? It's unclear.
Comment: There was a typo in my code. I need to get the second select in a variable, so I could prop to select all. I was missing the `option`
Comment: @FelixKling: Their selector gets the selected option of the last `select` box. It wasn't at all clear from the original question that that was what they wanted. My (first) selector does something different: In a multi-select box, it gets the *last* of the selected options (from what I presumed, in the absense of any data to the contrary, was the only `select` box). It's still completely unclear to me what the OP wants, in light of their edit. I've scattershot it above. :-)
Comment: @silentw: I still don't get it, but if this solved the problem, great! :-)
Answer: Your selector looks correct, where you might be going wrong is getting the value, you just need to add .val() on the end of it.
var lastSelVal = $("td.listas select:last option:selected").val();

Rough demo: jsfiddle.net/Marcel/4m3jG/

jQuery combine selectors


Cannot relate seek bar to text

Question: I am trying to relate my seekbar to the text. With assistance, I tried to do so, but my attempt have been unsuccessful.
Also I want to also include a range seek bar in the below is the code, but not sure how to work that out. Any assistance would be greatly appreciated.
public class ProfileCreation extends Activity {

    private static final int RESULT_LOAD_IMAGE = 1;
    FrameLayout layout;
    Button save;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_creation);
        save = (Button) findViewById(R.id.button2);
        String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
        if (!picturePath.equals("")) {
            ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }

        Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // Locate the image in res >
                Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

                Object image = null;
                try {
                    String path = null;
                    image = readInFile(path);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // Create the ParseFile
                ParseFile file = new ParseFile("picturePath", (byte[]) image);
                // Upload the image into Parse Cloud
                file.saveInBackground();

                // Create a New Class called "ImageUpload" in Parse
                ParseObject imgupload = new ParseObject("Image");

                // Create a column named "ImageName" and set the string
                imgupload.put("Image", "picturePath");

                // Create a column named "ImageFile" and insert the image
                imgupload.put("ImageFile", file);

                // Create the class and the columns
                imgupload.saveInBackground();

                // Show a simple toast message

                SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
                final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);

                seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress,
                                                  boolean fromUser) {
                        // TODO Auto-generated method stub
                        seekBarValue.setText(String.valueOf(progress));
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub
                    }
                });
            }
        });
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                    && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }

        }

        private byte[] readInFile(String path) throws IOException {
            // TODO Auto-generated method stub
            byte[] data = null;
            File file = new File(path);
            InputStream input_stream = new BufferedInputStream(new FileInputStream(
                    file));
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            data = new byte[16384]; // 16K
            int bytes_read;
            while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, bytes_read);
            }
            input_stream.close();
            return buffer.toByteArray();

        }
    }

Updated code
public class ProfileCreation extends Activity {

    private static final int RESULT_LOAD_IMAGE = 1;
    FrameLayout layout;
    Button save;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_creation);
        save = (Button) findViewById(R.id.button2);
        String picturePath = PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
        if (!picturePath.equals("")) {
            ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }

        SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
        final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                seekBarValue.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

        }

        Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // Locate the image in res >
                Bitmap bitmap = BitmapFactory.decodeFile("picturePath");
                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

                Object image = null;
                try {
                    String path = null;
                    image = readInFile(path);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                // Create the ParseFile
                ParseFile file = new ParseFile("picturePath", (byte[]) image);
                // Upload the image into Parse Cloud
                file.saveInBackground();

                // Create a New Class called "ImageUpload" in Parse
                ParseObject imgupload = new ParseObject("Image");

                // Create a column named "ImageName" and set the string
                imgupload.put("Image", "picturePath");

                // Create a column named "ImageFile" and insert the image
                imgupload.put("ImageFile", file);

                // Create the class and the columns
                imgupload.saveInBackground();

                // Show a simple toast message

            }
        });
    }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

            if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                    && null != data) {
                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
                imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            }

        }

        private byte[] readInFile(String path) throws IOException {
            // TODO Auto-generated method stub
            byte[] data = null;
            File file = new File(path);
            InputStream input_stream = new BufferedInputStream(new FileInputStream(
                    file));
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            data = new byte[16384]; // 16K
            int bytes_read;
            while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, bytes_read);
            }
            input_stream.close();
            return buffer.toByteArray();

        }
    }

Answer: Below code snippet should be moved out of onClickListener. Write it in onCreate
 SeekBar seekBar = (SeekBar) findViewById(R.id.seekBarDistance);
    final TextView seekBarValue = (TextView) findViewById(R.id.seekBarDistanceValue);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
            // TODO Auto-generated method stub
            seekBarValue.setText(String.valueOf(progress));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }
    });

Comment: Thanks man. I moved the code around, but can't seem to be having formating issues with curley braces. I tried using source->format and try fixing it, but is still getting an error. I have posted an updated code in my initial post.

Cannot relate seek bar to text


Zapier to look up Twitter URL mentions

Question: Is it possible to create a Zap that will look-up URL mentions on Twitter?
Been scratching my head with this one, since simply entering URL into Zapier's Twitter Search field doesn't seem to do the trick.
Is it at all possible? Twitter uses its URL shortening service t.co on all links posted. Might this be a reason for me not being able to find any mentions of my URL even when I post one myself? Twitter's native search function finds URL just fine, but not Zapier.
Answer: OK, I think I figured it out.
First, use Twitter's special "url" prefix for URL search:

url:amazon - will find URL with the word "amazon" anywhere within it.

Second, most importantly, it looks like the Twitter account I used for testing got ignored after a couple of same URL posts. So be aware of that too.

Zapier to look up Twitter URL mentions


How to divide an interval into geometrically increasing sub-intervals

Question: It'll be obvious when I see the answer, but I'm too tired/thick to figure it out.
If I have a certain time period, and I want to divide it into a given number of geometrically increasing (or decreasing) durations so that the ratio of interval(n)/interval(n-1) is constant, then how do I figure out the ratio and/or the starting duration of the first interval?
For example if you wanted to divide a 15-second period into 4 increasing durations you might get the geometric series 1, 2, 4, 8 (where the ratio is 2, and start duration is 1 second)
I think it's something to do with log(T)/N, where T is the total time and N is the number of divisions, but that's as far as I've got.
Comment: The ratio and start value are not uniquely determined (you have to choose at least one). That is unless you put some additional constraints, such as both being integers or something, but than you don't have guarantee this will have a solution for all $T$. This is just sum of geometric progression $a\frac{r^N-1}{r-1}=T$, so choose $a$ and compute $r$, or vice-versa.
Comment: Thanks, had no idea there was a closed form of the sum of a geometric progression (although I suspected it :-))
Answer: Say you have an interval of length L and you want to divide it into N intervals $I_k$ s.t. $I_k/I_{k-1} =q$ for $k=1,..N-1$. Then the length of each interval can be written as $cq^k, k=0,...,N-1$, where $c>0$ is some constant and $$ L= \sum_{k=0}^{N-1} cq^k = c \frac{1-q^N}{1-q}$$
You would have to solve this for fixed N, which is not trivial when N>2.
Comment: Thanks, I should have specified that either the ratio is given or start duration is known. Then it's a simple calculation to get the other value, now that you've given me the closed form.


Disable Inline Form Errors

Question: I'm encountering the bug described at "Login form error message is redundant and does not make sense". In short, if a user enters invalid credentials once, an inline error message appears on the user login page. When the user enters valid credentials and successfully logs in, the user sees an error message at the top of the screen (e.g., "1 error has been found: username"), but that is no longer correct.
According to this blog post one can disable inline form errors for a particular form by adding $form['#disable_inline_form_errors'] = TRUE; to the form definition, and I have seen that code snippet elsewhere, too. But I can't find any instructions for implementing it. Does anyone have an example?
Answer: The core issue you linked to has a patch that is apparently working, so it may be easier to patch with Composer instead of adding code to a custom module.
But, if you want to use the $form['#disable_inline_form_errors'] = TRUE;, this is what you can do:
First, create a custom module.  You need to follow the instructions on the first four pages:

Getting Started - Background & Prerequisites
Prepare a Module skeleton
Naming and placing your Drupal module
Let Drupal know about your module with an .info.yml file

Then, in the MYMODULE.module file, add this code:
/**
 * Customize the user login form.
 */
function MYMODULE_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['#disable_inline_form_errors'] = TRUE;
}

What this code does is use hook_form_alter to target the user login form.  Lots of Drupal's form behaviors can be customized in this way.
You then need to go to the module administration page and enable your custom module.
Comment: Thanks so much, Patrick! I ended up patching with https://www.drupal.org/project/drupal/issues/2880011#comment-13666816, and it worked! I added `$form['#disable_inline_form_errors_summary'] = TRUE;` to my theme file, and the problem is gone. I appreciate the time you took to write that clear explanation!

Disable Inline Form Errors


McNemar-Bowker exact test in R: difficulty interpreting the results

Question: I am trying to use the McNemar-Bowker test to test the difference in performance for 2 classifiers. Since my input matrix is sparse and the sum of some of the symmetric cells is less than 10, I am trying to use the exact McNemar-Bowker test using nominalSymmetryTest in this way:
data <- c( 0,0,0,0,0,0,0,0,0,0,
       23,253,35,0,0,0,0,0,0,0,
       9,299,1510,329,7,0,0,0,0,0,
       0,1,289,1193,136,3,0,0,0,0,
       0,0,35,403,4437,338,1,0,0,0,
       0,0,0,15,70,692,114,7,1,0,
       0,0,0,0,3,50,87,18,0,0,
       0,0,0,0,1,14,57,35,15,1,
       0,0,0,0,2,2,16,12,1,0,
       0,0,0,1,0,3,31,33,12,3)
rownames <- c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
colnames <- c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
ada_cat <- matrix(data, nrow = 10, ncol = 10, byrow = TRUE)
                  #dimnames=list(rownames,colnames))

nominalSymmetryTest(ada_cat,
                    digits = 3,
                    MonteCarlo = TRUE,
                    exact = TRUE,
                    ntrial = 100000)

The results look like this:
  $Global.test.for.symmetry
  Dimensions p.value
1    10 x 10      NA

$Pairwise.symmetry.tests
    Comparison  p.value p.adjust
1    1/1 : 2/2 2.38e-07 9.12e-07
2    1/1 : 3/3  0.00391 7.49e-03
3    1/1 : 4/4     <NA>       NA
4    1/1 : 5/5     <NA>       NA
5    1/1 : 6/6     <NA>       NA
6    1/1 : 7/7     <NA>       NA
7    1/1 : 8/8     <NA>       NA
8    1/1 : 9/9     <NA>       NA
9  1/1 : 10/10     <NA>       NA
10   2/2 : 3/3 2.12e-53 4.88e-52
11   2/2 : 4/4        1 1.00e+00
12   2/2 : 5/5     <NA>       NA
13   2/2 : 6/6     <NA>       NA
14   2/2 : 7/7     <NA>       NA
15   2/2 : 8/8     <NA>       NA
16   2/2 : 9/9     <NA>       NA
17 2/2 : 10/10     <NA>       NA
18   3/3 : 4/4    0.117 1.92e-01
19   3/3 : 5/5 1.51e-05 3.86e-05
20   3/3 : 6/6     <NA>       NA
21   3/3 : 7/7     <NA>       NA
22   3/3 : 8/8     <NA>       NA
23   3/3 : 9/9     <NA>       NA
24 3/3 : 10/10     <NA>       NA
25   4/4 : 5/5 1.11e-31 8.51e-31
26   4/4 : 6/6  0.00754 1.33e-02
27   4/4 : 7/7     <NA>       NA
28   4/4 : 8/8     <NA>       NA
29   4/4 : 9/9     <NA>       NA
30 4/4 : 10/10        1 1.00e+00
31   5/5 : 6/6  3.3e-43 3.80e-42
32   5/5 : 7/7    0.625 7.99e-01
33   5/5 : 8/8        1 1.00e+00
34   5/5 : 9/9      0.5 6.76e-01
35 5/5 : 10/10     <NA>       NA
36   6/6 : 7/7 6.33e-07 2.08e-06
37   6/6 : 8/8    0.189 2.90e-01
38   6/6 : 9/9        1 1.00e+00
39 6/6 : 10/10     0.25 3.59e-01
40   7/7 : 8/8 7.24e-06 2.08e-05
41   7/7 : 9/9 3.05e-05 7.02e-05
42 7/7 : 10/10 9.31e-10 5.35e-09
43   8/8 : 9/9    0.701 8.49e-01
44 8/8 : 10/10 4.07e-09 1.87e-08
45 9/9 : 10/10 0.000488 1.02e-03

$p.adjustment
  Method
1    fdr

$statistical.method
         Method
1 binomial test

I am having difficulties in understanding them. Can I conclude from these results that the difference in classifier performance is statistically significant/non-significant?
Thank you!
Answer: ILR.
At the time of writing, the nominalSymmetryTest function doesn't return an exact test for the omnibus test.  The webpage (https://rcompanion.org/handbook/H_05.html) has not yet been updated to reflect this.  So that may be confusing, but the documentation for the function is correct.
As @IanCampbell noted, the McNemar-Bowker test will fail when there are zeros in certain places in the data matrix. If you switched to the exact=FALSE option, the function would invoke the mcnemar.test function, and still give you an NA result.
So what do you have?  You have the results of the 2 x 2 McNemar tests from your larger data table.  Since you have used the exact=TRUE option, these are actually calculated using the binomial.test function.  In your case, for example, you have a significant difference from "1" to "2" (or vice-versa; I don't if rows or columns might be a "before" or "after"), and from "1" to "3", and so on.
I don't know of a statistical test like McNemar that will work with a matrix with so many zeros like this.
I also note that you have a large sample size.  I would advise you to not rely too much on p-values, but to also look at some form of effect size statistic.  If you are relying on the 2 x 2 tables, it's easy to calculate the odds ratio.  For example, if the change from "5" to "3" is 35 and the change from "3" to "5" is 7, the odds ratio is 5 or 0.2, depending.
As a final comment, the fact that your categories are labeled "1" to "10" makes me suspect that you should be treating these data as ordered categorical (ordinal) and not nominal categorical, and you should be using a totally different test. (But I don't know without more information).
Comment: Thank you so much for the explanation provided and for the heads-up regarding the p-value reliability! This has been very helpful!
Answer: As noted in the R Companion:

For a 2 x 2 table, the most common test for symmetry is McNemar's test.  For larger tables, McNemar's test is generalized as the McNemar–Bowker symmetry test.  One drawback to the latter test is that it may fail if there are 0's in certain locations in the matrix. McNemar's test may not be reliable if there are low counts in the "discordant" cells.  Authors recommend that these cells to sum to at least 5 or 10 or 25. [Emphasis added]

You're getting NaN statistics and P-values because the following line of stats::mcnemar.test evaluates some elements to Inf:
y[upper.tri(x)]^2/x[upper.tri(x)]
# [1]  21.0434783   7.1111111 207.0928144         Inf   0.0000000   2.4611650         Inf         Inf  17.3571429 131.2727273         Inf         Inf         Inf
#[14]   6.7222222 174.7279412         Inf         Inf         Inf         Inf   0.2500000  24.2012195         Inf         Inf         Inf         Inf   0.0000000
#[27]   1.7142857  19.2533333         Inf         Inf         Inf         Inf   0.5000000   0.0000000  14.0625000   0.1481481         Inf         Inf         Inf
#[40]   0.0000000         Inf   1.3333333  29.0322581  28.2647059  10.0833333

This answer suggested an approach by Evans and Hoenig. This approach is implemented in the fishmethods package. I use igraph to make the matrix transformation convenient.
library(fishmethods)
library(igraph)
pairs <- get.edgelist(graph.adjacency(ada_cat,mode = "directed"))
compare2(pairs,cont.cor = FALSE, twovsone = FALSE, plot.summary = FALSE, barplot = FALSE)
#$thecall
#compare2(readings = get.edgelist(graph.adjacency(ada_cat, mode = "directed")),
#    twovsone = FALSE, plot.summary = FALSE, barplot = FALSE,
#    cont.cor = FALSE)
#
#$McNemar
#     Chisq       pvalue
#1 59.25231 1.387779e-14
#
#$Evans_Hoenig
#     Chisq df pvalue
#1 140.1184  4      0
#
#$difference_frequency
#      difference frequency percentage
# [1,]         -6         1        0.0
# [2,]         -5         0        0.0
# [3,]         -4         5        0.0
# [4,]         -3        34        0.3
# [5,]         -2       126        1.2
# [6,]         -1      1215       11.5
# [7,]          0      8211       77.5
# [8,]          1       985        9.3
# [9,]          2        19        0.2
#[10,]          3         1        0.0
#
#$sample_size
#[1] 10597

Comment: Thank you for your answer, Ian! It's good to know that there are other alternatives out there!


MutableCopy AllocLeak MemoryLeak

Question: I have an NSTimer that fires once per second.
And every second I have an NSString that needs to be changed.
I've never tried to deal with memory management before so I'm not sure if what I'm doing is right but instruments is saying under "alloc" that the line of code with stringByReplacingOccurrencesOfString has 45MB of "Live Bytes" after about a minute...
(and the live byte count keeps on rising with every second and eventually crashes the app).
I think my issue lies somewhere with the MutableCopy code?
Here is my code:
-(void)myTimer {
    if (testedit) {
        [testedit release];
        [withString1a release];
        [forString1a release];
    }
    testedit = [[NSString alloc] init];
    withString1a = [[NSString alloc] init];
    forString1a = [[NSString alloc] init];

    testedit = [[NSString alloc] initWithFormat:@"example"];
    withString1a = [[NSString alloc] initWithFormat:@"e"];//this string gets its values randomly from an array in my real code
    forString1a = [[NSString alloc] initWithFormat:@"flk34j"];//this string gets its values randomly from an array in my real code

    testedit = [[testedit stringByReplacingOccurrencesOfString:withString1a withString:forString1a]  mutableCopy];//memory leak /:

}

Comment: Why are you doing so many allocations? Are you using ARC?
Comment: Did you declare `testedit` as `@property` or is it just a class level variable?
Comment: I made `NSString testedit;` in my .h under `@interface ViewController` but I never made a property.
Comment: @AlbertRenshaw, in case you are still having problems, see my updated answer
Answer: You are allocating memory for each object twice.  When you alloc the second time and assign it to the same variable, the first piece of alloc'd memory becomes inaccessible and unreleasable.
Then you make a mutableCopy of testedit and assign the copy to the original's variable. Again, you leave a piece of inaccessible memory floating around.
The rule with non-ARC memory management is - for every alloc, new, copy or retain you need to have a corresponding release. You have 6 allocs, one copy, and only 3 releases.
Here are some suggestions.
Remove these duplicated allocations:
   testedit = [[NSString alloc] init];
   withString1a = [[NSString alloc] init];
   forString1a = [[NSString alloc] init];

Presumably testedit, withString1a and forString1a are all iVars. (Please declare your iVars as autosynthesized properties and refer to them as self.testedit ... etc. that will make your code so much clearer to stack overflowers).
Take out all of this:
if (testedit) {
        [testedit release];
        [withString1a release];
        [forString1a release];
    }

Assuming these are all iVars, the correct place to release them is in your object's dealloc method
In fact withString1a and forString1a can be local variables, as you get their content from elsewhere:
 NSString*  withString1a = [[[NSString alloc] initWithFormat:@"e"] autorelease];
 NSString*  forString1a =  [[[NSString alloc] initWithFormat:@"flk34j"] autorelease];

You can autorelease them as you don't need them to hang around after the method has finished.
These lines can also be written:
 NSString*  withString1a = [NSString stringWithFormat:@"e"];
 NSString*  forString1a =  [NSString stringWithFormat:@"flk34j"];

(-stringWithFormat is a convenience method that returns an autoreleased object)
That leaves us with these two lines.
  testedit = [[NSString alloc] initWithFormat:@"example"];
  testedit = [[testedit stringByReplacingOccurrencesOfString:withString1a
                                                  withString:forString1a]  mutableCopy];

It's not clear why you are treating testedit as an immutable string in the first line and a mutable string in the second. You don't need a mutable string here at all, as you are replacing testedit with a new string.
 self.testedit = [[NSString alloc] initWithFormat:@"example"];
 self.testedit = [[testedit stringByReplacingOccurrencesOfString:withString1a
                                                  withString:forString1a] copy];

(you need copy as stringByReplacingOccurrencesOfString:withString: returns an autoreleased object, and here you want to keep hold of it)
THE last piece of the jigsaw is getting rid of your _testedit iVar memory allocation. You do this in the dealloc method of your object:
- (void) dealloc {
    [_testEdit release];
    [super dealloc];
}

(Note that init, accessor, and dealloc methods are the three places where you should not refer to an iVar using property syntax.)
All good, but really, you should be using ARC! You are _far_more likely to introduce memory bugs this way than if you rely on the compiler to manage memory for you.
Comment: You dont need to use `self.testedit = [[NSString alloc] initWithFormat:@"example"];
 self.testedit = [[testedit stringByReplacingOccurrencesOfString:withString1a
                                                  withString:forString1a] copy];`. `self.testedit = @"example";` is enough. And why do you need to do a `copy` when your are using `@properties`? Doesn't make any sense.
Comment: @ACB, I know about the redundancy, but I had assumed Albert had put that in as a placeholder for something more meaningful. If I abbreviated it the answer wouldn't have helped the question. Tx for pointing out `copy` error, corrected.
Answer: I would suggest you to make use of @property here.
In .h file declare the properties as:
@property (nonatomic, retain) NSString *testedit;
@property (nonatomic, retain) NSString *withString1a;
@property (nonatomic, retain) NSString *forString1a; //if required write the @synthesize as well in .m class

You can write your timer method as:
-(void)myTimer {

    self.testedit = @"example";
    self.withString1a = @"e";//this string gets its values randomly from an array in my real code
    self.forString1a = @"flk34j";//this string gets its values randomly from an array in my real code
    self.testedit = [self.testedit stringByReplacingOccurrencesOfString:self.withString1a withString:self.forString1a];
}

In dealloc method, you can set all the above properties as nil (self.testedit = nil;) or do a release on them([testedit release];).
If possible, try to switch to ARC, you dont have to worry about the memory management. The problem with your code was that you are using a lot of alloc/init statements without releasing the variable before doing it. This causes it to lose the reference of that variable and you will leak it. You dont need that many allocation statements. For every allocation or retain, there should be a corresponding release/auto-release statement.
Comment: @AlbertRenshaw, You dont need `mutableCopy` at all. You should have used `retain` instead even with your current code in question.
Comment: @AlbertRenshaw, All I can say is, this is the only way you can optimize the memory. I wonder if anyone else can do anything on this matter. Please follow this everywhere and check if it helps. Please accept in that case.
Comment: I switched to ARC and I still got a memory leak with some code somewhere in my HUGE app so I'm just going through and manually managing everything.
Comment: I'll try this. I was wondering how to get rid of that darned `mutableCopy` hehe
Comment: Ah! Brilliant! Okay, I added your properties and synthesized and changed mutableCopy to retain... now I'm running instruments, I'll get back to you!
Comment: @AlbertRenshaw, With the above code, you dont need retain. I was just referring to the question where you used mutableCopy. You could have used retain there but with properties, you dont need it anymore. And dont forget to set these properties as nil in dealloc.
Comment: I was using retain because when I didn't with the above code it crashed... But I forgot to set them nil in dealloc... would that be why?
Comment: That is because `stringByReplacingOccurrencesOfString` returns an autoreleased string and when it executes `[testedit release]` it was causing it to crash. That is why I mentioned as you should have used retain(mutableCopy also does the same) there. You should release only when you have allocated or retained and in this case, the previous testedit was leaked and this new one returned from `stringByReplacingOccurrencesOfString` was autoreleased. So it crashes.
Comment: So to fix it I use `retain]autorelease];` and I get rid of my custom release method?
Comment: That much will not be sufficient. You have to do all the changes mentioned in my answer.
Comment: Okay, I did everything you said in your answer. Do I also get rid of my custom release method? And then I don't use `retain` or `autorelease` at all?
Comment: Yes, with @properties, you dont need those release statements and retain/autorelease at all. Just use `self.testedit = @"example";` and it will release `testedit` and will initialize `testedit` with `@"example"`. Later in dealloc you can set to `self.testedit = nil;` which will internally release `testedit` and then set it to `testedit = nil;`
Comment: It didn't fix my allocations live bytes problem /:
Comment: @AlbertRenshaw, Is it reduced now? You have to do this everywhere in your project.
Comment: Nope /: I don't know what else to try! It's been all day! I might just hire someone to fix my code who lives near me.
Comment: let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23180/discussion-between-acb-and-albert-renshaw)
Comment: I got everything working now... turning on ARC still had memory problems but enabling arc along with removing some of the things like MutableCopy and a mixture of code from various answers got it working.. thank you for all of your help! God bless!
Comment: @AlbertRenshaw, To be frank, that answer you choose as correct answer has lot of mistakes. You dont use `self.testedit = [[NSString alloc] initWithFormat:@"example"];` and it should be `self.testedit = @"example";` Not sure how that got two upvotes.
Answer: You are getting a memory leak because you never de-allocate testedit.  Whenever you call alloc, that means you need to deallocate it.  This usually just means calling release.
Do something like this instead, then be sure to free up the memory you've allocated:
NSString* newString = [[testedit stringByReplacingOccurrencesOfString:withString1a withString:forString1a]  mutableCopy];

Comment: Never call -dealloc directly except [super dealloc] when implementing your own dealloc method. Other than that, only call -release or -autorelease
Comment: How and where do I deallocate it? I tried adding `[testedit dealloc];` to the end of the NSTimer function and the app crashed.
Answer: If you're using ARC you shouldn't have an issue. If you aren't using ARC you can try adding autorelease:
testedit = [[[testedit stringByReplacingOccurrencesOfString:withString1a withString:forString1a]  mutableCopy] autorelease];

Comment: autorelease freezes and crashes the app.

MutableCopy AllocLeak MemoryLeak


Classical vs. Flamenco techniques ... differences?

Question: I was reading some questions here on the website and that made me a bit confused ! about moving from flamenco to classical playing, how easy/hard would it be.
The point is the used techniques difference, are really there new techniques may a flamenco player face difficulty with or may not know when he's moving to classical ?
I suppose that much of if not all of classical techniques are derived from flamenco's, aren't they ?
Actually I don't see that classical guitar has its own techniques !

(Picado, Tremolo, Arpeggio, Legato) are all flamenco techniques and they are mainly used in classical playing !
So can somebody enlighten me with the techniques that classical playing has as its own ? because really I don't see any !
Please point me if I'm wrong.
Answer: Pi or pmi scales, artificial harmonics, open arpeggios (i.e. when ami aren't plucking adjacent strings), muting strings with finger planting. That's a few examples of techniques that hardly ever occur in flamenco but are common in the classical repertoire.
Bear in mind that some of the techniques may be shared by both but aren't exactly the same. The clearest example of that is tremolo. Whereas flamenco players prefer the quintuplet piami, classical players will often prefer 16-notes (pami for example).
The biggest hurdle for a flamenco guitarist moving to classical is probably the esthetics of tone, which are completely different in each style, and this does have an influence on the techniques you develop. Altering the tone by changing the angle of attack or by moving the right hand closer or further away from the sound-hole is pretty uncommon in flamenco. Or being able to single out a lead tone while playing other simultaneous notes. That hardly ever occurs in flamenco but it is very important in classical guitar (for instance, playing ami at the same time but having one note sound out clearer than the others. Very important for classical. Irrelevant for flamenco).
edit: however, the difference between the two has absolutely nothing to do with flamenco being "dance music" as was suggested in another reply. The fundamental basis of flamenco is cante (song) not baile (dance). Forms like seguiriyas are very flexible in their relationship to the pulse. Not to mention the cantes de levante that are usually played without any real pulse at all.
There is a common misconception today that "in compas" means "cuadrado" (steady beat) but these are too very different concepts. Accompanying dance does usually require more of a steady beat. And perhaps the fact that most flamenco shows outside of Spain are dance-centric has created this misconception that flamenco always needs a steady pulse, needs to be "cuadrado". But nothing could be further from the truth.
Comment: Great answer! — I suppose by _pinch harmonics_ you mean normal artificial harmonics: i.e. the left hand fingers a note, the right hand uses one finger to enforce a particular flageolett node, and one finger plucks the string? What I'd understand by the term "pinch" would rather be the electric guitar technique where you partially damp the string at some _not exactly specified_ spot, then agressively pick the string with a plectrum to generate a sharp, thin/screaming spectrum but with a pitch that's still recognised as the fundamental.
Answer: There are some techniques that are exclusive to each. Flamenco has rasgueado and tambura, and a snappy pizzicato that don't really happen in classical repertoire. And in classical you have ... well, I'm sure there's something.
Within the broad overlap, there's still a great difference in style, particularly in the sense of time and tempo. Flamenco is dance music. Even if there isn't a dancer, the idea of dancing should still be conveyed. And this requires a consistent pulse that you can tap your foot to. This is very unlike a more classical application of borrowing time and padding measures and pushing the strong beats, where the music may more-or-less define its own axioms of phrasing.
Comment: Yeah I totally agree about the difference in tempo and rhythm, but the question is what techniques does really the classical playing have? the flamenco seems to me the origin while as you mentioned of course it has some more techniques that I didn't mention as they're not used in classical.


Como inserir apenas alguns dos atributos

Question: estou utilizando JPA com eclipse link, queria inserir apenas alguns dados no banco, alguns dos atributos não são necessário salvar, como eu poderia fazer isso?
Answer: use a annotation @Transient do JPA na propriedade que você não deseja serializar(não persistir), ex:
<code>import javax.persistence.Transient;

@Transient
private String zip;
</code>
Annotation Type Transient


Improper Integrals Convergent or Divergent $\int_1^\infty \frac{2}{x(1+\cos^2(x^2+x+1))}dx$

Question: I have the integral:
$$\int_1^\infty \frac{2}{x(1+\cos^2(x^2+x+1))}dx$$
I could not figure out how to represent the equation in the denominator, so I could apply the limit. I need only to find if it is convergent or divergent, I do not care about the value.
Answer: $$\frac2{x(1 + \cos^2(...))} \ge \frac2{x(1 + 1)} = \frac1x$$


How to calculate adjusted HR?

Question: What is the difference between crude HR and adjusted HR in Cox regression? How do we calculate adjusted HR?
Answer: Crude HR refers to the HR estimate from a cox regression model including only your variable of interest as predictor.
Adjusted HR usually refers to the estimated HR of that variable where other covariates are also included in the cox regression model.
Comment: Right, but it's not multivariate, it's multivariable. Multivariate refers to multiple response variables.
Comment: SO, "CRUDE HR" WE GET IN UNIVARIATE ANALYSIS AND "ADJUSTED HR" - IN MULTIVARIATE?
Comment: Great Thanks for explanation and useful correction!

How to calculate adjusted HR?


How to constrain gradient boosting predictions to be non-negative?

Question: I have a regression problem with the target always nonnegative, I used gbdt as the model, but sometimes the model outputs negative prediction value.Is there any way to output nonnegative value using gbdt, or how to impose nonnegative target(prediction value) constraint for gbdt or any other machine learning model?
Comment: This depends on the software you're using. For example, in [tag:xgboost] you could assume that your response is gamma-distributed and specify `"objective":"reg:gamma"`. You can also probably enforce non-negativity in a more general way using customized objective functions.
Answer: One way is to transform your data in such a way that negative values of your "real" variable are impossible. For instance, suppose you have a response variable Y. Define W(Y) = log(Y), then do a regression with W as your response variable. The take your predicted values, and exponentiate them to get back to Y. It's possible that your model will return a negative value of W, but e^W will always be positive.
Of course, this will raise other issues, such as heteroskedasticity and not being linear (although if you're constraining the results to be positive, then it wasn't linear to begin with). You can play around with other functions; you just need two functions $f$ and $g$ such that they are inverses of each other and one of them has a codomain of only nonnegative numbers. You should think about why you expect Y to always be positive to guide you in deciding what function to use.
Comment: I would be more concerned about the bias we introduce by the naive backtransformation, cf. [Miller (1984, *The American Statistician*)](https://doi.org/10.1080/00031305.1984.10483180).
Comment: In the case of gradient boosting, the linearity and heteroskedacity is not so much a problem.
Comment: we take the log for response only, or for both response and predictor? thanks!


Derivation of triangular number line segment recurrence relation

Question: I was reading about triangular numbers on Wikipedia, where the following statement is made:

The number of line segments between closest pairs of dots in the triangle can be represented in terms of the number of dots or with a recurrence relation:
  $$L_n=3T_{n-1}=3\binom{n}2{}; $$
  $$L_n=L_{n-1}+3(n-1),L_1=0$$
  In the limit, the ratio between the two numbers, dots and line segments is
  $$lim_{n \rightarrow \infty}\frac{T_n}{L_n}=\frac{1}{3}$$

I understand how we get $L_n=L_{n-1}+3(n-1),L_1=0$: for every new side we add, we add $3$ line segments for every triangle that can be formed from that new side, and this comes out to $3(n-1)$ more segments for each increment of $L_n$.
However, I don't understand how $$L_n=3T_{n-1}=3\binom{n}2{} $$ and $$lim_{n \rightarrow \infty}\frac{T_n}{L_n}=\frac{1}{3}$$ were derived.
Answer: For $L_n = 3T_{n-1}$, visualize all the small line segments being drawn. Then you will have many small equilateral triangles, $T_{n-1}$ of which point "upward." Each of the small line segments is an edge of exactly one of these "upward" small equilateral triangles, so there are $3 T_{n-1}$ segments.
For the limit,
$$\frac{T_n}{L_n} = \frac{T_n}{3 T_{n-1}} = \frac{1}{3} \frac{n(n-1)}{(n-1)(n-2)} \to \frac{1}{3}.$$
Comment: How come $T_{n-3}$ of the triangles point "upward"? It's visibly "true", but is there an way to show this? Some trivial combinatorial (or otherwise) proof?
Comment: @Princee The top vertices of these "upward" triangles form a diagram of points corresponding to the picture for the triangular number $T_{n-1}$. Sorry, there was a typo.


Interpreting a confidence interval

Question: Everything I read about confidence intervals warns against the following interpretation: If I construct a 95% confidence interval (a,b) for some parameter p, it is incorrect to say that the probability that p is in (a,b) is .95.
Mathematically, I totally get it. a, b, and p are constants, so the probability is either 1 or 0.
However, philosophically, I don't see the problem with constructing an interval based on a sample and saying that the probability that your interval contains the unknown parameter is .95 since probability is, after all, a consequence of uncertainty in the face of imperfect information.
For example, before I flip a coin, there is a 50% chance it lands on heads. Suppose I flip the coin but I don't look at the outcome. Is it wrong to say that the probability that the coin is currently heads up is 50%, even though it's and 'unknown constant?'
Comment: There is nothing wrong, philosophically, *"with constructing an interval based on a sample and saying that the probability that your interval contains the unknown parameter is .95"*. However, that is not what a frequentist confidence interval is. If you want to construct such an interval, a Bayesian credible interval would be more appropriate,.
Answer: This issue for interpreting a confidence interval in that manner is more that the frequentist statistical theory says the true parameter value is constant so a randomly generated interval will either contain or exclude that fixed parameter value (slightly different look than yours).
If you want to make that kind of probability statement, a Bayesian credible interval will allow for it because it allows parameters to be random variables and via the incorporation of prior information and current information you can make a probability statement about an interval containing a parameter which is a random variable (contrary to a fixed value of a parameter).
The coin question depends on the definition of probability for whoever you ask.
Answer: For a sample as opposed to a population, the mean and the confidence interval are estimates.  Consequently, to obtain a prediction interval as opposed to a confidence interval, you would need to take account of the potential errors in the calculation of the mean and the variance. This reference provides a more complete explanation.
In the specific case of tossing a coin, it would be reasonable to presume that the probability of getting an head or a tail is equal, apart from the incredibly rare case where it lands and stays on its edge.

Interpreting a confidence interval


In OLS is it methodologically correct to use the variance of a variable as an explanatory variable?

Question: Are some OLS assumptions not satisfied if I use the variance of a variable as a proxy of uncertainty in a regression? For instance, would it be methodologically correct if I use moving averages of rainfall and moving averages of the variance of rainfall in a regression were the dependent variable is production at the farm level? I have not found examples of regressions containing the variance of a variable in the X's so I wonder if that is because there are some modeling issues with that approach.
Thank you for your help.
Answer: As long as the standard assumptions of OLS (normality, independence of error and explanatory, iid ) are satisfied, it's not a problem to do it that way. But just be careful how you interpret coefficients to those variable.
Or you can run moving window OLS and allow varying coefficients over time. In this case, you don't need the variance variable.


NumberFormatException: invalid long:"name"

Question: I am getting this error in my android app. When I am going for auto suggestion, I got this error. Why I am getting this error. Please Help me.
FATAL EXCEPTION: main
java.lang.NumberFormatException: Invalid long: "name"
at java.lang.Long.invalidLong(Long.java:125)
at java.lang.Long.parse(Long.java:362)
at java.lang.Long.parseLong(Long.java:353)
at java.lang.Long.parseLong(Long.java:319)
at android.database.MatrixCursor.getLong(MatrixCursor.java:255)
at android.widget.CursorAdapter.getItemId(CursorAdapter.java:220)
at android.widget.AutoCompleteTextView.buildImeCompletions(AutoCompleteTextView.java:1123)
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1082)
at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:971)
at android.widget.AutoCompleteTextView.access$800(AutoCompleteTextView.java:91)
at android.widget.AutoCompleteTextView$PopupDataSetObserver$1.run(AutoCompleteTextView.java:1268)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

Comment: please check "name" field is long or string..?
Comment: That field is String.
Comment: Is that string contains always text or numbers only.
Comment: @bharat Both string and number.
Answer: This will crash since it isn't a long
Long.parseLong("name")

It has to be something like this
Long.parseLong("12345567890")


Xcode target compiled with optimization

Question: I am seeing following warning message, when I build target ABC.
ABC was compiled with optimization - stepping may behave oddly; variables may not be available.
I have made sure about optimization (GCC_OPTIMIZATION_LEVEL = 0), but still build is showing above message.
How to resolve this issue?
Answer: In Xcode go to Product > Scheme > Edit Scheme...
and change the Build Configuration from "Release" to "Debug" (within the "Run" menu under "Info").
Comment: Thanks for help, but it didn't work. I am still seeing same warning message.
Answer: Xcode generally throws this type of error when compiler optimisation level is set other than None in debug mode. To do it follow below steps.
Step 1: Go to your build settings
Step 2: Search for SWIFT_OPTIMIZATION_LEVEL
Step 3: Go to Optimization level in debug mode.
Step 4: Change it into None[-Onone].
Comment: This did not work for me, as None was already selected when the crash happened.
Answer: Try turning off the 'Debug Executable' in scheme settings. But I do not know if this is recommended.

Xcode target compiled with optimization


Chord progression question

Question: Dmaj7 - Emaj7 - Gbmaj7 - Gbmaj7 x repeats
What is this chord progression?
I could not figure out its key and know nothing about this one.
It is from this video.
Answer: Call that F♯maj7 rather than G♭maj7 and it starts to make more sense!
I'm hearing F♯ as the tonal centre.   It's not a functional progression.   Just F♯maj7 approached by a couple of similar-shaped chords  two and one whole step below.   This is called 'planing'. There's a discussion of the technique here:
https://www.secretsofsongwriting.com/2008/09/04/chord-progressions-can-come-alive-with-planing/
As mentioned in a comment, 'planing' became popular with the French impressionist composers, particularly Ravel and Debussy.   It's the antithesis of Common Practice harmony, where parallel 5ths and octaves were avoided.   EVERYTHING's parallel in planing!
Jazz theory tends to obsess on functional harmony, always looking for 'progressions'.  We must remember that one perfectly valid way that a chord can 'fit' is simply being the same shape as the one before it.
Comment: Yes, the French impressionist composers were big on planing!
Comment: Yes, it's easy to 'plane' on guitar. Which is probably how many guitarists first discover it sounds good!   Keyboard players tend instead to discover 'diatonic planing' - Cmaj7, Dm7, Em7, Fmaj7 etc. -  which lies much more easily under their fingers.   True planing is a little harder.
Comment: Also, what scales or chord tones did he use on this chord progression? Or any other scales or mode that would fit well on it.
Comment: Thanks for teaching me the term "planing". I remember being introduced to Ravel's "Pavane pour une infante défunte" which has an example of planing which really stands out.
Comment: Guitars are so good for planing, you might want one in your woodworking tool kit.
Answer: I have a slightly different way to look at this. It's D - E - F#, with F# being the tonal center, and the relevant trick that makes this interesting is alternating between F# major and F# minor sounds. While the D and E chords are playing, it sounds like things from F# minor i.e. A major would fit (with come added twist like the D# in Emaj7), but then every time there's a "surprising" change - instead of F#m you get F#. "Surprising" in quotes, because it's not really surprising at all, switching between parallel major and minor is a very common thing to do in jazzy genres.
Try playing F# minor pentatonic or F# blues licks over it, they should fit just about everywhere, including the F# major chord. Toy around with solo A and A# notes, when the F# major chord is sounding.
"Planing" might describe what happens with the guitar chords, because the chord progression can be played by keeping the same fingering pattern but moving the left hand to a different position. But a practically identical chord progression would be:
Emaj7 - C#m9 - F#maj7
or
Bm9 - C#m9 - F#maj7
or, actually you can even change the F#maj7 and make it, say, D#m9. So
Bm9 - C#m9 - D#m9
or if you like, split the second bar in half:
Emaj7 - C#m9 - F#maj7 - D#m9
And as you can see, these don't have to be played with "planing". Actually, you can EQ out the bass in the video and change the video to one of these other versions by playing the bass notes from above over the video. And all the solos on the video would sound practically the same. The idea is the same during the first two chords, you treat it like being in F# minor, and during the last chords you treat it like being in F# major.
Here's my favourite variation written out with the implied scale changes explicated as key signatures: (I tested that this works perfectly over the video, too bad we can't have audio clips here)

Note that during Emaj7 or C#m9 chords, despite the key signature markings, D is sharpened. That's just one more trick that brings extra flavor. Even over the second bar with six sharps, you can play anything you want there, including E even though there's an E# in the default scale and in the F#maj7 chord. Or A even though there's an A# in the default scale and in the F#maj7 chord. Try all notes over all chords and learn what the combinations taste like. :)
Planing can be a creative technique for coming up with progressions like that. But when you start doing variations of the chord progression to find out what is its harmonic essence, you may find that no planing is needed to play what's essential to the progression. This is probably somewhat subjective, but in my opinion, it doesn't change the "song" at all whether you have E or C# in the bass. Or D vs B. If you're bored with one, use the other one.
Comment: 'Planing' is easy on guitar, not so much on keyboard!   But that's not the point.   It's primarily a musical device that lets you play step-adjacent chords of the same shape without having to worry about them all fitting the same scale or the same key.  The unity comes simply from them being the same shape, and adjacent.
Comment: @LaurencePayne It seems I have to repeat everything I said in the answer. Planing is a technique that can be used to come up with the OP's progression. But planing is not needed for reproducing the essential harmonic elements in that progression. It doesn't need to be planed. The essential thing is not that the chord shapes are exactly the same. They are not, other instruments are playing and the total sound is not only the guitar's planed chords. You could even leave out the whole guitar and play something different and it would be identical. The soloists do not think about planing.
Comment: @LaurencePayne  Planing is not needed for reproducing the end-result. Not as a technique, or in the voicings. The voicings do not have to be planed. The alternative variations I listed work pretty much exactly the same or better. If the progression had been the one I wrote in the notation example, and the OP had asked what's happening here, would you have answered, "planing". What does the soloist think about here? Planing? No. The soloist does not perform planing over the chords, or think about planing when soloing. The relevant thing is switching between F# minor and major.

Chord progression question


detect number of peaks in audio recording

Question: I'm trying to figure out how to detect the number of syllables in a corpus of audio recordings. I think a good proxy might be peaks in the wave file.
Here's what I tried with a file of me speaking in English (my actual use case is in Kiswahili). The transcript of this example recording is: "This is me trying to use the timer function. I'm looking at pauses, vocalizations." There are a total of 22 syllables in this passage.
wav file: https://www.dropbox.com/s/koqyfeaqge8t9iw/test.wav?dl=0
The seewave package in R is great, and there are several potential functions. First things first, import the wave file.
library(seewave)
library(tuneR)
w <- readWave("YOURPATHHERE/test.wav")
w
# Wave Object
# Number of Samples:      278528
# Duration (seconds):     6.32
# Samplingrate (Hertz):   44100
# Channels (Mono/Stereo): Stereo
# PCM (integer format):   TRUE
# Bit (8/16/24/32/64):    16

The first thing I tried was the timer() function. One of the things it returns is the duration of each vocalization. This function identifies 7 vocalizations, which is far short of 22 syllables. A quick look at the plot suggests that vocalizations do not equal syllables.
t <- timer(w, threshold=2, msmooth=c(400,90), dmin=0.1)
length(t$s)
# [1] 7

I also tried the fpeaks function without setting a threshold. It returned 54 peaks.
ms <- meanspec(w)
peaks <- fpeaks(ms)

This plots amplitude by frequency rather than time. Adding a threshold parameter equal to 0.005 filters out noise and reduces the count to 23 peaks, which is pretty close to the actual number of syllables (22).

I'm not sure this is the best approach. The result will be sensitive to the value of the threshold parameter, and I have to process a big batch of files. Any better ideas about how to code this to detect peaks that represent syllables?
Comment: This is a very interesting question, but you might get better help on methods over at the [Stack Exchange Signal Processing Q&A site](http://dsp.stackexchange.com/).
Comment: Just an idea, but would it be worthwhile to consider undertaking [change point analysis](http://www.variation.com/cpa/tech/changepoint.html)? The analysis can be undertaken easily in **R** with use of the [**`changepoint`**](https://cran.r-project.org/web/packages/changepoint/changepoint.pdf) package. Simply put, the change point analysis focuses on *detecting change,* the linked example is concerned with trade data but it could be interesting to apply this technique to sound data.
Comment: I'm going to accept the answer that has the most votes, which happens to be my attempt to implement another CV idea. I think the core question remains however: how to use features of the recordings to accurately detect a number of peaks that corresponds to the number of syllables spoken. Thank you for all of the ideas. I will post back here when I have a solution.
Comment: ok, thanks. will check it out if no one responds. much appreciated.
Answer: I don't think what follows is the best solution, but @eipi10 had a good suggestion to check out this answer on CrossValidated. So I did.

A general approach is to smooth the data and then find peaks by comparing a local maximum filter to the smooth.

The first step is to create the argmax function:
argmax <- function(x, y, w=1, ...) {
  require(zoo)
  n <- length(y)
  y.smooth <- loess(y ~ x, ...)$fitted
  y.max <- rollapply(zoo(y.smooth), 2*w+1, max, align="center")
  delta <- y.max - y.smooth[-c(1:w, n+1-1:w)]
  i.max <- which(delta <= 0) + w
  list(x=x[i.max], i=i.max, y.hat=y.smooth)
}

Its return value includes the arguments of the local maxima (x)--which answers the question--and the indexes into the x- and y-arrays where those local maxima occur (i).

I made minor modifications to the test plotting function: (a) to explicitly define x and y and (b) to show the number of peaks:
test <- function(x, y, w, span) {
  peaks <- argmax(x, y, w=w, span=span)

  plot(x, y, cex=0.75, col="Gray", main=paste("w = ", w, ", span = ",
                                              span, ", peaks = ",
                                              length(peaks$x), sep=""))
  lines(x, peaks$y.hat,  lwd=2) #$
  y.min <- min(y)
  sapply(peaks$i, function(i) lines(c(x[i],x[i]), c(y.min, peaks$y.hat[i]),
                                    col="Red", lty=2))
  points(x[peaks$i], peaks$y.hat[peaks$i], col="Red", pch=19, cex=1.25)
}

Like the fpeaks approach I mentioned in my original question, this approach also requires a good deal of tuning. I won't know the "right" answer (i.e., the number of syllables/peaks) going into this, so I'm not sure how to define a decision rule.
par(mfrow=c(3,1))
test(ms[,1], ms[,2], 2, 0.01)
test(ms[,1], ms[,2], 2, 0.045)
test(ms[,1], ms[,2], 2, 0.05)

At this point fpeaks seems a little less complicated to me, but still not satisfying.
Comment: It might be unsatisfying because your loess parameters do not do enough smoothing.  The choice of smoother needs to be guided by the nature of the data and the objectives; it is not something to be left to whatever is offered by the computing platform and the default values it supplies.
Comment: These are not defaults. Just examples. I'm puzzled by the larger challenge of unsupervised learning in this case. I don't know the number of syllables in the recordings, so I'm not sure how to tune a batch of files. Constant parameters probably don't make sense, but I'm not sure how to set up some other decision rules (e.g., other metrics of the wave that could be used to determine optimal values for these parameters). I'm thinking I need to create a training set that helps some algorithm set these parameters. Not sure though.
Comment: In your command to `loess`, I see no arguments explicitly given for the degree of smoothing.  Actually, there's little point to running loess over a moving window: it already does that internally.
Comment: I see your point. I assumed that `w` was an argument in the smoothing. This is [how the author of the original solution](http://stats.stackexchange.com/questions/36309/how-do-i-find-peaks-in-a-dataset) described the function: "There are two parameters to be tuned to the circumstances: w is the half-width of the window used to compute the local maximum...Another--not explicit in this code--is the span argument of the loess smoother."
Comment: That author included `w` as one of the parameters because he had in mind a very general approach in which the smoother might not be loess but perhaps would be a windowed median, or Hanning, or anything else deemed appropriate for the statistical behavior of the data and the objectives of the analyst. The properties of many of those smoothers would depend on the width of the window.
Comment: That's helpful. Thanks. I need to better understand the parameters. That said, I think my earlier comment on the unsupervised nature of the task is still my main source of confusion.
Comment: Although the task may be unsupervised, it is likely that much can be learned from a few representative sets of data about common statistical features of all future circumstances, which you can exploit in your choice of smoother.  "Unsupervised" does not have to mean "mindless" (although I'm sure you didn't intend to imply that)!
Answer: I had similar problems to analyse protein electrophoresis profiles.
I solved them by applying some of the functions of the msprocess R package on the second derivates of the profiles (see https://fr.wikipedia.org/wiki/D%C3%A9pouillement_d'une_courbe#Position_et_hauteur_du_pic). This has been published here:
http://onlinelibrary.wiley.com/doi/10.1111/1755-0998.12389/abstract;jsessionid=8EE0B64238728C0979FF71C576884771.f02t03
I have no idea whether similar solution can work for you.
Good luck
Comment: thanks, @user17493.bis. kudos to you for publishing with supplemental material. will make it so much easier for me to give this idea a try!
Answer: I would like to suggest a solution utilising the changepoint package. The simplistic example below attempts to identify peaks, defined here as change points by looking at one channel from the available data.
Example
Data sourcing
# Libs
library(seewave)
library(tuneR)

# Download
tmpWav <- tempfile(fileext = ".wav")
download.file(url = "https://www.dropbox.com/s/koqyfeaqge8t9iw/test.wav?dl=0",
              destfile = tmpWav)

# Read
w <- readWave(filename = tmpWav)

Data preparation
# Libs
require(changepoint)

# Create time series data for one channel as an example
leftTS <- ts(data = w@left)

## Preview
plot.ts(leftTS)

Chart generated via the plot.ts call:

Change-point analysis
The changepoint package provides a number of option for identifying changes/peaks in the data. The code below provides only a simple example of finding 3 peaks using BinSeg method:
# BinSeg method (example)
leftTSpelt <- cpt.var(data = leftTS, method = "BinSeg", penalty = "BIC", Q = 3)
## Preview
plot(leftTSpelt, cpt.width = 3)

Obtained chart:

It is also possible to get values:
cpts(leftTSpelt)
[1]  89582 165572 181053

Side notes
The provided example is mostly concerned with illustrating how the change point analysis can be applied to the provided data; caution should be exercised with respect to parameters passed to the cp.var function. A detailed explanation of the package and the available functionalities is given in the following paper:
Killick, Rebecca and Eckley, Idris (2014) changepoint:an R package for changepoint analysis. Journal of Statistical Software, 58 (3). pp. 1-19.
ecp
ecp, is another worth mentioning R package. The ecp facilitates undertaking non-parametric multivariate change point analysis, which may be useful if the one would like to identify change points occurring across multiple channels.
Comment: Thanks, @konrad. I did not know about either package, so thanks for taking the time to demo. I think the fundamental challenge I have with all of these packages is that I do not know how many peaks to look for, so I'm not sure how to tune the parameters. This still seems like a situation where I have to use some algorithm to determine how to set the parameters to accurately identify the correct number of peaks (i.e., syllables).
Comment: @EricGreen On principal the change point analysis would enable you to identify your peaks just by looking at the distribution. It would be a matter of applying a suitable method, penalties and so on. I would suggest that you have a look at the website linked in my previous comment as it outlines the process in detail.
Comment: I'm not sure if you literally mean eyeballign the distribution. I have 2000 files and need a way to automate this. Even if I could examine each file, I find it hard to see the number of syllables as peaks. Maybe I am being dense and I'll come to see the merits of this approach. I'm still stuck on needing to a way to auto tune the parameters of each file so the resulting number of peaks detected is an accurate proxy for the number of syllables.
Comment: @EricGreen No, not literary of course. If you figure out on the appropriate parameters that should be passed to one of the *cpt* functions you will be able to run it across any number of objects. As I've no expertise in linguistics I don't know whether syllables would correspond to the usual peaks observed on time series data.
Comment: gotcha. I think I'm stumbling on the "figure out the appropriate parameters" step for this particular use case. But I've appreciated all of the ideas and learned about a few new packages that could be good alternatives to the ones I tried.
Comment: @EricGreen Ideally, if you have a file for which you know how many peaks you would expect, you could attempt to find configuration that yields the desired results and then apply it across the remaining files.
Comment: I think you're right. I'm planning to create a test set of files and count the syllables. I'm just not certain yet how to train an algorithm to set the optimal parameters for detecting a number of peaks that corresponds to the syllable count.
Answer: Here is a library in Python I used earlier while trying to estimate periodicity by finding peaks in the autocorrelation function.
It uses first-order differences/discrete derivatives for peak detection and supports tuning by threshold and minimum distance (between consecutive peaks) parameters. One can also enhance the peak resolution using Gaussian density estimation and interpolation (see link).
It worked quite well out of the box for me without much tweaking, even for noisy data. Give it a try.
Comment: Thanks, @tool.ish. It looks like a good alternative to the R methods I cited. I think I'd still have the tuning challenge, however.


Columns values as row heading

Question: I have a table with partners details, like [ID], [PartnerName],[webaddress], [short description] etc.
In another table i have stored Social addresses the schema of this table looks like:

My desired output is
ID, PartnerName, webaddress, [short description] from partners table and [Twitter], [Facebook], [LinkedIn] from social table
the values available in Name columns should come in the column heading of my output table for that particular partner
Can we achieve this via sql query?
Comment: select case when name = 'twitter' then value end as twitter, case when name = 'linkedin' then value end as linkedin....
Comment: i was also thinking the same...but suppose for x partner there is a new social address say instagram, in that case i need to write a new case. I want to skip these case. what i want is - whatever is present for a particular Partner in social table, should come in heading - values pairs.
Comment: Please give expected result.
Answer: Please use the PIVOT functionality to do as you desire.
You can see the answers on this question: Convert Rows to columns using 'Pivot' in SQL Server
You can also read the MSDN documentation on how to pivot a table: https://msdn.microsoft.com/en-us/library/ms177410.aspx
Answer: You can use pivot, but if you do not know the name of the social sites and it may vary as they are added, you would need to do something like this by using dynamic sql. Note that it assumes one distinct address per partner per social type in order to use pivot.
create table #partners (
    ID int
    ,PartnerName nvarchar(100)
    ,WebAddress nvarchar(100)
    ,ShortDescription nvarchar(100)
)

create table #social  (
    ID int
    ,Name nvarchar(100)
    ,Value nvarchar(100)
    ,PartnerID int
    ,IsActive int
)

insert into #partners
values (1, 'Bob Jones', 'www.bobjones.com', 'bob desc')
,(2, 'Mike Smith', 'wwww.mikesmith.com', 'mike desc')

insert into #social
values (1, 'Facebook', 'www.facebook.com/bobjones', 1,1)
,(2, 'Twitter', 'wwww.twitter.com/bobjonestweets', 1,1)
,(3, 'Linkedin','wwww.linkedin.com/msmith', 2,1)
,(4, 'Facebook', 'www.facebook.com/smitty',2,1)

declare @columns nvarchar(max) = (
    select '[' + Name + '],'
    from (select distinct Name from #social ) Name
    for xml path (''))

declare @SQL nvarchar(max)
set @SQL = 'select *
            from (
                    select p.ID, PartnerName, WebAddress, ShortDescription, Name, Value
                    from #partners p
                    join #social s on p.ID = s.PartnerID
                    where s.IsActive = 1
            ) s
            pivot ( max(Value) for [Name] in (' + left(@columns, len(@columns)-1) +
            ')) as piv'

exec sp_executesql @SQL

Comment: Thank You @MPR, i am getting the expected result.

Columns values as row heading


(Possible) application of Sarason interpolation theorem

Question: This question is related to the following Wikipedia article on Nevanlinna–Pick interpolation. At the end it has been written as

Pick–Nevanlinna interpolation was introduced into robust control by Allen Tannenbaum.

I do not have the access of the paper or even if I had, without any basic knowledge on control theory, I would, perhaps,  not be able to understand it.
There is a operator theoretic avatar of the above problem, called Sarason interpolation theorem. I am interested in its finite dimensional version. Can anybody help me in understanding, how it can be used in control theory from a mathematical point of view or give some relevant references? I do not have any knowledge of control theory. This is only for my curiosity. Advanced thanks for any help/suggestion.
Answer: The simplest linear control theory deals with time signals on $[0,\infty)$, and linear differential/integral operators with constant coefficients applied to those signals. After Laplace transforming functions in $L^{2}[0,\infty)$ ($L^{2}$=finite energy,) you end up with functions which are holomorphic on the right half plane with square-integrable boundary functions along the imaginary axis (Paley-Wiener Theorem.) Equivalently, a function $f$ is the Laplace transform of an $L^{2}$ function iff it is holomorphic on the right half plane and has uniformly bounded $L^{2}$ norms on all lines in the right half plane which are parallel to the imaginary axis. Transfer functions become multipliers in this context because the Laplace transform turns the operators into multipliers.
Stability requires that the transfer functions not have poles in the right half plane, or you end up with instabilities that show when you try to evaluate the inverse Laplace transform using residues. These undamped terms $e^{rt}e^{i\omega t}$ for $r > 0$ are unacceptable; they correspond to undamped resonances or positive feedback loops. So, one typically wants to deal with multiplication by functions which are holomorphic on the right half-plane. That's part of the control of control theory.
Apparently, the desire to control is then cast in terms of finding holomorphic multipliers with positive real part on the right half plane that have prescribed values at a certain finite number of points in the right half-plane. So you're dealing with multiplier functions that map the right half plane to the right half plane and take prescribed values. By a simple Cayley transform, this can be recast in terms of finding holomorphic functions that map the unit disk into the unit disk and which take prescribed values at a certain finite number of points. That is the Nevanlinna-Pick problem.
The conditions under which such bounded holomorphic functions exist are known in terms of the Pick matrix associated with the points $\{ z_{j}\}_{j=1}^{N}$ in the unit disk and the prescribed values $\{ w_{j}\}_{j=1}^{N}$ at those points. Of course, determining when such a Pick matrix is positive semidefinite isn't so easy either, but at least the criteria is something that can be checked, and it leads to a solution of the stated control problem.
Sarason's work deals with these problems in an operator theoretic framework. The space is the classical Hardy space $H^{2}(D)$ of functions which are holomorphic in the unit disk $D$, and whose $L^{2}$ norms on concentric circles remain uniformly bounded for all radii $0 < r < 1$. This setting is equivalent to the Paley-Wiener setting of holomorphic functions on the right half-plane with uniformly bounded $L^{2}$ norms on lines parallel to the imaginary axis. He then deals with bounded multiplier functions which are holomorphic inside the disk and which take prescribed values at a prescribed number of points inside the disk.


Delphi x DLL x Resources

Question: Estou criando uma DLL em DELPHI, dentro dela existe algumas funções que meu sistema vai usar. Gostaria de saber o seguinte, existe uma forma de embutir essa DLL criada em DELPHI direto no EXE através de RESOURCES ? Tendo em vista que preciso criar uma PROCEDURE no FORM, como fazer para extrair essa DLL antes do form "subir" ?
Estou usando o seguinte para subir a DLL:
procedure DllMessage; external 'teste.dll'

Gostaria de embutir essa DLL direto no executável.
Comment: @EProgrammerNotFound, na verdade é o seguinte. Algumas funções vão rodar em DLL, para não ter que entregar o projeto para o cliente com várias DLL, gostaria de colocar no EXE (usando resource) e quando o form for criado (CREATE FORM) ele descompacta essas DLL, entende ? é possível será ? as DLL são criadas no próprio delphi mesmo.
Comment: Até é possível, mas porque não utilizar o bom e velho instalador? Ou compactar em um arquivo zip?
Comment: EProgrammerNotFound, porque quero entregar somente o executável para o cliente abrir e pronto. Enfim, por vários outros motivos aqui da empresa, como seria possível fazer isso ? poderia me ajudar ?
Comment: @EProgrammerNotFound, isso que eu queria fazer mesmo, porque se eu usar o static linking, não vai subir nunca mesmo rsrss, eu queria usar algo como LOADLIBRAY mesmo, poderia me dar essa força ?
Comment: Obrigado amigo @EProgrammerNotFound, eu tentei fazer usando FREELIBRAY, mas, não sei como eu chamo a função dentro da DLL usando ele rsrsrs.
Comment: Qual a versão do Delphi que você está utilizando? Em novas versões você pode utilizar a keyword delayed e talvez possa manter o código da forma atual. Desculpe-me por não responder mais cedo, estive ocupado no trabalho, estou no horário do almoço e escrevendo uma solução pra você
Comment: @EProgrammerNotFound, eu uso o DELPHI XE7.
Comment: Qual a diferença entre usar o instalador que irá extrair a DLL e você fazer isso ? O arquivo de qualquer forma não vai ficar na pasta ? Isso de qualquer forma não iria contra "os motivos da empresa"?
Answer: Algumas etapas são necessárias para efetuar o procedimento desejado. Em primeiro lugar, vamos embutir a dll dentro de um arquivo .res:
Primeiramente crie sua dll e exporte-a para algum lugar de fácil acesso, para que você possa utilizar o caminho mais facilmente para gerar o arquivo .res.

Criando um arquivo com a extensão .rc:
1.1 Abra o notepad ou qualquer editor de textos e mande salvar com a
extensão .rc, por exemplo: DLL_Resoures.rc
Inserindo as informações necessárias:
2.1 No seu arquivo RC adicione as seguintes informações:
 IdentificadorDLL RCDATA "NomeArquivoDll.dll"

O Identificador deve ser um nome que não se repita para cada dll,
RCDATA especifica que serão dados crus.
O nome do arquivo da dll, deve conter um caminho válido para que o compilador encontre essa dll no seu Disco.

Compilando o arquivo .rc:
Para compilar o arquivo .rc, procure pelo executável BRCC32.exe dentro da pasta bin do delphi, ou abra-o pelo prompt de comandos. Pelo prompt, utilize o comando:
BRCC32.exe DLL_Resoures.rc

O Compilador de resources irá gerar um arquivo .res com o mesmo nome do arquivo .rc por padrão, esse nome será utilizado para inserir dentro do seu executável

Configurando o executável:
4.1 Você deve definir um local para esse arquivo RES para evitar ter que controlar manualmente toda vez que fizer alterações na DLL, para facilitar o exemplo, apenas copie o .RES para dentro da pasta da aplicação EXE e adicione a seguinte linha no início do projeto EXE:
program SeuPrograma;
uses
X, Y, Z;
{$R *.res}
(* Adicione seu RES aqui *)
{$R DLL_RESOURCES.res}
.....

A diretiva {$R} permite você especificar um caminho absoluto e relativo para o arquivo, portanto, se você definir outro lugar para o res, apenas configure um caminho diferente, por exemplo:
    (* Irá buscar o res um diretório acima *)
    {$R ..\DLL_RESOURCES.res}

Extraindo o RES e salvando a DLL:
5.1 Crie a seguinte função:
procedure ExtrairDLL(const NomeResource, NomeArquivo: String);
var
  RStream: TResourceStream;
begin
  RStream:= TResourceStream.Create(HInstance, NomeResource, RT_RCDATA);
  try
    RStream.Seek(0, soFromBeginning);
    RStream.SaveToFile(NomeArquivo);
  finally
    RStream.Free;
  end;
end;

5.2 Na criação do seu formulário chame-a passando os seguintes argumentos:
procedure TForm1.FormCreate(Sender: TObject);
begin
  ExtrairDLL(  (* Nome da Resource: Mesmo nome que você colocou no arquivo DLL_RESOURCES.RC *)
               'IdentificadorDLL',
               (* - IncludeTrailingPathDelimiter: Incluiu uma / (barra) no   final do caminho
                  - ExtractFilePath: Retorna apenas o caminho do nome de   um arquivo
                  - ParamStr(0): Recupera o nome do executável: É o primeiro parâmetro do executável *)
                 IncludeTrailingPathDelimiter(ExtractFilePath(ParamStr(0))) + 'NomeArquivoDLL.dll'
            );
end;

Como você está na versão XE7 você pode apenas adicionar delayed no final dos seus métodos external. Como você sempre extrairá os arquivos antes de usar, não terá problema para rodar a aplicação
procedure DllMessage; external 'teste.dll' delayed;

Comment: Muito obrigado @EProgrammerNotFound, deu tudo certo.

Delphi x DLL x Resources