Arquivo amr convertido de wav diferente do original

Question: Estou convertendo um arquivo wav para amr utilizando a biblioteca amrInputStream, mas o arquivo original Wav tem 15s 387ms de duração com o tamanho de 1.3Mb e o arquivo gerado AMR tem 133kb mas fica com 1m 25s de duração, além de ficar lento e distorcido na reprodução. O que estou fazendo errado?
Aqui está o codigo da classe AmrInputStream
package android.media;

import java.io.InputStream;
import java.io.IOException;

/**
 * AmrInputStream
 * @hide
 */
public final class AmrInputStream extends InputStream
{
    static {
        System.loadLibrary("media_jni");
    }

    private final static String TAG = "AmrInputStream";

    // frame is 20 msec at 8.000 khz
    private final static int SAMPLES_PER_FRAME = 8000 * 20 / 1000;

    // pcm input stream
    private InputStream mInputStream;

    // native handle
    private long mGae;

    // result amr stream
    private final byte[] mBuf = new byte[SAMPLES_PER_FRAME * 2];
    private int mBufIn = 0;
    private int mBufOut = 0;

    // helper for bytewise read()
    private byte[] mOneByte = new byte[1];

    /**
     * Create a new AmrInputStream, which converts 16 bit PCM to AMR
     * @param inputStream InputStream containing 16 bit PCM.
     */
    public AmrInputStream(InputStream inputStream) {
        mInputStream = inputStream;
        mGae = GsmAmrEncoderNew();
        GsmAmrEncoderInitialize(mGae);
    }

    @Override
    public int read() throws IOException {
        int rtn = read(mOneByte, 0, 1);
        return rtn == 1 ? (0xff & mOneByte[0]) : -1;
    }

    @Override
    public int read(byte[] b) throws IOException {
        return read(b, 0, b.length);
    }

    @Override
    public int read(byte[] b, int offset, int length) throws IOException {
        if (mGae == 0) throw new IllegalStateException("not open");

        // local buffer of amr encoded audio empty
        if (mBufOut >= mBufIn) {
            // reset the buffer
            mBufOut = 0;
            mBufIn = 0;

            // fetch a 20 msec frame of pcm
            for (int i = 0; i < SAMPLES_PER_FRAME * 2; ) {
                int n = mInputStream.read(mBuf, i, SAMPLES_PER_FRAME * 2 - i);
                if (n == -1) return -1;
                i += n;
            }

            // encode it
            mBufIn = GsmAmrEncoderEncode(mGae, mBuf, 0, mBuf, 0);
        }

        // return encoded audio to user
        if (length > mBufIn - mBufOut) length = mBufIn - mBufOut;
        System.arraycopy(mBuf, mBufOut, b, offset, length);
        mBufOut += length;

        return length;
    }

    @Override
    public void close() throws IOException {
        try {
            if (mInputStream != null) mInputStream.close();
        } finally {
            mInputStream = null;
            try {
                if (mGae != 0) GsmAmrEncoderCleanup(mGae);
            } finally {
                try {
                    if (mGae != 0) GsmAmrEncoderDelete(mGae);
                } finally {
                    mGae = 0;
                }
            }
        }
    }

    @Override
    protected void finalize() throws Throwable {
        if (mGae != 0) {
            close();
            throw new IllegalStateException("someone forgot to close AmrInputStream");
        }
    }

    //
    // AudioRecord JNI interface
    //
    private static native long GsmAmrEncoderNew();
    private static native void GsmAmrEncoderInitialize(long gae);
    private static native int GsmAmrEncoderEncode(long gae,
                                                  byte[] pcm, int pcmOffset, byte[] amr, int amrOffset) throws IOException;
    private static native void GsmAmrEncoderCleanup(long gae);
    private static native void GsmAmrEncoderDelete(long gae);

}

Aqui está o codigo da classe Recorder que uso para gravar o audio do microfone, gerar o aquivo Wav e converter em AMR
package iupi.com.br.calls;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;

import android.util.Log;

import android.media.AmrInputStream;

/**
 * Created by Bernardo on 29/06/2015.
 */
public class Recorder {

    //Begin private fields for this class
    private AudioRecord recorder;

    private static final int RECORDER_BPP = 16;
    private static final String AUDIO_RECORDER_FILE_EXT_WAV = "Audio";
    private static final String AUDIO_RECORDER_FILE_EXT = ".wav";
    private static final String AUDIO_ENCODED_FILE_EXT = ".amr";
    private static final String AUDIO_RECORDER_FOLDER = "files";
    private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.3gp";
    private static final int RECORDER_SAMPLERATE = 44100;
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private static final int RECORDER_CHANNELS_INT = 1;

    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

    private int bufferSize = 200000;
    short[] buffer;
    private Thread recordingThread = null;
    private boolean isRecording = false;
    private String path;

    //Constructor
    public Recorder(String path)
    {
           int bufferSize = AudioRecord.getMinBufferSize(RECORDER_SAMPLERATE,
                RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING);
        this.path = path;

        System.out.println("BUFFER SIZE VALUE IS " + bufferSize);

        int buffercount = 4088 / bufferSize;
        if (buffercount < 1)
            buffercount = 1;
        recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
                RECORDER_SAMPLERATE, RECORDER_CHANNELS,
                RECORDER_AUDIO_ENCODING, 44100);

        //recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        //recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);

    }

    public void start() throws IllegalStateException, IOException
    {

        buffer = new short[4088];

        recorder.startRecording();

        isRecording = true;

        recordingThread = new Thread(new Runnable()
        {
            @Override
            public void run() {
                writeAudioDataToFile();
            }
        }, "AudioRecorder Thread");

        recordingThread.start();
    }

    public void stop()
    {
        Log.i("SmartMontor","Parando a gravacao do audio");
        stopRecording();
    }

    public boolean isRecording()
    {
        if(recorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING)
            return true;
        else
            return false;
    }

    private void stopRecording() {
        // stops the recording activity

        if (null != recorder) {
            isRecording = false;

            recorder.stop();

            recorder.release();

            recorder = null;
            recordingThread = null;}
        // copy the recorded file to original copy & delete the recorded copy
        copyWaveFile(getTempFilename(), getFilename()+AUDIO_RECORDER_FILE_EXT);
        deleteTempFile();
        convertWaveToAmr(getFilename()+AUDIO_RECORDER_FILE_EXT);
    } // stores the file into the SDCARD
    private String getFilename() {
        System.out.println("---3---");
        //String filepath = Environment.getExternalStorageDirectory().getPath();
        File file = new File(path, AUDIO_RECORDER_FOLDER);

        if (!file.exists()) {
            file.mkdirs();
        }

        return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_FILE_EXT_WAV);
    }

    private void deleteTempFile() {
        File file = new File(getTempFilename());
        Log.i("SmartMontor","Deletando o arquivo temporario");

        file.delete();
    }

    private void copyWaveFile(String inFilename, String outFilename) {
        System.out.println("---8---");
        Log.i("SmartMontor", "Copiando o arquivo Wav na pasta");
        FileInputStream in = null;
        FileOutputStream out = null;
        long totalAudioLen = 0;
        long totalDataLen = totalAudioLen + 36;
        long longSampleRate = RECORDER_SAMPLERATE;
        int channels = RECORDER_CHANNELS_INT;
        long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels / 8;

        byte[] data = new byte[bufferSize];

        try {
            in = new FileInputStream(inFilename);
            out = new FileOutputStream(outFilename);
            totalAudioLen = in.getChannel().size();
            totalDataLen = totalAudioLen + 36;

            //Controller.doDoc("File size: " + totalDataLen, 4);

            WriteWaveFileHeader(out, totalAudioLen, totalDataLen,
                    longSampleRate, channels, byteRate);
            byte[] bytes2 = new byte[buffer.length * 2];
            ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN)
                    .asShortBuffer().put(buffer);
            while (in.read(bytes2) != -1) {
                out.write(bytes2);
            }

            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // stores the file into the SDCARD
    private String getTempFilename() {
        // Creates the temp file to store buffer
        System.out.println("---4-1--");
        //String filepath = Environment.getExternalStorageDirectory().getPath();
        System.out.println("---4-2--");
        File file = new File(path, AUDIO_RECORDER_FOLDER);
        System.out.println("---4-3--");

        if (!file.exists()) {
            file.mkdirs();
        }

        File tempFile = new File(path, AUDIO_RECORDER_TEMP_FILE);
        System.out.println("---4-4--");

        if (tempFile.exists())
            tempFile.delete();
        System.out.println("---4-5--");
        return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
    }
    private void writeAudioDataToFile() {

        // Write the output audio in byte
        byte data[] = new byte[bufferSize];

        String filename = getTempFilename();
        //
        FileOutputStream os = null;
        //
        try {
            //
            os = new FileOutputStream(filename);
            //
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        int read = 0;

        // if (null != os) {
        while (isRecording) {
            // gets the voice output from microphone to byte format
            recorder.read(buffer, 0, buffer.length);
            // read = recorder.read(data, 0, 6144);

            if (AudioRecord.ERROR_INVALID_OPERATION != read) {
                try {
                    // // writes the data to file from buffer
                    // // stores the voice buffer

                    // short[] shorts = new short[bytes.length/2];
                    // to turn bytes to shorts as either big endian or little
                    // endian.
                    // ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);

                    // to turn shorts back to bytes.
                    byte[] bytes2 = new byte[buffer.length * 2];
                    ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN)
                            .asShortBuffer().put(buffer);

                    os.write(bytes2);
                    //  ServerInteractor.SendAudio(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void WriteWaveFileHeader(FileOutputStream out, long totalAudioLen,
                                     long totalDataLen, long longSampleRate, int channels, long byteRate)
            throws IOException {
        System.out.println("---9---");
        byte[] header = new byte[4088];

        header[0] = 'R'; // RIFF/WAVE header
        header[1] = 'I';
        header[2] = 'F';
        header[3] = 'F';
        header[4] = (byte) (totalDataLen & 0xff);
        header[5] = (byte) ((totalDataLen >> 8) & 0xff);
        header[6] = (byte) ((totalDataLen >> 16) & 0xff);
        header[7] = (byte) ((totalDataLen >> 24) & 0xff);
        header[8] = 'W';
        header[9] = 'A';
        header[10] = 'V';
        header[11] = 'E';
        header[12] = 'f'; // 'fmt ' chunk
        header[13] = 'm';
        header[14] = 't';
        header[15] = ' ';
        header[16] = 16; // 4 bytes: size of 'fmt ' chunk
        header[17] = 0;
        header[18] = 0;
        header[19] = 0;
        header[20] = 1; // format = 1
        header[21] = 0;
        header[22] = (byte) RECORDER_CHANNELS_INT;
        header[23] = 0;
        header[24] = (byte) (longSampleRate & 0xff);
        header[25] = (byte) ((longSampleRate >> 8) & 0xff);
        header[26] = (byte) ((longSampleRate >> 16) & 0xff);
        header[27] = (byte) ((longSampleRate >> 24) & 0xff);
        header[28] = (byte) (byteRate & 0xff);
        header[29] = (byte) ((byteRate >> 8) & 0xff);
        header[30] = (byte) ((byteRate >> 16) & 0xff);
        header[31] = (byte) ((byteRate >> 24) & 0xff);
        header[32] = (byte) (RECORDER_CHANNELS_INT * RECORDER_BPP / 8); // block align
        header[33] = 0;
        header[34] = RECORDER_BPP; // bits per sample
        header[35] = 0;
        header[36] = 'd';
        header[37] = 'a';
        header[38] = 't';
        header[39] = 'a';
        header[40] = (byte) (totalAudioLen & 0xff);
        header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
        header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
        header[43] = (byte) ((totalAudioLen >> 24) & 0xff);

        out.write(header, 0, 4088);
    }

    public void convertWaveToAmr(String wavFilename)
    {
        Log.i("SmartMonitor","Convertendo o arquivo para AMR");
        AmrInputStream aStream = null ;
        InputStream inStream = null;
        OutputStream out = null;

        try {
            inStream = new FileInputStream(wavFilename);
            aStream= new AmrInputStream(inStream);
            File file = new File(getFilename()+AUDIO_ENCODED_FILE_EXT);
            file.createNewFile();
            out= new FileOutputStream(file);

            // #!AMR\n
            out.write(0x23);
            out.write(0x21);
            out.write(0x41);
            out.write(0x4D);
            out.write(0x52);
            out.write(0x0A);

            byte[] x = new byte[1024];
            int len;
            while ((len=aStream.read(x)) > 0) {
                out.write(x,0,len);
            }
        } catch (FileNotFoundException e) {
            Log.e("SmartMonitor","Erro FileNotFoundException "+e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("SmartMonitor","Erro IOException "+e.getMessage());
            e.printStackTrace();
        }
        finally
        {
            try {
                out.close();
                aStream.close();
                inStream.close();
            } catch (IOException e) {
                Log.e("SmartMonitor","Erro IOException 2 "+e.getMessage());
                e.printStackTrace();
            }
        }
    }
}

Aqui está a amostra do audio original
Audio Wav
E o audio apos ser convertido
Audio AMR
Answer: O Seu problema é o sample rate, você esta gerando o áudio em 44100hz e gravando em 8000hz.
você precisa decidir e escolher qual deles utilizar, se estiver trabalhando só com voz pode ser considerado um desperdício de recursos trabalhar a 44100hz já que a voz humana pode chegar até 3400Hz a faixa de espectro de um sample rate a 8000hz é mais que suficiente para atender estas necessidades.
Provando que os audios estão em sample rate diferente só com as informações passadas por você:
Multiplicando 8000hz por 1m e 25s
8000*85

ans =

      680000

Multiplicando 44100 por 15 segundos arredondando para 4 milisegundos
44100*15.4

ans =

      679140

O resultado de ambos é quase o mesmo número de samples!
Meu conselho é alterar seu wav para gravar em 8000hz linha:
private static final int RECORDER_SAMPLERATE = 8000;

E tbm a linha:
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            RECORDER_SAMPLERATE, RECORDER_CHANNELS,
            RECORDER_AUDIO_ENCODING, 8000);

Comment: Obrigado pela resposta, mas após a alteração sugerida os áudios ficaram com quase o mesmo tempo mas a reprodução está muito rápida. O resultado pode ser visto (https://soundcloud.com/la-rcio-bernardo/sets/teste-amrinputstream).
Comment: Na classe Recorder somente em 44100 é que o wav fica com a reprodução normal, já o amr não fica, tentei também trocar na classe AmrInputStream o campo  private final static int SAMPLES_PER_FRAME = 8000 * 20 / 1000; por  private final static int SAMPLES_PER_FRAME = 44100 * 20 / 1000, mas também não funcionou, desculpe-me a ignorância, mas não entendo de conversão de arquivos de áudio, estou implementando por meu software funcionará como backup automático de chamadas.
Comment: Já tentou colocar o SAMPLES_PER_FRAME = 44100 ? Seu problema continua sendo o frame rate ...


I have a problem with finding the trace to a matrix.

Question: Let $T$ be a matrix of which I know its characteristic values, how can I find $\operatorname{Tr}(T-I)^{-1}$?
I know that the sum of the characteristic values is the trace, but I'm having a problem as I can't understand how I can know the new characteristic values of the new matrix using the old characteristic values and the old matrix.
Comment: Do you mean $\text{trace}((T - I)^{-1})$?
Comment: Also:  I think you might mean characteristic *values*, not characteristic *vectors*.  Am I right?
Comment: yes. the invertible matrix of (T-I)
Comment: I learn in another language so translating is a bit tricky... I think I know both the vectors and the values (not sure if that is what you meant)
Comment: If $Tv = \lambda v$, we usually say (in English) that $v$ is the *characteristic vector* corresponding to the *characteristic value* $\lambda$.
Comment: aha then I mean I know the characteristic values. thanks for correcting me
Answer: Hint: if you know all of the eigenvalues (characteristic values) of $T$ then you also know the eigenvalues of $T-I$. Now you need two facts: one, $\lambda$ is an eigenvalue of $S^{-1}$ if and only if $\lambda^{-1}$ is an eigenvalue of $S$, and two, the trace of an operator is the sum of its eigenvalues.
Comment: can I say that if λ is a eigenvalue of T so λ-1 is an eigenvalue of T-I, then considering that the number of times λ-1 appears in T-1 is equal to the algebraic multiplicity of the old matrix, find the eigenvalue of T-1 using that then the trace of the matrix is 1/λ (because it's an invertible matrix).
is that how this question is solved?
Comment: Yep, pretty much! Once you have the eigenvalues of $T-I$ and their multiplicities, you simply invert them and add them (with the corresponding multiplicities, of course).
Comment: YES!! THANK YOU THANK YOU THANK YOU!
If I pass my exam I'm getting you a new car lol.


How to compare AIC values from two Bayesian posteriors

Question: I have a simple question about model comparison:
Let's say you fit two models using MCMC: Model A and model B, where model B is model A minus one parameter. You want to assess whether dropping the parameter improves the model fit.
You calculate the likelihood and AIC for each MCMC sample.  So you end up with a distribution of AIC values for each model (e.g., if you take 10,000 MCMC samples for each model, then you have 10,000 AIC values from the posterior for each model).
How do I compare the AIC distributions?  I know that I could compare means, medians, or do other simple tests, but I don't want to guess wrong.  I've seen libraries such as AICcmodavg that automate model comparison, but my model is complicated enough that I'll probably have to write this by hand, and I'm embarrassed to say that I don't know what goes on behind the curtain.
Comment: Model selection in a Bayesian context is usually done via model evidence aka marginal likelihood, not AIC.
Comment: Thank you.  That led me to Bayes factors--hopefully that is the right way to go.
Comment: This is not correct, if only because this uses the data twice. Check [Murray Aitkin's book](https://projecteuclid.org/euclid.aos/1236693147) for a similar (and [flawed](https://xianblog.wordpress.com/2015/07/22/measuring-statistical-evidence-using-relative-belief-book-review/)) approach.
Answer: Firstly, you do not necessarily need to decide on a single model. Bayesian model averaging or a single model with some kind of sparsity-inducing prior on the extra parameter are often very good options for prediction modeling.
Secondly, tying a decision about a model choice to any particular threshold for Bayes factors, DIC or AIC can be quite problematic - except in extreme cases, when it is completely clear that the simple model is too simple. Thus, approaches that allow for uncertainty in this respect (see "Firstly, ..."). Just because one does this in a Bayesian context does not make the problems of model selection with hypothesis tests/AIC/whatever go away.
Finally, there are further options to look at how well a model is performing. These include posterior predictive checks, evaluation on a hold-out test set, leave-one-out cross-validation (which can be very easy to implement in MCMC samplers with a lot of useful discussion available on Andrew Gelman's blog).
Comment: Thanks.  I found Andrew Gelman's blog enormously useful in providing a broad overview of how all of these confusingly similar techniques are related, and when each might--or might not--be appropriate.


Using SVN with multiple users

Question: We are currently using SVN to handle our code. However, sometimes we all code all at once.
For example 3 people check out the same project. User 1 gets something done and goes to check it in. How can Users 2 and 3 get the update without losing their current work? We've tried the update command but usually yields some errors.
Should we be using something other than SVN?
I am using eclipse's svn repository.
Comment: You shouldn't be losing local changes as a result of `svn update`, but I assume you guys are running into merge conflicts when you do an update - someone changed code that you have you in your local copy and then you have to resolve them locally: http://tortoisesvn.net/docs/nightly/TortoiseSVN_en/tsvn-dug-conflicts.html
Comment: Thanks that makes more sense.
Answer: Subversion is designed exactly for what you're doing. However, your questions are covered by a basic understanding of how Subversion works. I recommend reading Chapters 1 and 2 of the svn book.
Also, the errors you're seeing on updates are probably conflicts, which are covered in the book.
Answer: You could try Git, but I doubt that the problem is the source code management software.
Which client are you using?  Update shouldn't be yielding errors.  Developers need to learn how to branch, update, merge, and commit code.  It's not always easy, but it's necessary.
A few rules to follow:

Check in early and often.  The longer you wait to check in, the more likely collisions and merges will be.
Get used to updating frequently.
Always update before you commit.

Git can make merges easier, but it doesn't eliminate them.
Comment: Does the update download changes from when you first checked it out? Also are using the SVN repository built into eclipse. Thanks
Answer: If it yields errors it means you made incompatible changes to the same lines of code, which leads to merge conflicts. Whatever VCS you'll use you'll have the same issues, should you be patching the same piece of code in incompatible ways.
VCS doesn't solve your problems with merging always. You need some collaboration rules as to who's working on which part of code and that will solve the merging issues mostly.
Practically, if you experience a merge conflict, you need to resolve it by hand, commit back and let other users update their working copy.

Using SVN with multiple users


Configuring reverse proxy with mod_proxy rules using only .htaccess file

Question: I am trying to connect website (which is share hosted) to a remote server, so that when I access http://example.com/test/ it will display the results from http://remote-example.com/ (which is a web app server)
The web hosting provider only allows me to modify the .htaccess file for configuration, they said mod_proxy is enabled and it can be configured with the .htaccess file.
They linked me Apache's Documentation, but I haven't been able to get it to work.
The hosting provider is using Lightspeed as the web server.
Is someone able to give me an example of what the .htaccess file should look like?
Can a reverse proxy be configured by modifying the .htaccess file only?
Comment: See also https://stackoverflow.com/questions/16297233/how-to-rewrite-location-response-header-in-a-proxy-setup-with-apache
Comment: @MrWhite I've tried using `ProxyPass /test http://remote-example.com` and
`ProxyReverse /test http://remote-example.com` But it didn't make any difference. Maybe the `proxy_http` module is not enabled
Answer: After some discussion with the hosting provider and further research into the issue I've concluded it cannot be done, due to the host restrictions.
The apache documentation itself specifies that ProxyPass can only be used in the Context: server config, virtual host, directory
Here is a link to some useful information which might help others Can ProxyPass and ProxyPassReverse work in htaccess
Comment: @DocRoot they have rewrite and mod_proxy enabled but its still cannot be done under their current system
Comment: `RewriteRule [P]` corresponds to `ProxyPass`, but it doesn't implement `ProxyPassReverse`. I'm not sure it is possible to implement its behavior in `.htaccess` - you need to rewrite the `Location` response header.
Comment: Did you try using mod_rewrite's `RewriteRule` with the `P` flag? This allows mod_rewrite to route requests through mod_proxy and is the only way you'd be able to do this in a shared environment, without access to the server - which appears to be what your host is suggesting. (Then again, this still might not be sufficient. Then again, if it's not then why has your host enabled mod_proxy in the first place?)
Answer: It can be done, as long as you have the ability to rewrite the HTML that comes back from the origin server. I've been able to create a working reverse proxy for WordPress hosted on a shared server where I don't have access to the Apache configuration.
See: My comments in response to a similar question on Stack Overflow
Note: Proxied pages are noticeably slower.


How do I get the grids to look like this using Bootstrap 4 grids?

Question: I can't figure out how to get the required result using Bootstrap 4 grid.
This is how the part of the site looks in desktop mode. 2, 3 and 6 are images while the rest are text.

This is how the same site should look like in mobile.

the HTML is:

                <divclass="row">

Here is a link to the project: https://rohithandique.github.io/Rare-Monk-Website/ and for 4,5,6 in the css, I put:
position: relative;
top: -25%;

to get the desired result. but it leaves a wide gap in the bottom. So how do I change my Bootstrap classes to get the desired result? I am a beginner.
EDIT: Tried all solutions and found out that using CSS grid gave more flexibility in this situation as the positioning of HTML elements were fixed and bootstrap 4 doesn't allow such mixing.
Answer: You should use CSS Grid  a modern API that replaces flex and older CSS stuff.  You can achieve whatever layout you want.  Keep in mind that is bidimensional.
It's the best you can get.  Easy to learn.
Answer: Bootstrap's grid are
display:flex
flex-direction:column
you cant style them that way you will have to use custom css for it !!!!
its as simple as that.
Answer: Hey please try to use bootstrap fluid container in these way you will get the desired result you want for example.look up to the link https://getbootstrap.com/docs/3.4/css/


NullReferenceException ao chamar função

Question: Estou criando alguns testes unitário para uma biblioteca de classes que faz manipulações em uma base de dados.
O método que está sendo testado é esse:
public object ExecuteNoQueryOperation ( string spOrSqlInstructions, CommandType commandType = CommandType.StoredProcedure, Dictionary<string, object> parameters = null )
{
    NpgsqlCommand npgsqlCommand = new NpgsqlCommand(spOrSqlInstructions, Connection);
    npgsqlCommand.CommandType = commandType;

    if ( parameters != null )
        foreach ( var item in parameters )
            npgsqlCommand.Parameters.Add ( new NpgsqlParameter ( item.Key, item.Value ) );

    try
    {
        Connection.Open ( );

        return npgsqlCommand.ExecuteScalar ( );

    }
    catch ( NpgsqlException e )
    {
        throw new Exception ( "Error: " + e.ToString() );
    }
    finally
    {
        if ( Connection != null && Connection.State != ConnectionState.Closed )
            Connection.Close ( );

        npgsqlCommand.Dispose ( );
    }
}

E aqui o meu caso de teste:
[TestMethod]
public void TestCallFunction ( )
{
    Dictionary<string, object> dic= new Dictionary<string, object> ();

    dic.Add ( "f_name", "Joice Silva" );
    dic.Add ( "f_age", 31 );

    pgDal.ExecuteNoQueryOperation ( "insertPerson", parameters: dic);

    Assert.AreEqual ( 31, ret);
}

Ao rodar esse teste estou recebendo o erro:

Nome de Teste:    TestCallFunction FullName de
  Teste:    DataAccessLayerTest.PostgreSqlDataAccessTest.TestCallFunction
  Fonte de Teste:   C:\Users\Matheus
  Saraiva\OneDrive\Desenvolvimento\Sistemas\DataAccessLayer\DataAccessLayer\DataAccessLayerTest\PostgreSqlDataAccessTest.cs:
  linha 39 Resultado de Teste:  com Falha Duração do
  Teste:    0:00:00,1025821
StackTrace do Resultado:  em
  DataAccessLayerTest.PostgreSqlDataAccessTest.TestCallFunction() na
  C:\Users\Matheus
  Saraiva\OneDrive\Desenvolvimento\Sistemas\DataAccessLayer\DataAccessLayer\DataAccessLayerTest\PostgreSqlDataAccessTest.cs:linha
  45 Mensagem de Resultado:  Método de teste
  DataAccessLayerTest.PostgreSqlDataAccessTest.TestCallFunction gerou
  exceção:  System.NullReferenceException: Referência de objeto não
  definida para uma instância de um objeto.

Não estou conseguindo identificar o erro.
Answer: Se isso é um teste "formal" você não pode acessar o que bem entende. Na verdade em um teste deve fazer o mínimo necessário. Muitas vezes é só a linha do Assert mesmo.
Esse teste não parece fazer sentido. Começando pelo nome. Não informa nada o que está testando.
Se não for um teste, o que não parece, o problema está em outro lugar da classe que não foi postado.
Independente do problema o erro ocorre porque a variável não foi inicializada, no caso a pgDAL. A única diferença é que no teste não tem porque ela existir ali, a linha não faz sentido, um teste não pode depender de coisas externas, e se fosse um método normal é só corrigir o erro na inicialização da variável. Não tenho como informar o jeito exato sem maior contexto.
De qualquer o código do método principal está muito complicado. Seria algo mais assim (não sei se é bem isto, não testei e não sei bem o contexto):
public object ExecuteNoQueryOperation(string spOrSqlInstructions, CommandType commandType = CommandType.StoredProcedure, Dictionary<string, object> parameters = null) {
    using var npgsqlCommand = new NpgsqlCommand(spOrSqlInstructions, Connection);
    npgsqlCommand.CommandType = commandType;
    if (parameters != null) {
        foreach (var item in parameters) npgsqlCommand.Parameters.Add(new NpgsqlParameter(item.Key, item.Value));
    }
    try { //eu deixei isso, mas não é uma boa ideia
        Connection.Open(); //precisa ter uma forma melhor de controlar abertura
        return npgsqlCommand.ExecuteScalar();
    } finally {
        if (Connection != null && Connection.State != ConnectionState.Closed) Connection.Close();
    }
}

Coloquei no GitHub para referência futura.
Não capture exceção para não fazer nada útil. Prefira usar o using para dispor recursos. Aproveitei para deixar mais fácil de ler.
Algumas perguntas para ajudar entender o assunto:

Minhas respostas sobre exceção.
Fechando requisições
Devo sempre utilizar Dispose?
Preciso usar o try/catch em toda a cadeia de um processo?
É correto usar um bloco using dentro de outro bloco using?

NullReferenceException ao chamar função


Any problems with using a 301 redirect to force https traffic in IIS?

Question: Is there any problem with using a 301 redirect to force all traffic to go to a secure-only site?  We originally had redirect rules, but enforcing SSL-only seemed more secure.  Here is how we set it up:
<code>Site 1:  https://example.com/
  Require SSL set
  Bound to 443 only

Site 2:  http://example.com
  Bound to 80 only
  Empty folder - no actual html or other data
  301 Redirects to https://example.com
</code>
This seems to work beautifully, but are there any issues with doing this?  Would any browsers not recognize the 301 redirect, or could there be security warnings during the redirect?
Comment: The problem is that we end up relying on the rewrite rules, which someone could screw up since they're under control of developers (i.e. in web.config)
Comment: You could do the same with just 1 site (bound to both 80 & 443) and redirect all HTTP requests to HTTPS with the help of 1 URL Rewrite Rule. The end result will be exactly the same.
Comment: Agree, that sometimes can be problematic to keep under control
Answer: As long as you use a full domain name (www.dom.com) you won't receive any SSL warnings.
Redirecting with a 301 will not cause any issues; simply put, it's a big part of the HTTP 1.0/1.1 protocols and should not cause any issues with any 'real' browsers or bots.
Comment: Thanks - one of my big concerns was that everyone on the web was suggesting code changes, fancy URL rewriting logic, and other methods to enforce SSL.  My 'solution' seemed so simple that I figured I missed something.


mod_rewrite apache for custom flat links gets stuck

Question: I want to create flat links for my website. The .htaccess code is following:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([0-9A-Za-z]+)?$ /index.php?page=$1 [L]
    RewriteRule ^([0-9A-Za-z]+)/?$ /index.php?page=$1 [L]
</IfModule>`

Now my website is working which can be seen through print_r($_GET) and getting all the values.
My website has a code like this: , where the image resides on path: /images/icons/up.png.
Now I can visit my website as: http://somedomain.com/home and everything works fine. But when I put: http://somedomain.com/home/, it gets stuck.
The image gets a path like this for the browser: http://somedomain.com/home/images/icons/image.png which is not available and it should be: http://somedomain.com/images/icons/image.png.
How to solve this?
It would helpful if there is any solution by modifying the .htaccess as I want to use relative paths for all links and not full path.

Thanks to Ben Clifford for your help. According to him if I redirect the second rule one to the format like the first one and not over index.php. Then it should work.
Comment: Include the base-url metatag in your head. See if that fixes the problem.
Answer: You could change that second rewrite rule to redirect the user to the non-/ version of the path, rather than to index.php; then when that redirected page is loaded it would hit the first rule, with the correct base path.
The issue is that the image name replaces everything after the last / in the URL: when you say somedomain.com/home then the image filename is appended to somedomain.com/ but in the second case, the final / is after the home. Using a rewrite rule to prune this / off would fix that, i think.
Comment: Ok, got the point. But it would be helpful if you build that code for the redirection as you stated, as I am a newbie to this mod_rewrite stuff.
Comment: maybe this: RewriteRule ^([0-9A-Za-z]+)/?$ /$1 [R]


Category as Collection/Set

Question: My instructor introduced the definition of a category where the objects and morphisms are said to be in "collections" instead of "sets". I am new to Category Theory so I am not really sure what is the difference between these concepts? Does this have anything to do with having sets as categories (with collection of sets and functions between sets)?
And later on in class there are definitions like:

Definition: A category $\mathscr{C}$ is small if $\text{Mor}(\mathscr{C})$ is a set (which implies $\text{Object}(\mathscr{C})$ is a set).

I also have trouble understanding why a category being small implies the objects also form a set?
Thanks in advance!
Comment: Quote: "You might wonder why the framers of the notion of category bothered to use two difficult words class and set rather than only one, in their definition. One could, after all, simply require that there be a set of objects of the category, rather than bring in the airy word class. In fact, people do that, at times: it is standard to call a category whose objects form a set a small category, and these small categories do have their uses. ...
Comment: ... **Indeed, if you are worried about foundational issues, it is hardly a burden to restrict attention to small categories. But I think the reason that the notion of class is invoked has to do with the high ambition we have for categories**: categories are meant to offer a fluid vocabulary for whole 'fields of mathematics' like group theory or topology, with a Fregean desire for freedom from the contingency implicit in subjective choices."

— Barry Mazur, [When is one thing equal some other thing?](http://people.math.harvard.edu/~mazur/preprints/when_is_one.pdf) (page 10).
Comment: Check this question out https://math.stackexchange.com/questions/1601545/whats-the-definition-of-a-collection
Answer: I also define categories using the notion of "collections". The reason is that I do not want to deal with set-theoretic problems (at least at first). Therefore I refrain from specifying whether something is a set or class and just call them collections. This is very common and most likely the reason why your instructor also did that.
Regarding your second question: For each object $X$ of a category $\mathcal{C}$ we have (per definition) one morphisms $X \rightarrow X$, namely the identity morphism. Therefore, the size of $\text{Mor}(\mathcal{C})$ is bigger or equal than the size of $\text{Ob}(\mathcal{C})$. If now $\text{Mor}(\mathcal{C})$ is small enough to be set, then so is $\text{Ob}(\mathcal{C})$.
Comment: The most satisfying definition I have seen of a class is "A class is a well-formed formula". It avoids the "class of all class" problem because it does not contain anything (this is not quite true; a careful justification is more subtle, but it can be done). The formula does not even refer to anything, necessarily, it is just an arrangement of symbols that is allowed in your logic scheme. A class $F$ with a free variable is "small enough to be a set" if $\{x: F(x)\}$ is a well-defined set, where $F(x)$ means that by instantiating the free variable as $x$, the resulting statement is true.
Comment: Thank you, I wonder if you can elaborate on some of the ideas you mentioned? 1) what would be a key difference between "sets" and "collections"? Is it just because sets follow ZFC, etc? (My instructor actually talked about Russell's paradox and stuff to support the idea of using "collections", but I still don't see a key difference.) 2) When you say Morphism being "small enough to be a set", what does that actually mean?
Comment: @EricNathanStucky Oh I see, thank you for these comments!

Category as Collection/Set


VHDL testbench getting U in simulation waveform

Question: I'm trying to implement a testbench using a Golden Model and a DUT, in this case I'm testing a full adder 4 bits. I'm always getting undefined at the signal s_dut while s_gm works just fine. I'm stuck on this for a while and I really don't know what the problem would be.
Here is the top module:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity topmodule is
end topmodule;

architecture Behavioral of topmodule is

    component SomadorCompleto4bits_dut is
    Port ( A : in  STD_LOGIC_VECTOR (3 downto 0);
             B : in  STD_LOGIC_VECTOR (3 downto 0);
             Cin : in  STD_LOGIC;
             S : out  STD_LOGIC_VECTOR (3 downto 0);
             Cout : out  STD_LOGIC);
    end component;

    component SomadorComOperador_golden_model is
    Port ( A : in  STD_LOGIC_VECTOR (3 downto 0);
             B : in  STD_LOGIC_VECTOR (3 downto 0);
             S : out  STD_LOGIC_VECTOR (4 downto 0));
    end component;

    component testbench is
    port (s_dut, s_gm : in STD_LOGIC_VECTOR (4 downto 0);
            a, b : out STD_LOGIC_VECTOR (3 downto 0));
    end component;

    signal a, b : STD_LOGIC_VECTOR (3 downto 0);
    signal s_dut, s_gm : STD_LOGIC_VECTOR (4 downto 0);

begin
    U0: SomadorCompleto4bits_dut port map(a, b, '0', s_dut (3 downto 0), s_dut(4));
    U1: SomadorComOperador_golden_model port map(a, b, s_gm);
    U2: testbench port map(s_dut, s_gm, a, b);
end Behavioral;

and here the testbench:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity testbench is
    port (s_dut, s_gm : in STD_LOGIC_VECTOR (4 downto 0);
            a, b : out STD_LOGIC_VECTOR (3 downto 0));
end testbench;

architecture Behavioral of testbench is
begin
    process
        variable a_teste_in, b_teste_in : STD_LOGIC_VECTOR (3 downto 0);
    begin

        report "Iniciando teste..." severity NOTE;

        a_teste_in := "0000";
        b_teste_in := "0000";

        for i in 1 to 16 loop
            for j in 1 to 16 loop

                a <= a_teste_in;
                b <= b_teste_in;
                wait for 500 ns;

                assert (s_dut = s_gm) report "Falhou: i = " & integer'image(i) & " j = " & integer'image(j) severity ERROR;

                a_teste_in := a_teste_in + 1;

            end loop;

            b_teste_in := b_teste_in + 1;

        end loop;

        report "Teste finalizado!" severity NOTE;

        wait;

    end process;
end Behavioral;

I believe the error is somewhat related with the line:
U0: SomadorCompleto4bits_dut port map(a, b, '0', s_dut (3 downto 0), s_dut(4));

---edited:
Here's the DUT:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

--DEVICE UNDER TEST--

entity SomadorCompleto is
     Port ( S : out  STD_LOGIC;
              Cout : out  STD_LOGIC;
              A : in  STD_LOGIC;
              B : in  STD_LOGIC;
              Cin : in  STD_LOGIC);
end SomadorCompleto;

architecture Behavioral of SomadorCompleto is

begin
    S <= A xor B xor Cin;
    Cout <= (A and B) or (A and Cin) or (B and Cin);
end Behavioral;

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity SomadorCompleto4bits_dut is
     Port ( A : in  STD_LOGIC_VECTOR (3 downto 0);
              B : in  STD_LOGIC_VECTOR (3 downto 0);
              Cin : in  STD_LOGIC;
              S : out  STD_LOGIC_VECTOR (3 downto 0);
              Cout : out  STD_LOGIC);
end SomadorCompleto4bits_dut;

architecture Behavioral of SomadorCompleto4bits_dut is

signal fio_c1, fio_c2, fio_c3 : STD_LOGIC;

component SomadorCompletoSimples is
    Port ( a : in  STD_LOGIC;
             b : in  STD_LOGIC;
             cin : in  STD_LOGIC;
             s : out  STD_LOGIC;
             cout : out  STD_LOGIC);
end component;

begin
    U0: SomadorCompletoSimples port map(A(0),B(0),'0',S(0),fio_c1);
    U1: SomadorCompletoSimples port map(A(1),B(1),fio_c1,S(1),fio_c2);
    U2: SomadorCompletoSimples port map(A(2),B(2),fio_c2,S(2),fio_c3);
    U3: SomadorCompletoSimples port map(A(3),B(3),fio_c3,S(3),Cout);
end Behavioral;

--------------------------------------

Thanks in advance!
Comment: You dont show the code for you DUT - the problem is likely in there.
Comment: You show the definition for `SomadorCompleto` but don't use it, and you instantiate `SomadorCompletoSimples` but don't show its definition. If you meant for these to be the same thing, their port orders are also different.
Comment: sorry, updated.
Comment: yep, that was the problem. Thanks!
Answer: I just forgot to put "Simples" on SomadorCompleto, because both are the same


How to return a variable multiple times and create an array

Question: I am creating a shop/inventory system for a python rpg and return several variables from a function in my shop. I create 3-5 items every time the shop is visited so right now if you print one of my returned variables it will print the info from the last item created. I need to be able to store each unique items values in an array so that each item can be called on not just the last one.
if plus == 0: #if item made has a plus equal to nothing
                        if has_add == 'yes': #if it has an add
                            if item_type == 'weapon':  #if it is a weapon (no plus, has add, is weapon)
                                created_item = print(str(count) + ". " + quality + " " + wep_adj + " " + "" + weapon_type + " of " + wep_add + "..........Attack: " + str(weapon_attack) + " Price: " + str(wep_price)) #print this
                            if item_type == 'armor': #if it is an armor (no plus, has add, is armor)
                                created_item = print(str(count) + ". " + quality + " " + arm_adj + " " + " " + armor_piece + " of " + arm_add + "..........Armor: " + str(armor_defense) + " HP: " + str(armor_hp) + " Price: " + str(arm_price)) #print this
                        else: # if item doesnt have add
                            if item_type == 'weapon':  #if it is a weapon (no plus, no add, is weapon)
                                created_item = print(str(count) + ". " + quality + " " + wep_adj + " " + "" + weapon_type + "..........Attack: " + str(weapon_attack) + " Price: " + str(wep_price)) #print this
                            if item_type == 'armor': # if it is an armor (no plus, no add, is armor)
                                created_item = print(str(count) + ". " + quality + " " + arm_adj + " " + " " + armor_piece  + "..........Armor: " + str(armor_defense) + " HP: " + str(armor_hp) + " Price: " + str(arm_price)) #print this
                    else: #if item made has a plus
                        if has_add == 'yes': # if it has an add
                            if item_type == 'weapon': # if it is a weapon (has plus, has add, is weapon)
                                created_item = print(str(count) + ". " + quality + " " + wep_adj + " " + "" + weapon_type + " of " + wep_add + " +" + str(plus) + "..........Attack: " + str(weapon_attack) + " Price: " + str(wep_price)) #print this
                            if item_type == 'armor': #if it is an armor (has plus, has add, is armor)
                                created_item = print(str(count) + ". " + quality + " " + arm_adj + " " + " " + armor_piece + " of " + arm_add + " +" + str(plus)  + "..........Armor: " + str(armor_defense) + " HP: " + str(armor_hp) + " Price: " + str(arm_price)) #print this
                        else: # if it doesnt have an add
                            if item_type == 'weapon': #if it is a weapon (has plus, no add, is weapon)
                                created_item = print(str(count) + ". " + quality + " " + wep_adj + " " + "" + weapon_type + " +" +  str(plus) + "..........Attack: " + str(weapon_attack) + " Price: " + str(wep_price)) #print this
                            if item_type == 'armor': #if it is an armor (has plus, no add, is armor)
                                created_item = print(str(count) + ". " + quality + " " + arm_adj + " " + " " + armor_piece + " +" + str(plus)  + "..........Armor: " + str(armor_defense) + " HP: " + str(armor_hp) + " Price: " + str(arm_price)) #print this

                    if item_type == 'weapon': #allows us to get info out of function
                        return (created_item, count, quality, wep_adj, weapon_type, wep_add, plus, weapon_attack, wep_price, randvar)
                    else:
                        return (created_item, count, quality, arm_adj, armor_piece, arm_add, plus, armor_defense, arm_price, armor_hp)

                while items_amount > items_made: #if we've made 3-5 items, stop making them
                    new_item, count, quality, adj, piece, add, plus, stat, price, other_stat = make_item()
                    items_made += 1 #increase items made every time one is made
                    count += 1
                return (new_item, count, quality, adj, piece, add, plus, stat, price, other_stat)

            new_item, count, quality, adj, piece, add, plus, stat, price, other_stat = generate_items() #call function to make all items when shop is visited
            print(new_item, count, quality, adj, piece, add, plus, stat, price, other_stat)

Since my code is so immense I don't want to link the entirety of relevant code. The most relevant is this:
while items_amount > items_made: #if we've made 3-5 items, stop making them
                    new_item, count, quality, adj, piece, add, plus, stat, price, other_stat = make_item()
                    items_made += 1 #increase items made every time one is made
                    count += 1
                return (new_item, count, quality, adj, piece, add, plus, stat, price, other_stat)

I need to be able to return them as arrays instead of variables
Comment: What you are doing already return a tuple containg the variables new_item, count, quality, adj, piece, add, plus, stat, price, other_stat
Comment: @user312016 yes but I need each variable in the tuple to be stored seperately for each item created. Right now each item created overwrites the last.
Comment: @Shnipper You may use a dictionnary instead though. What are you meaning by "Right now each item created overwrites the last" ?
Comment: @user312016 the function is called between 3-5 times. Every time the function is called a new item is created. In my last line of the first code above I print every variable that is returned. What gets printed is the information for the last item that was created. I need a way to access the information for the other items that were made before that one
Answer: You could treat whatever you are returning as one tuple variable and append it to a list:
items = list()
while items_amount > items_made:  # if we've made 3-5 items, stop making them
    new_item_details = make_item() # assign to one tuple variable
    items.append(new_item_details) # append to list
    items_made += 1  # increase items made every time one is made
    count += 1
return items

If you want to access each details of individual item and you want it to be nice as well, I suggest you create a class with required variables:
class Item(object):
    def __init__(self, item_details):
        self.item = item_details[0]
        self.count = item_details[1]
        self.quality = item_details[2]
        self.adj = item_details[3]

        ... # similarily other fields

        self.other_stat = item_details[9]

and then you can create these items:
items = list()
while items_amount > items_made:  # if we've made 3-5 items, stop making them
    new_item_details = make_item() # assign to one tuple variable
    items.append(Item(new_item_details)) # append to list
    items_made += 1  # increase items made every time one is made
    count += 1
return items

Now, if you want to access adj of 2nd item:
# 1st index would be the second item in the list
items[1].adj # access using variable name on the instance

Answer: Insert your tuples into a list:
items = list()
while items_amount > items_made:  # if we've made 3-5 items, stop making them
    new_item, count, quality, adj, piece, add, plus, stat, price, other_stat = make_item()
    items_made += 1  # increase items made every time one is made
    count += 1
    items.append((new_item, count, quality, adj, piece, add, plus, stat, price, other_stat))

return items

Comment: TypeError: append() takes exactly one argument (10 given)
Comment: @Shniper I fixed a typo.
Comment: This works but how do I access each individual value. So if I wanted to access the adj of the 2nd item how would I? I tried items.adj[1], didn't work
Comment: You mean you added extra parenthesis to make the appended type a tuple.
Comment: @Shniper You would have to do items[1][3]
Comment: If you want to be able to manipipulate the data after it is returned you should use append([...]) instead of append((...))


Identify the locus.

Question: Let $A,B,C$ lie on a straight line. $B$ is lying between $A$ and $C$. Consider all circles passing through $B$ and $C$. The point of contact of the tangents from $A$ to these circles lies on .....
We have to identify the locus not give its equation. I tried to make the diagram and apply some geometry, I found a cyclic quadrilateral with vertices : $A$, centre of circle and ends of chord of contact. But, I am unable to proceed further. Any help would be appreciated.
Answer: The locus will be a circle.Let the point of contact from the tangent be $P$. Note that for any circle through $B$ and $C$, $AB \times AC $ is a constant which is equal to $AP^2$(The length of the tangent squared-Tangent Secant Theorem)
Comment: So elementary... Thank you.

Identify the locus.


apply function on groups of k elements of a pandas Series

Question: I have a pandas Series:
0     1
1     5
2    20
3    -1

Lets say I want to apply mean() on every two elements, so I get something like this:
0    3.0
1    9.5

Is there an elegant way to do this?
Comment: I've updated my post.
Answer: You can use groupby by index divide by k=2:
k = 2
print (s.index // k)
Int64Index([0, 0, 1, 1], dtype='int64')

print (s.groupby([s.index // k]).mean())
   name
0   3.0
1   9.5

Comment: Thanks! Is there any way to deal with the case that k does not divide the length of the series? I tried: (s[-m:].index // k) when k divides m, but it did not work
Comment: I think I catch it. But you need first `reset_index` - `s = s[-m:].reset_index(drop=True)` and then use `print (s.groupby([s.index // k]).mean())`
Comment: Got it, so I guess need to do [(s.index + ((k - (len(s) % k)) % k)) // k]
Answer: If you are using this over large series and many times, you'll want to consider a fast approach.  This solution uses all numpy functions and will be fast.
Use reshape and construct new pd.Series
consider the pd.Series s
s = pd.Series([1, 5, 20, -1])

generalized function
def mean_k(s, k):
    pad = (k - s.shape[0] % k) % k
    nan = np.repeat(np.nan, pad)
    val = np.concatenate([s.values, nan])
    return pd.Series(np.nanmean(val.reshape(-1, k), axis=1))

demonstration
mean_k(s, 2)

0    3.0
1    9.5
dtype: float64

mean_k(s, 3)

0    8.666667
1   -1.000000
dtype: float64

Answer: You can do this:
(s.iloc[::2].values + s.iloc[1::2])/2

if you want you can also reset the index afterwards, so you have 0, 1 as the index, using:
((s.iloc[::2].values + s.iloc[1::2])/2).reset_index(drop=True)

Comment: My bad, was a bit too quick ;)
Comment: This works good for the k=2 case, but I want to do this for many k's...
Comment: Mmmm should still be possible though, I'll think about it a bit :).


Loop em JavaScript para clicar em botão

Question: Eu estou usando o console para clicar em um botão com o seguinte código:
document.getElementById('meubotao').click();

Eu gostaria de fazer um loop for e deixar o JavaScript clicar no botão umas 100 vezes, mas toda vez que a página recarrega dá erro.
Como eu faço para o console manter o script mesmo com a atualização da página (SPA)?
Observação: Eu só estou usando o console, não tenho acesso ao código fonte. Só quero facilitar meu trabalho e não ficar clicando no botão.
Answer: Eu acredito que para conseguir o comportamento que você precisa poderia usar a função setInterval ao invés de um for.  Tente dar 1 segundo de intervalo em cada clique para que a página possa processar (1 segundo são expressos por 1000 milisegundos na função abaixo)
setInterval( function(){ document.querySelector('#meubotao').click();}, 1000);

Answer: Não sei se entendi bem, mas você quer dispara programaticamente 100 cliques em um botão de uma páginas de terceiros.
Se for eu vou dar uma resposta específica para o Chrome pois o Chrome é browser mais comum e possui na opção das Ferramentas do Desenvolvedor o console, ferramenta que permite modificar a página vigente.
Carregue a página em que quer gerar os cliques.
Pressione [Ctrl] + [Shift] + I para abrir as Ferramentas do Desenvolvedor.
No barra de menu selecione a opção Elementos e descubra o atributo id do botão que quer instalar o gerador de cliques.
Se o botão não possuir o atributo id sinto muito mas vai ter que dar 100 cliques na mão.
De posse do atributo 'id' volte a barra de menu e selecione a opção Console.
No console crie uma função para disparar evento programaticamente. Nesse exemplo a função chama-se DispararEvento que aceita um elemento da página e o nome de um evento.
    function DispararEvento( elemento , evento ) {
        var eventoDOM = new Event( evento ); // Cria o evento
        elemento.dispatchEvent( eventoDOM );    // Despacha o evento.
    }

A lógica consiste em pegar o betão pelo id e criar um loop que itere 100 vezes usando a função DispararEvento para disparar os 100 cliques.
   function loop(){
       for(var i = 0; i < 100; i++){
            DispararEvento(document.getElementById(/*Aqui vai o id do botão alvo*/), "click");
       }
  }

Depois é só criar um botão na página que clicado invoque a função loop.
document.body.innerHtml += '</input><button onclick="loop();">Clicar em loop</button>';

Feche as ferramentas do desenvolvedor e clique no novo botão que criou e serão disparados os 100 clique contra o botão alvo.

Um exemplo simples dá logica envolvida, criei um input e dois botões um deles é o alvo(id='somar') que soma 1 ao valor do input e outro botão dispara 100 cliques.

    <input id="resultado" type="number" value="0"></input><button id="somar" onclick="incrementar();">Somar 1</button>

    </input><button onclick="loop();">Somar em loop</button>

Loop em JavaScript para clicar em botão


Delay A pipeline For 24Hours

Question: Hello Im using Azure piplines for some tasks
Here is a simiple example of what im trying to achieve :

what im trying to do is to to trigger pipline2 after pipeline1 complition (this is done)
and once the pipeline 2 is triggered i want it to wait for 24 Hours before it runs
i don't know if that is possible
i tried the delay task
but it timesout in 1 Hour
i've Read that timouts are

For 360 minutes (6 hours) on Microsoft-hosted agents with a public project and public repository
For 60 minutes on Microsoft-hosted agents with a private project or private repository

so the option to have a task last for 24hours is only possible on self-hosted agents
i have also seen the schedule option wich is on the repo level not pipeline compeletion
i hope someone can guide me to the right direction
Answer: Such tasks could be run with Agentless jobs: Agentless jobs supported tasks
<code>jobs:
- job: string
  pool: server # note: the value 'server' is a reserved keyword which indicates this is an agentless job
  steps:
  - task: Delay@1
    inputs:
      delayForMinutes: '1440'
</code>

Delay A pipeline For 24Hours


Show scrollable full screen Bottom sheet Android

Question: I have countered a situation that needs to implement a scrollable modal bottom sheet. Basically, it will show half off the screen at first, then when scrolling up, it will stretch to 90% of the screen. If keep swiping up, it will scroll the content inside bottom sheet.
What I have tried

layout.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/design_bottom_sheet"
        style="@style/RootLayout"
        android:background="@drawable/rounded_shape"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <...Content.../>

        </android.core.widget.NestedScrollView>
    </FrameLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout

Bottom Sheet Dialog class

I followed the answer here accepted answer
Result
It only shows half of the screen when opened, and not scroll the bottom sheet to Expanded State when I scroll the contents up like this result.
Expectation
I expected the above code should behave like this expectation
Does any one have the solution for this situation?
Thank you
Answer: So after more than a week, I found a solution for this problem,
I override the onCreateDialog method like below
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    // This will disable the behavior of bottom sheet to have a flat corner
    //https://github.com/material-components/material-components-android/pull/437#issuecomment-678742683

    val dialog = BottomSheetDialog(requireContext(), theme)
    dialog.behavior.disableShapeAnimations()

    dialog.setOnShowListener { dialogInterface ->

        val bottomSheetDialog = dialogInterface as BottomSheetDialog
        val parentLayout =
            bottomSheetDialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
        parentLayout?.let { it1 ->
            val behaviour = BottomSheetBehavior.from(it1)
            it1.layoutParams.also {
                it.height =
                    context?.resources?.displayMetrics?.heightPixels?.times(0.9)?.toInt()!!
            }
            behaviour.peekHeight =
                context?.resources?.displayMetrics?.heightPixels?.times(0.6)?.toInt()!!
        }
    }
    return dialog
}

What is happening in this code?
I was NOT use the dialog provider by BottomSheetDialog neither super.onCreateDialog()
Instead I used BottomSheetDialog(context,theme) in order to create my custom bottom sheet theme.
Then, I set listener for dialog, get parent layout and bottom sheet dialog like below. The most important part is dealing with behavior.
After achieved the behavior correctly, you are done. Setting it's state to be expanded as recommended by @Nitish Chaudhary and setting the layout height to the height that you want. That's all I need, also this code below is reusable to me wherever you follow the modal bottom sheet XML layout rule.
Happy coding.


ListView muito lenta na barra de rolagem

Question: Estou com um problema que não sei como solucionar, tenho um ListView personalizada com imagens e a barra de rolagem fica travando, mesmo com a lista carregada. Fica lento a lista para rolar.
Adapter:
public class AdapterSegmento extends BaseAdapter {

private LayoutInflater mInflater;
private ArrayList<Categoria> itens;

public AdapterListView(Context context, ArrayList<Categoria> itens) {
    //Itens que preencheram o listview
    this.itens = itens;
    //responsavel por pegar o Layout do item.
    mInflater = LayoutInflater.from(context);
}

/**
 * Retorna a quantidade de itens
 *
 * @return
 */
public int getCount() {
    return itens.size();
}

/**
 * Retorna o item de acordo com a posicao dele na tela.
 *
 * @param position
 * @return
 */
public Categoria getItem(int position) {
    return itens.get(position);
}

/**
 * Sem implementação
 *
 * @param position
 * @return
 */
public long getItemId(int position) {
    return position;
}

public View getView(int position, View view, ViewGroup parent) {
    //Pega o item de acordo com a posção.
    Categoria item = itens.get(position);
    //infla o layout para podermos preencher os dados
    view = mInflater.inflate(R.layout.item_list, null);

    //atravez do layout pego pelo LayoutInflater, pegamos cada id relacionado
    //ao item e definimos as informações.
    ((TextView) view.findViewById(R.id.text)).setText(item.getTexto());
    ((ImageView) view.findViewById(R.id.imagemview)).setImageResource(item.getIconeRid());
  //  ((TextView) view.findViewById(R.id.subtitulo)).setText(item.getSubtitulo());

    return view;
}

Classe que preenche a lista:
private void gerarLista() {

    itens = new ArrayList<Categoria>();

    String[] categorias = getResources().getStringArray(R.array.categorias);
    String[] drawableCategorias = getResources().getStringArray(
            R.array.categorias_drawable);

    listView.setScrollingCacheEnabled(false);

    // Vai recuperar os dois array's la do strings.xml e iterar sobre eles e criar os itens
    for (int i = 0; i < categorias.length; ++i) {
        itens.add(new Categoria(categorias[i], getResources()
                .getIdentifier(drawableCategorias[i], "drawable",
                        this.getPackageName())));
    }

    // Criamos uma lista que preenchera o ListView
    /*
     * itens = new ArrayList<Categoria>(); Categoria item1 = new
     * Categoria("Alimentação", R.drawable.alimentacao); Categoria item2 =
     * new Categoria("Esporte", R.drawable.esporte);
     *
     * itens.add(item1); itens.add(item2);
     */
    // Cria o adapter
    adapterListView = new AdapterListView(this, itens);

    // Define o Adapter
    listView.setAdapter(adapterListView);
    // Cor quando a lista é selecionada para ralagem.
    listView.setCacheColorHint(Color.TRANSPARENT);
}

Como resolver isso?
Comment: O que há nas classes `Segmento` e `Categoria`?
Comment: olá, eu alterei o post pois o adapter estava errado, nao existe a classe Segmento, apenas categoria.Bom, Categoria é apenas uma classe onde contem Titulo, e Imagem, e os get e set, é apenas uma forma de modelo.
Comment: Depois de rolares a lista toda as imagens continuam carregadas e a rolagem fica melhor?
Comment: não, sempre fica lenta mesmo as imagens carregadas.
Comment: Provavelmente é o decode da imagem. Já tentou colocar a linha  `((ImageView) view.findViewById(R.id.imagemview)).setImageResource(item.getIconeRid());` dentro de um `AsyncTask`?
Answer: O problema está na arquitetura do seu algoritmo. A cada iteração no método getView() do Adapter você está inflando a view que deseja. Isso é uma má prática!
Você precisa utilizar o padrão de projetos Holder, que irá guardar as referências estaticamente das suas Views e irá melhorar a performance. Além disso, se você perceber, o método getView() recebe como parâmetro uma View (view) que nada mais é do que a View que você retorna neste próprio método, ou seja, eles utilizam a metodologia da recursividade.
Um exemplo bem básico do padrão Holder seria:
public static class ViewHolder {
  TextView hTextViewName;
}

public View getView(int position, View view, ViewGroup parent) {
  ViewHolder holder;
  if (view == null) {
    holder = new ViewHolder();
    view = LayoutInflater.from(context).inflate(R.layout.view_to_inflate, parent, false);
    holder.hTextViewName = (TextView) view.findViewById(R.id.textView_name);
    view.setTag(holder);
  } else {
    holder = (ViewHolder) view.getTag();
  }

  String name = getItem(position);
  holder.hTextViewName.setText(name);

  return view;
}

Perceba a diferença!
Dessa forma você terá muito mais performance.
Resumindo, a partir do caso acima você apenas precisa adapta-lo ao seu contexto! :-)
Comment: Você pode olhar mais curiosidades sobre performance em ListView no próprio site do Android: http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Comment: mas isso seria no adapter? voce pode me mostrar como aplicar ao meu modelo? pois não to conseguindo. obrigada
Comment: Clara, é no seu adapter sim! Note que eu estou utilizando o mesmo método que você "getView()". Você só precisa adicionar o ImageView no meu ViewHolder e instanciar a variável das Views no ViewHolder. Não tem segredo, é só analisar o código e entender!
Comment: Clara, você conseguiu fazer com que o seu Adapter ficasse performático? Olha esse vídeo que fiz a muito tempo atrás http://debugcodigos.blogspot.com.br/2012/05/adapter-eficiente-android.html


Subset Differences between Bagging, Random Forest, Boosting?

Question: Per my understanding, there are 2 kinds of "subsets" that can be used when creating trees: 1) Subset of the dataset, 2) Subset of the features used per split.
The concepts that I'm comparing are: 1) Bagging, 2) Random Forest, and 3) Boosting.
Please let me know if the following is correct or incorrect:
Bagging: Uses Subset of the dataset (bootstrapping) to create trees, but All features can be used for splits.
Random Forest: Uses Subset of the dataset (bootstrapping) to create trees, and only Subsets of features can be used for splits.
Boosting: Uses Entire dataset to create trees (no bootstrapping), and All features can be used for splits.
Yes I know there are more differences between the three, but wanted to hone in on these subset differences here and clarify my understanding on these first.
Comment: Sounds like a good summary. Maybe it is worth noting that boosting is also possible with both types of subsetting.
Comment: Breiman's clever coining of words in 2001 is the original, go-to work on these methodologies.  https://www.stat.berkeley.edu/~breiman/randomforest2001.pdf
Answer: Random forests use bagging (bagging is just a contraction of "bootstrap aggregation," see Elements of Statistical Learning Section 8.7). Bagging draws a bootstrap sample of the data (randomly select a new sample with replacement from the existing data), and the results of these random samples are aggregated (because the trees' predictions are averaged). But bagging, and column subsampling can be applied more broadly than just random forest. (There's also a discussing in ESL of how random forest is well-positioned to benefit from bagging, while other learning methods are not.)
The boosting implementations that I'm familiar (e.g. xgboost) will also support random subsampling of columns. But your guess is correct, in the sense that column subsampling is really incidental to the boosting procedure itself. Boosting is about estimator $T+1$ "correcting" the errors of the previous $T$ estimators. Likewise, bagging is also incidental to the boosting procedure, but bootstrap resampling of the data could be implemented as well.
Comment: So sounds like the bagging, random forest, and boosting above are correct, and you would add: `XGBoost/GBT`: Uses Entire dataset to create trees (no bootstrapping), and either All or Subset of Features can be used for splits.
Comment: You can use resampling with boosting models. For example, the `xgboost` documentation https://xgboost.readthedocs.io/en/stable/parameter.html#parameters-for-tree-booster lists several options for sampling training observations.
Comment: Gotcha, edited to: `XGBoost/GBT`: Uses Entire or Subset of Dataset to create trees (bootstrapping optional), and All Features can be used for splits.


SQL Server : convert char to float and round it

Question: I have a database with a column of VARCHAR2 type keeping a value like: 398566,569885
What I need is to cast this number to float and then to make it looking like: 398566,56
Casting it to float is not so important, if It's possible to trim the numbers it will be also ok.
What I have tried is:
select
cast (p1.VALUE as float) as Total
from Workflow p
LEFT JOIN PA_PARAMETER p1 on p1.ID=p.ID AND p1.NAME = 'Total'
WHERE p.TYPE = 'Marketing'

Is there a way to trim these numbers or to round it somehow?
Comment: As far as I know, SQL Server doesn't have a `VARCHAR2` datatype (that would be Oracle) ....
Answer: This will round your numbers
select cast(value as decimal(9,2))
from workflow...

Comment: @podiluska I'm 99% sure SQL Server cannot round 5698.6548 to 5698.66. Only way to do it I can imagine is to do bankers' rounding. C# round work as banker's rounding by default, though
Comment: it's throwing: "Arithmetic overflow error converting numeric to data type numeric."
Comment: You may need to increase the `9` value depending on the largest value in your data.
Comment: seems to be working but it's returning +1 in the results. For exampe 5698.6548 is casted to 5698.66
Answer: you can do
declare @a nvarchar(max)
select @a = '398566,569885'
select cast(@a as decimal(29, 2))

But then you'll receive 398566.57 because of rounding
To get 398566.56 you have to use round function with specified third parameter - http://msdn.microsoft.com/en-us/library/ms175003.aspx (When a value other than 0 is specified, numeric_expression is truncated.)
select cast(round(cast(@a as decimal(29, 10)), 2, 1) as decimal(29, 2))

Answer: From what I can gather, you only want to reformat a VARCHAR column containing some (European) decimal text to 2 places.
select
left(p1.value, charindex(',', p1.value + ',')+2) as Total
from Workflow p
LEFT JOIN PA_PARAMETER p1 on p1.ID=p.ID AND p1.NAME = 'Total'
WHERE p.TYPE = 'Marketing';

And if you consistently want the 2 decimal places, then
select
left(p1.value + ',00', charindex(',', p1.value + ',')+2) as Total
from Workflow p
LEFT JOIN PA_PARAMETER p1 on p1.ID=p.ID AND p1.NAME = 'Total'
WHERE p.TYPE = 'Marketing';


First jQuery project - Using a variable as a selector

Question: What I'm trying to do is quite simple but as a beginner I'm getting incredibly frustrated with it.
Here's my first attempt just to give you the idea .
I plan to do something a bit more fancy with the images, but I could see straight away that this wasn't the solution. Mouseover was changing the image before it had faded out and looked horrible.
So, I thought I might put all the images in the same place and hide them, making them visible and bringing them to the front on mouseover of the corresponding hotspot. I'm hoping there's a way to decrement an element's z-index value with .css().
Here's where I am with it (same URL, 9872_gangsters_moll_2nd_attempt.html).
$(".hotspot").mouseover(function(){
          //Get the id of this triggered item
          var imageid = $(this).attr("id");
          //use it to make corresponding image id to use as jQuery selector
          var currentImg = '#img_'+ imageid;
  //      alert(currentImg);                   //shows variable is correct
  //      $('.product-img').show();            //works fine with a class
          $('currentImg').show();                //doesn't work with a variable
          $('currentImg').addClass('front');     //same, obviously
});

I tried toggling visibility with css originally, but went with jQuery's show/hide. Neither worked. The problem seems to be with passing 'currentImg' as a selector.
Any help you could offer with this would be hugely appreciated.
Thanks,
Andy
Answer: Just remove the quotes making it use a literal string (instead of your variable). $('currentImg') should be just $(currentImg), like this:
$(".hotspot").mouseover(function(){
   var currentImg = '#img_'+this.id;
   $(currentImg).show().addClass('front')
});

The other change above... things like .id are DOM properties, you can access them directly (as opposed to the less efficient .attr("id")).
Comment: @andylamb: Re Nick's final comment, about `id`: Specifically, notice he used `this.id` (the `id` property on the *raw* DOM object), rather than `$(this).attr("id")` (wrapping a jQuery instance around the DOM object [`$(this)`] and then calling a function [`attr`] to look up the ID). You can safely do this with `id`, `src` on images, `href` on links, and a few others.
Comment: Thanks for your answer and especially for the extra info re: raw DOM objects. Hugely helpful.
Answer: $('currentImg').show(); is using a string literal, not a variable. Get rid of the quote marks.
Comment: Thanks for your answer. Needed a fresh pair of eyes
Answer: When you enclose a set of alphabets within single quotes or double quotes, it becomes a string literal.
When you say $('currentImg'), it will search for an html tag with name 'currentImg'. What you need is $(currentImg) as mentioned by @Nick and @David
Comment: Thanks Srisa. I was just testing *ahem* ;)


How to print only src path of taxonomy image

Question: I'm using Taxonomy image 1.6 on D6.19, I'm working on a legacy stack that I can't upgrade, nor do I have direct access to the taxonomy image module. Nice right?
Right now I'm using the taxonomy image in two ways, one is being printed out in the standard functionality of the mod and thats fine, the second is as a popup overlay created by jquery.
The popup is holding it's info in the "data-content" portion of a div, and required that any tag inside using only single quotes. Double quotes end the script early. I need to call the Src of the image so I can dynamically print it into an image tag. or I need to know how to hook into it so I can rewrite it for this particular use.
Any guidance would be welcome.
Thanks
Stephanie
Answer: A quick look at a download of the current version of the module (1.7) shows that there is a function based on the $tid that returns a lot of formatted and unformatted information:
<code>/**
 * Function to get an image object with more useful data for custom formatting.
 * @param
 *   $tid - the term tid.
 * @return
 *   image object.
 */
function taxonomy_image_get_object($tid, $recursive = NULL)
</code>
with one of the things being returned being the url to the image.  Assuming this function exists in your version, would this work for your use case?  If so, there is even a helper function available as well:
<code>/**
 * Function to get a url for an image, for use in css, html or other client-side code.
 * @param
 *   $tid - the term id
 * @return
 *   The url string.
 */
function taxonomy_image_get_url($tid) {
  $image = taxonomy_image_get_object($tid);
  return $image->url;
}
</code>
Comment: I saw this too, but wasn't sure how/where to apply.
I have a vocabulary of color filter images to assign to a node type, and wasn't sure how to get the specific TID for each term in this vocab dynamically. I appreciate the help and if you have any other advise you don't mind sharing it would be appreciated.
Comment: From the little I've looked at this module, all it does is allow you to display an image if a node is already tagged with a taxonomy term/tid in addition to the textual term itself.  So, you would just grab those existing $tids and do whatever with them.
Comment: Thanks but it's the "how" and "where" I need now. I'm sorry, but please don't assume I'm totally php literate I've also not used this module before. My problem seems to be the double quotes that are being generated are breaking my popover effect my ending it too soon. I'm also attempting to str_replace/perg_replace the quotes. I appreciate your time.
Comment: If you throw a `drupal_set_message('' . print_r($node->taxonomy, TRUE) . '');` in your node.tpl.php, you can see all that you have to work with, but basically `$node->taxonomy` is an array of taxonomy info keyed by the tid.


Unable get 600,000 deletion stubs ro purge from database

Question: Update: I have issued a: show database mycleandb.nsf and I can see there are still 835,000 deletion stubs. I can't seem to remove them.
Summary:
We have a large database that we are now ready to start trimming down to a more manageable size.
I deleted the documents, but I am left with a database it appears I can't copy or replicate, receiving error: Unable to extend an ID table - insufficient memory.
Detail:
Current live database has roughly 1,400,000 docs in it, we can immediately reduce it to 650,000.  With some further design\ architectural work we can reduce this to 300,000.  This web application is clustered on 5 servers, 1 being the application "hub", with 3 HTTP\SSO servers behind a reverse proxy and the 5th server is the access point for external services.  The HUB initiates a PUSH\PULL replication (with no document restrictions) every 30mins, logs show no replication errors.  Transaction logging is set for Run Time performance on servers, back up is performed on another back server not in cluster.  We run daily archiving that archives roughly 1,500 docs a day.
So far, nearly 2 years on, application has run fine...apart from a few nightmares here and there :-).  But, we are finally at point we can trim the databases down.
Prior to any work I OS backed up the live DB, and copied it to a test server. On a test server I did a: CL copy mylivedb.nsf mycleandb.nsf
In mycleandb.nsf I then deleted docs to reduce the DB to 650,000 documents.
All I'm after is a new copy, not a replica, see replica issue below, so I issued another command...to give me a new copy without deletion stubs, it's my understanding that a COPY does not copy del' stubs.:
CL copy mycleandb.nsf mycleandbNEW.nsf
...this also ensures that if any rogue replica is out on the network, we don't get allt he docs back in (not all our servers are 8.5.3, so we can't use the Database Property to set a cut off date for deletion stubs)
But I got a: Unable to extend an ID table - insufficient memory.
Tried this:
CL copy mycleandb.nsf mycleandbNEW.nsf REPLICA
...same thing.
Tried steps found here to my mycleandb.nsf:
http://www-01.ibm.com/support/docview.wss?uid=swg21220384
...same thing.
Changed purge interval to 0 days as described here, also set purge date into future:
http://www-01.ibm.com/support/docview.wss?uid=swg21095683
...same thing.
Then ran a:
load compact mycleandb.nsf -B
...same thing.
I have seen this thread here, which is a similar situation, except, we don't have any issues with clustering\ replication...yet!
http://www-10.lotus.com/ldd/nd85forum.nsf/DateAllFlatWeb/74d3e0f5467f843885257aaf0081abe5?OpenDocument
So, I have a DB I have deleted docs from, I can open it, it appears to function, but I can't copy\ replicate it.
Replication\ Cluster Issue:
I have seen this error before: Unable to extend an ID table - insufficient memory...and what I have done, is drop one of our primary servers, OS copy the DB to server with issue, restart.
I have always created new replicas using Admin Client\ Dom console, but in this case, whenever I do it floods the database with documents, but purge intervals are correct on all servers. The new replicas have been created from the HUB server, which initiates PUSH\PULL replication with all the cluster mates every 30mins, so if this was a deletion stub issue, it would manifest itself all the time?
I will NotesPeek it tomorrow to see if any deletion stubs are still there, but I am not sure how to proceed.
Any comments or suggestions gratefully received.
Comment: when you write "same thing." do you mean you also get the error " Unable to extend an ID table - insufficient memory" (especially when trying  File --> Replication --> Settings, and entering 0 (zero) days in the "Remove documents not modified in the last" which has always be my good friend)?
Comment: Could you File copy **without** document and then make a simple action agent that copy all docs to the new DB? You will have to replicate this new DB on all your servers. Probably proceed 2 steps to copy the docs.
Comment: @Emmanuel  Thanks for your replies.  To answer your first question, tried the zero days.  There's actually a POST on web, forget url right now, that says also try setting to 1 then 0.  I've tried setting future dates on the "Other" tabs to 2 days into future etc.  From Richard's post below, I may have run into some limitation...but to answer your second question, that's exactly what I did.  I created a new blank copy, and ran an agent to copy across the docs, took 30mins for 600,000, so not bad.  Next step will mean I can reuce to 300,000.
Answer: In my experience, the code in Domino that actually purges deletion stubs cannot work when the total number of modified docs + stubs is too high.  I think it's probably a limitation that's pretty deep in the Notes API internals, combined with the algorithm that has to be used -- which is to look at all notes modified before the purge interval date -- a potentially very large number.
Your best option may be  to make a local non-replica copy and re-deploy.
I researched this a while back.  I found some LotusScript code that uses Notes C API calls to purge deletion stubs posted on various blogs.  (I think the original code may have come from Rod Whitely, but I'm not sure.)  My version code is below.
When run on a database where the total of docs + stubs is somewhere between 2 and 3 million, it gets the "Unable to extend an ID table - insufficient memory".  I never did contact IBM support about this, as it was really just a side project for me.  I ended up just making non-replica copies of the production databases and then setting the purge interval low enough so that the number of stubs never got too high again.
<code>Declare Private Sub IDDestroyTable Lib wAPIModule Alias "IDDestroyTable" _
( ByVal hT As Long)
Declare Private Function IDScan Lib wAPIModule Alias "IDScan" _
( ByVal hT As Long, ByVal F As Integer, ID As Long) As Integer
Declare Private Function NSFDbOpen Lib wAPIModule Alias "NSFDbOpen" _
( ByVal P As String, hDB As Long) As Integer
Declare Private Function NSFDbClose Lib wAPIModule Alias "NSFDbClose" _
( ByVal hDB As Long) As Integer
Declare Private Function NSFDbGetModifiedNoteTable Lib wAPIModule Alias "NSFDbGetModifiedNoteTable" _
( ByVal hDB As Long, ByVal C As Integer, ByVal S As Currency, U As Currency, hT As Long) As Integer
Declare Private Function NSFNoteDelete Lib wAPIModule Alias "NSFNoteDelete" _
( ByVal hDB As Long, ByVal N As Long, ByVal F As Integer) As Integer
Declare Private Function OSPathNetConstruct Lib wAPIModule Alias "OSPathNetConstruct" _
( ByVal NullPort As Long, ByVal Server As String, ByVal FIle As String, ByVal PathNet As String) As Integer
Declare Private Sub TimeConstant Lib wAPIModule Alias "TimeConstant" _
( ByVal C As Integer, T As Currency)

Function countAndDeleteStubsByOpenDatabase(db As NotesDatabase, deleteFlag As boolean) As Long

        If db Is Nothing GoTo bail

        Dim forever As Currency
        Dim last As Currency
        Dim hT As Long
        Dim RRV As Long
        Dim hDB As Long
        Dim path As String
        Dim nStubs As Long
        Dim ret As integer

        On Error GoTo bail

        path = Space(1024)
        Call OSPathNetConstruct(0, db.Server, db.FilePath, path)
        Call NSFDbOpen(path, hDB)

        Call TimeConstant(2, forever)
        Call NSFDbGetModifiedNoteTable(hDB, &H7FFF, forever, last, hT)

        nStubs = 0

        ret = IDScan(hT, True, RRV)

        While Not (ret = 0)
            If RRV < 0 Then
                If (deleteFlag = true) Then
                    NSFNoteDelete hDB, RRV And &H7FFFFFFF, &H0201
                End If
                nStubs = nStubs + 1
            End If
            ret = IDScan(hT, False, RRV)
        Wend

        IDDestroyTable hT
        NSFDbClose hDB

        If deleteFlag = True Then
            Print "Deleted " + CStr(nStubs) + " stubs for " + db.Filepath  + " on " + db.Server
        Else
            Print "Counted " + CStr(nStubs) + " stubs for " + db.Filepath  + " on " + db.Server
        End If

        countAndDeleteStubsByOpenDatabase = nStubs

        On Error GoTo 0

        Exit Function

        bail:
            Print "Unexpected error in countAndDeleteStubsByOpenDatabase. Line " + CStr(Erl) + " " + CStr(Err()) + " " + Error(Err())
            countAndDeleteStubsByOpenDatabase = 0

    End Function
</code>
I never tried it, but it also occurred to me that it might be possible to modify this code to just search for the most recent one day's worth of stubs, delete them, then go back one more day and get the most recent stubs, delete those, etc.  It might not be all that easy to do in LotusScript, though, given that you have to deal with the C TIMEDATE struc as a Currency field.  You might have to do it in C.  Of course, deleting the newest stubs first is the exact reverse of what you want to do in practice, but this strategy might work around the limitation on idtables.
Comment: Thanks Richard for your reply.
Comment: @nickwall If you resolve this, either by calling IBM Support (perhaps they have a tool...) or on your own, I would love to hear the outcome, especially if it doesn't involve making new non-replica copies.
Comment: @Richard...My post is too long...trimming it. I've followed your blogs\ writings for years with great interest, so firstly...hi there.  I have resolved the issue, but unfortunately not as a non-replica.   I was really constrained by time.  I tried the agent you posted above, but it would crash my dev 8.5.3 server on the NSFDbOpen line...will verify. I deleted docs, copied to another DB. If deleted and use purge interval, only seemed to remove 10,000ish stubs at a time, too long for 835,000 stubs. I did del' 600K docs other day and did CL COPY, did not check via SH DB, so maybe were stubs.
Comment: I'm moving another 300,000 docs out just after Xmas, will be a more "relaxed" process time wise.  I'll try the agent again, and will post results here.
Comment: I haven't blogged in so long!  I'm glad you remember me. Thanks!  Re crashing the server... Hmmm... I always ran it on a client.  Is it by any chance a 64 bit server?  I don't think I've ever tried calling C API functions from LotusScript on Win64 + 64bit Domino, and it wouldn't surprise me if the declarations would have to be adjusted. That's just a guess, of course.
Answer: http://www.ytria.com/ has a tool ScanEZ that has a function to delete deletion stubs comfortably. I'm not sure if it would help if you're hitting against internal Notes limits as Richard suggested, but it might be worth a try.


Finding the dimension of range and null space given a matrix and determining bases

Question: I am attempting to solve the following problem.
I am given the matrix A=\begin{pmatrix}
        1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 \\
        \end{pmatrix}
Specifically I am asked to: (1) find the dimension of the range of this matrix, (2) give a range for this subspace, (3) find the dimension of the null space of this matrix, and (4) give a basis for this subspace.
I just keep staring at the problem but can't seem to put my finger on how to tackle it since I am not given a linear transformation.
Thanks in advance for the help!
Comment: Just regard $A$ as a linear transformation. Then, range will be a set $\{A\mathbf{x}:\mathbf{x}\in\mathbb{R}^4\}$ and null space be $\{\mathbf{x}\in\mathbb{R}^4:A\mathbf{x}=\mathbf{0}\}$.
Answer: Note that the $\ker A$ has dimension $3$ and is spanned by vector $(1,0,0,-1) ,(0,1,0,-1) , (0,0,1,-1)$ while $ Im A$ is spanned by vector $(1,1,1,1)$ Indeed to find $\ker A$ you have to solve a linear system $$\begin{pmatrix}
        1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 \\
        \end{pmatrix}\cdot
\begin{pmatrix}
        X_1 \\
        X_2 \\
        X_3 \\
        X_4\\
        \end{pmatrix}= vector zero$$
Therefore you find that $X_1+X_2+X_3+X_4=0$
Comment: So when you mention that we have to solve a linear system to find ker A, are you saying that is how you find those spanning vectors? (1,0,0-1), (0,1,0,-1), and (0,0,1,-1)? Also, since you only have one linear equation to work with, are you basically just playing around with numbers and seeing what works?
Comment: @KingTut No, you don't "just play around with numbers," although in this case it is pretty easy so come up with a solution by inspection. You can use any of several methods to solve this system of linear equations, such as row-reduction. Once you have the row-reduced echelon form of the matrix, you can basically read off a basis for its kernel.
Comment: @amd Could you please provide such an example?
Comment: @KingTut There are several examples [here](http://math.stackexchange.com/a/1521354/265466).
Comment: @amd Thanks amd!


Resolving External DNS from Azure Virtual Network

Question: I've set up a virtual network in Windows Azure to host two Linux VMs (Centos / OpenLogic). I've registered a local DNS (local as in the cloud, not my personal network) inside the virtual network. I configured both VMs to use the virtual network and I am able to SSH into the boxes no problem. However, these boxes are not able to resolve external addresses (IP or DNS) and as a result, I am unable to use the package manager to install tools on these boxes. Is there a way that I can configure my virtual network to have an external gateway so that my VMs can access the outside world?
Comment: What kind of local dns are you using? Maybe you need to configure forwarders so they are able to resolve external adresses.
Answer: You can setup your local DNS server to forward queries to non-local addresses to external DNS server on the internet. This article provides instructions for how to achieve this on Ubuntu by forwarding unresolved addresses to Google's public DNS servers; you may be able to adapt the instructions for CentOS.


Ruby cabypara, execute script to hide elements not working

Question: So i am trying to take screenshots of web pages with cabypara/poltergeist, but there are certain elements that i want to hide, but executing the JQuery script to hide elements seems to be not working, i even tried hiding all elements and still the screenshot would show everything. Here's the code
<code>Capybara.register_driver(:poltergeist) { |app| Capybara::Poltergeist::Driver.new(app, js_errors: false) }
Capybara.default_driver = :poltergeist
internet = Capybara.current_session
internet.visit 'https://www.google.com/'
internet.execute_script %Q{$("*").hide();}
internet.save_screenshot 'image.png'
</code>
What's the problem here ?
Answer: A quick guess would be that the google homepage doesn't load JQuery, so you can't use JQuery methods in your script.  You will need to rely on the basic DOM methods.