text:
Q: CUDA matrix transpose with shared memory I need to implement a matrix transpose function on a GPU using shared memory. I have done it in a simple way without shared memory which works fine and also an attempt with SM. But unfortunately the calculation is not correct and I can not figure out why. A complete working example can be found here and at the bottom of this question.
EDIT 1
I further know that the first index of the result where I have a wrong value is index 32 (of the flatted matrix, so matr[0][32] in a two dimensional fashion).
If there are any more information neede I will prvide them gladly.
A short extract from the entire code which resembles the not working function is listed below:
__global__ void notSoNaivaTransKernel(float *matrB, float *matrA, const int width,
    const int height, const int nreps)
{
    __shared__ float tile[TILE_DIM][TILE_DIM + 1];
    int blockIdx_y = blockIdx.x;
    int blockIdx_x = (blockIdx.x + blockIdx.y) % gridDim.x;
    int xIndex = blockIdx_x * TILE_DIM + threadIdx.x;
    int yIndex = blockIdx_y * TILE_DIM + threadIdx.y;
    int index_in = xIndex + (yIndex)* width;

    xIndex = blockIdx_y * TILE_DIM + threadIdx.x;
    yIndex = blockIdx_x * TILE_DIM + threadIdx.y;
    int index_out = xIndex + (yIndex)* height;

    int r, i;
#pragma unroll
    for (r = 0; r < nreps; r++)
    {
#pragma unroll
        for (i = 0; i < TILE_DIM; i += BLOCK_ROWS)
            tile[threadIdx.y + i][threadIdx.x] = matrA[index_in + i * width];

        __syncthreads();

#pragma unroll
        for (i = 0; i < TILE_DIM; i += BLOCK_ROWS)
            if (index_in + i * width < width * height)
               matrB[index_out + i * height] = tile[threadIdx.x][threadIdx.y + i];
    }
}

The output looks like this:
Avg. CPU Transpose Time: 0.106048 ms, Bandwidth: 3.771873 GB/s

Avg. GPU Naive Trans Time: 0.009871 ms, bandwidth: 40.520836 GB/s
    Correct: 50000, Wrong: 0

Avg. GPU Trans with SM Time: 0.007598 ms, bandwidth: 52.643482 GB/s
    Correct: 12352, Wrong: 37648

Here is the full working example. I stripped most of the unnecessary code from it so it is less stuffed:
#include "cuda_runtime.h"
#include "device_functions.h"
#include "device_launch_parameters.h"

#include <chrono>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#define TILE_DIM 32
#define BLOCK_ROWS 8
#define BLOCK_COLS 32

cudaError_t matrMagicCuda(float *matrB, float *matrA, const int width, const int height, const int nreps, const int operation);
void cpuMatrTrans(float *matrB, float *matrA, const int width, const int height, const int nreps);
__global__ void naiveTransKernel(float *matrB, float *matrA, const int width, const int height, const int nreps);
__global__ void notSoNaivaTransKernel(float *matrB, float *matrA, const int width, const int height, const int nreps);

int main()
{
    int i, width, height, nreps, size, wrong, correct;
    double cpuTime, cpuBandwidth;
    cudaError_t cudaStatus;

    float *matrA, *matrATC, *matrATG, *matrAC;

    srand(time(NULL));

    nreps = 10000;
    width = 500;
    height = 100;
    size = width * height;

    matrA = (float*)malloc(size * sizeof(float)); // matrix A
    matrAC = (float*)malloc(size * sizeof(float)); // matrix A copied
    matrATC = (float*)malloc(size * sizeof(float)); // matrix A transposed by CPU
    matrATG = (float*)malloc(size * sizeof(float)); // matrix A transposed by GPU

    for (i = 0; i < size; i++)
    {
        matrA[i] = (float)i;
    }

    auto start = std::chrono::high_resolution_clock::now();

    //CPU Transpose
    cpuMatrTrans(matrATC, matrA, width, height, nreps);

    auto end = std::chrono::high_resolution_clock::now();

    std::chrono::duration<double> diff = end - start;
    cpuTime = (diff.count() * 1000) / nreps;
    cpuBandwidth = (sizeof(float) * size * 2) / (cpuTime * 1000000);//scaling from ms to s and B to GB doen implicitly, shortened in fraction, times two for read and write
    printf("Avg. CPU Transpose Time: %f ms, Bandwidth: %f GB/s\n\n", cpuTime, cpuBandwidth);

    correct = 0;
    wrong = 0;

    //Naive transpose
    matrMagicCuda(matrATG, matrA, width, height, nreps, 1);

    //Check if calc was correct
    for (i = 0; i < size; i++)
    {
        if (matrATC[i] != matrATG[i])
        {
            /*printf("ERROR - %d - ATC:%f - ATG:%f\n\n", i, matrATC[i], matrATG[i]);
            return;*/
            wrong++;
        }
        else
        {
            correct++;
        }
    }

    printf("\tCorrect: %d, Wrong: %d\n\n", correct, wrong);
    correct = 0;
    wrong = 0;

    //Transpose with shared memory
    matrMagicCuda(matrATG, matrA, width, height, nreps, 2);

    //Check if calc was correct
    for (i = 0; i < size; i++)
    {
        if (matrATC[i] != matrATG[i])
        {
            /*printf("ERROR - %d - ATC:%f - ATG:%f\n\n", i, matrATC[i], matrATG[i]);
            return;*/
            wrong++;
        }
        else
        {
            correct++;
        }
    }

    //printf("\tTranspose with SM on GPU was executed correctly.\n\n");
    printf("\tCorrect: %d, Wrong: %d\n\n", correct, wrong);
    correct = 0;
    wrong = 0;

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaDeviceReset failed!\n");
        return 1;
    }

    return 0;
}

cudaError_t matrMagicCuda(float *matrB, float *matrA, const int width, const int height, const int nreps, const int operation)
{
    float elapsed = 0;
    float *dev_matrA = 0;
    float *dev_matrB = 0;
    cudaError_t cudaStatus;
    dim3 dim_grid, dim_block;
    double gpuBandwidth;

    int size = width * height;

    dim_block.x = TILE_DIM;
    dim_block.y = BLOCK_ROWS;
    dim_block.z = 1;

    dim_grid.x = (width + TILE_DIM - 1) / TILE_DIM;
    dim_grid.y = (height + TILE_DIM - 1) / TILE_DIM;
    dim_grid.z = 1;

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaStatus = cudaSetDevice(0);
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
        goto Error;
    }

    // Allocate GPU buffers for three matrix
    cudaStatus = cudaMalloc((void**)&dev_matrA, size * sizeof(float));
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_matrB, size * sizeof(float));
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    // Copy input matrix from host memory to GPU buffers.
    cudaStatus = cudaMemcpy(dev_matrA, matrA, size * sizeof(float), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    cudaEvent_t start, stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);

    switch (operation)
    {
        case(1):
        {
            cudaEventRecord(start);
            // Launch a kernel on the GPU with one thread for each element.
            naiveTransKernel << <dim_grid, dim_block >> >(dev_matrB, dev_matrA, width, height, nreps);

            cudaEventRecord(stop);
            cudaEventSynchronize(stop);

            cudaEventElapsedTime(&elapsed, start, stop);
            cudaEventDestroy(start);
            cudaEventDestroy(stop);

            elapsed /= nreps;

            gpuBandwidth = (sizeof(float) * size * 2) / (elapsed * 1000000);//scaling from ms to s and B to GB doen implicitly, shortened in fraction, times two for read and write
            printf("Avg. GPU Naive Trans Time: %f ms, bandwidth: %f GB/s\n", elapsed, gpuBandwidth);

            break;
        }

        case(2):
        {
            cudaEventRecord(start);
            // Launch a kernel on the GPU with one thread for each element.
            notSoNaivaTransKernel << <dim_grid, dim_block >> >(dev_matrB, dev_matrA, width, height, nreps);

            cudaEventRecord(stop);
            cudaEventSynchronize(stop);

            cudaEventElapsedTime(&elapsed, start, stop);
            cudaEventDestroy(start);
            cudaEventDestroy(stop);

            elapsed /= nreps;

            gpuBandwidth = (sizeof(float) * size * 2) / (elapsed * 1000000);//scaling from ms to s and B to GB doen implicitly, shortened in fraction, times two for read and write
            printf("Avg. GPU Trans with SM Time: %f ms, bandwidth: %f GB/s\n", elapsed, gpuBandwidth);

            break;
        }

    default:
        printf("No matching opcode was found.\n");
    }

    // Check for any errors launching the kernel
    cudaStatus = cudaGetLastError();
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "Kernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
        goto Error;
    }

    // cudaDeviceSynchronize waits for the kernel to finish, and returns
    // any errors encountered during the launch.
    cudaStatus = cudaDeviceSynchronize();
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching Kernel!\n", cudaStatus);
        goto Error;
    }

    // Copy output matrix from GPU buffer to host memory.
    cudaStatus = cudaMemcpy(matrB, dev_matrB, size * sizeof(float), cudaMemcpyDeviceToHost);
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

Error:
    cudaFree(dev_matrB);
    cudaFree(dev_matrA);

    return cudaStatus;
}

void cpuMatrTrans(float *matrB, float *matrA, const int width, const int height, const int nreps)
{
    int i, j, r;

#pragma unroll
    for (r = 0; r < nreps; r++)
#pragma unroll
        for (i = 0; i < height; i++)
#pragma unroll
            for (j = 0; j < width; j++)
                matrB[j * height + i] = matrA[i * width + j];
}

__global__ void naiveTransKernel(float *matrB, float *matrA, const int width, const int height, const int nreps)
{
    int i, r;
    int row = blockIdx.x * TILE_DIM + threadIdx.x;
    int col = blockIdx.y * TILE_DIM + threadIdx.y;
    int index_in = row + width * col;
    int index_out = col + height * row;

#pragma unroll
    for (r = 0; r < nreps; r++)
#pragma unroll
        for (i = 0; i < TILE_DIM; i += BLOCK_ROWS)
            if (index_in + i * width < width * height)
                matrB[index_out + i] = matrA[index_in + i * width];
}

__global__ void notSoNaivaTransKernel(float *matrB, float *matrA, const int width, const int height, const int nreps)
{
    __shared__ float tile[TILE_DIM][TILE_DIM + 1];
    int blockIdx_y = blockIdx.x;
    int blockIdx_x = (blockIdx.x + blockIdx.y) % gridDim.x;
    int xIndex = blockIdx_x * TILE_DIM + threadIdx.x;
    int yIndex = blockIdx_y * TILE_DIM + threadIdx.y;
    int index_in = xIndex + (yIndex)* width;

    xIndex = blockIdx_y * TILE_DIM + threadIdx.x;
    yIndex = blockIdx_x * TILE_DIM + threadIdx.y;
    int index_out = xIndex + (yIndex)* height;

    int r, i;
#pragma unroll
    for (r = 0; r < nreps; r++)
    {
#pragma unroll
        for (i = 0; i < TILE_DIM; i += BLOCK_ROWS)
            tile[threadIdx.y + i][threadIdx.x] = matrA[index_in + i * width];

        __syncthreads();

#pragma unroll
        for (i = 0; i < TILE_DIM; i += BLOCK_ROWS)
            if (index_in + i * width < width * height)
               matrB[index_out + i * height] = tile[threadIdx.x][threadIdx.y + i];
    }
}

A: There were a number of issues with this code.  I'm not sure I will be able to cover all of them.
Possibly the most important issue was your lack of (and lack of understanding of) a proper 2D thread check.  Your algorithm creates a grid of threads that is larger in both dimensions than the problem size.  This creates logical threads outside of the dimension of your matrix, in both dimensions.
You have attempted to create a 2D thread check like this:
        if (index_in + i * width < width * height)

This will not work.  Suppose I have a 3x3 matrix and a 4x4 grid of threads.  The thread at (3,0) is clearly out-of-bounds for your matrix, but would pass your 2D thread check.
In this situation, a proper thread check must test each dimension separately, not as a product.
Note that this logical error exists in your "naive" transpose kernel as well, and you can confirm that if you run your code with cuda-memcheck.  It will indicate out-of-bounds access errors in that kernel, even though it appears to work correctly.
There were various other problems as well. Most of these had to do with indexing in your shared memory kernel.  It's not clear to me that you understood the necessary indexing manipulation for a shared memory transpose.  There are two separate indexing transposes that we must do in this case:

*

*Transpose the block (tile) indices

*Transpose the thread indices

Transposition of the thread indices is done upon reading/writing shared memory.  You have this correctly accounted for with a reversal of the use of threadIdx.x and threadIdx.y for read/write of shared memory. But as near as I can tell, your index generation for the reversal of block indices (which reversal is used during the reading/writing to global memory) was broken.  That was the other major issue to be sorted out.
The following code has these and some other issues fixed, and appears to work correctly for me:
$ cat t33.cu
#include <chrono>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#define TILE_DIM 32
#define BLOCK_ROWS 8
#define BLOCK_COLS 32

cudaError_t matrMagicCuda(float *matrB, float *matrA, const int width, const int height, const int nreps, const int operation);
void cpuMatrTrans(float *matrB, float *matrA, const int width, const int height, const int nreps);
__global__ void naiveTransKernel(float *matrB, float *matrA, const int width, const int height, const int nreps);
__global__ void notSoNaivaTransKernel(float *matrB, float *matrA, const int width, const int height, const int nreps);

int main()
{
    int i, width, height, nreps, size, wrong, correct;
    double cpuTime, cpuBandwidth;
    cudaError_t cudaStatus;

    float *matrA, *matrATC, *matrATG, *matrAC;

    srand(time(NULL));

    nreps = 10000;
    width = 500;
    height = 100;

    size = width * height;

    matrA = (float*)malloc(size * sizeof(float)); // matrix A
    matrAC = (float*)malloc(size * sizeof(float)); // matrix A copied
    matrATC = (float*)malloc(size * sizeof(float)); // matrix A transposed by CPU
    matrATG = (float*)malloc(size * sizeof(float)); // matrix A transposed by GPU

    for (i = 0; i < size; i++)
    {
        matrA[i] = (float)i;
    }

    auto start = std::chrono::high_resolution_clock::now();

    //CPU Transpose
    cpuMatrTrans(matrATC, matrA, width, height, nreps);

    auto end = std::chrono::high_resolution_clock::now();

    std::chrono::duration<double> diff = end - start;
    cpuTime = (diff.count() * 1000) / nreps;
    cpuBandwidth = (sizeof(float) * size * 2) / (cpuTime * 1000000);//scaling from ms to s and B to GB doen implicitly, shortened in fraction, times two for read and write
    printf("Avg. CPU Transpose Time: %f ms, Bandwidth: %f GB/s\n\n", cpuTime, cpuBandwidth);

    correct = 0;
    wrong = 0;

    //Naive transpose
    memset(matrATG, 0, size*sizeof(float));
    matrMagicCuda(matrATG, matrA, width, height, nreps, 1);

    //Check if calc was correct
    for (i = 0; i < size; i++)
    {
        if (matrATC[i] != matrATG[i])
        {
            /*printf("ERROR - %d - ATC:%f - ATG:%f\n\n", i, matrATC[i], matrATG[i]);
            return;*/
            wrong++;
        }
        else
        {
            correct++;
        }
    }

    printf("\tCorrect: %d, Wrong: %d\n\n", correct, wrong);
    correct = 0;
    wrong = 0;

    //Transpose with shared memory
    memset(matrATG, 0, size*sizeof(float));
    matrMagicCuda(matrATG, matrA, width, height, nreps, 2);

    //Check if calc was correct
    for (i = 0; i < size; i++)
    {
        if (matrATC[i] != matrATG[i])
        {
            /*printf("ERROR - %d - ATC:%f - ATG:%f\n\n", i, matrATC[i], matrATG[i]);
            return;*/
            wrong++;
        }
        else
        {
            correct++;
        }
    }

    //printf("\tTranspose with SM on GPU was executed correctly.\n\n");
    printf("\tCorrect: %d, Wrong: %d\n\n", correct, wrong);
    correct = 0;
    wrong = 0;

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaDeviceReset failed!\n");
        return 1;
    }

    return 0;
}

cudaError_t matrMagicCuda(float *matrB, float *matrA, const int width, const int height, const int nreps, const int operation)
{
    float elapsed = 0;
    float *dev_matrA = 0;
    float *dev_matrB = 0;
    cudaError_t cudaStatus;
    dim3 dim_grid, dim_block;
    double gpuBandwidth;

    int size = width * height;

    dim_block.x = TILE_DIM;
    dim_block.y = BLOCK_ROWS;
    dim_block.z = 1;

    dim_grid.x = (width + TILE_DIM - 1) / TILE_DIM;
    dim_grid.y = (height + TILE_DIM - 1) / TILE_DIM;
    dim_grid.z = 1;

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaStatus = cudaSetDevice(0);
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
        goto Error;
    }

    // Allocate GPU buffers for three matrix
    cudaStatus = cudaMalloc((void**)&dev_matrA, size * sizeof(float));
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_matrB, size * sizeof(float));
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    // Copy input matrix from host memory to GPU buffers.
    cudaStatus = cudaMemcpy(dev_matrA, matrA, size * sizeof(float), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }
    cudaMemset(dev_matrB, 0, size * sizeof(float));
    cudaEvent_t start, stop;
    cudaEventCreate(&start);
    cudaEventCreate(&stop);

    switch (operation)
    {
        case(1):
        {
            cudaEventRecord(start);
            // Launch a kernel on the GPU with one thread for each element.
            naiveTransKernel << <dim_grid, dim_block >> >(dev_matrB, dev_matrA, width, height, nreps);

            cudaEventRecord(stop);
            cudaEventSynchronize(stop);

            cudaEventElapsedTime(&elapsed, start, stop);
            cudaEventDestroy(start);
            cudaEventDestroy(stop);

            elapsed /= nreps;

            gpuBandwidth = (sizeof(float) * size * 2) / (elapsed * 1000000);//scaling from ms to s and B to GB doen implicitly, shortened in fraction, times two for read and write
            printf("Avg. GPU Naive Trans Time: %f ms, bandwidth: %f GB/s\n", elapsed, gpuBandwidth);

            break;
        }

        case(2):
        {
            cudaEventRecord(start);
            // Launch a kernel on the GPU with one thread for each element.
            notSoNaivaTransKernel << <dim_grid, dim_block >> >(dev_matrB, dev_matrA, width, height, nreps);

            cudaEventRecord(stop);
            cudaEventSynchronize(stop);

            cudaEventElapsedTime(&elapsed, start, stop);
            cudaEventDestroy(start);
            cudaEventDestroy(stop);

            elapsed /= nreps;

            gpuBandwidth = (sizeof(float) * size * 2) / (elapsed * 1000000);//scaling from ms to s and B to GB doen implicitly, shortened in fraction, times two for read and write
            printf("Avg. GPU Trans with SM Time: %f ms, bandwidth: %f GB/s\n", elapsed, gpuBandwidth);

            break;
        }

    default:
        printf("No matching opcode was found.\n");
    }

    // Check for any errors launching the kernel
    cudaStatus = cudaGetLastError();
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "Kernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
        goto Error;
    }

    // cudaDeviceSynchronize waits for the kernel to finish, and returns
    // any errors encountered during the launch.
    cudaStatus = cudaDeviceSynchronize();
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching Kernel!\n", cudaStatus);
        goto Error;
    }

    // Copy output matrix from GPU buffer to host memory.
    cudaStatus = cudaMemcpy(matrB, dev_matrB, size * sizeof(float), cudaMemcpyDeviceToHost);
    if (cudaStatus != cudaSuccess)
    {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

Error:
    cudaFree(dev_matrB);
    cudaFree(dev_matrA);

    return cudaStatus;
}

void cpuMatrTrans(float *matrB, float *matrA, const int width, const int height, const int nreps)
{
    int i, j, r;

#pragma unroll
    for (r = 0; r < nreps; r++)
#pragma unroll
        for (i = 0; i < height; i++)
#pragma unroll
            for (j = 0; j < width; j++)
                matrB[j * height + i] = matrA[i * width + j];
}

__global__ void naiveTransKernel(float *matrB, float *matrA, const int width, const int height, const int nreps)
{
    int i, r;
    int col = blockIdx.x * TILE_DIM + threadIdx.x;
    int row = blockIdx.y * TILE_DIM + threadIdx.y;
    int index_in = col + width * row;
    int index_out = row + height * col;

#pragma unroll
    for (r = 0; r < nreps; r++)
#pragma unroll
        for (i = 0; i < TILE_DIM; i += BLOCK_ROWS)
            if ((row+i<height) && (col < width))
                matrB[index_out + i] = matrA[index_in + i * width];
}

__global__ void notSoNaivaTransKernel(float *matrB, float *matrA, const int width, const int height, const int nreps)
{
    __shared__ float tile[TILE_DIM][TILE_DIM + 1];
    int ciIndex = blockIdx.x * TILE_DIM + threadIdx.x;
    int riIndex = blockIdx.y * TILE_DIM + threadIdx.y;
    int coIndex = blockIdx.y * TILE_DIM + threadIdx.x;
    int roIndex = blockIdx.x * TILE_DIM + threadIdx.y;
    int index_in = ciIndex + (riIndex)* width;
    int index_out = coIndex + (roIndex)* height;

    int r, i;
#pragma unroll
    for (r = 0; r < nreps; r++)
    {
#pragma unroll
        for (i = 0; i < TILE_DIM; i += BLOCK_ROWS)
            if ((ciIndex<width) && (riIndex+i < height))
              tile[threadIdx.y + i][threadIdx.x] = matrA[index_in + i * width];
        __syncthreads();

#pragma unroll
        for (i = 0; i < TILE_DIM; i += BLOCK_ROWS)
            if ((coIndex<height) && (roIndex+i < width))
               matrB[index_out + i*height] = tile[threadIdx.x][threadIdx.y + i];
        __syncthreads();
    }
}
$ nvcc -std=c++11 -arch=sm_61 -o t33 t33.cu
t33.cu(25): warning: variable "matrAC" was set but never used

t33.cu(25): warning: variable "matrAC" was set but never used

$ cuda-memcheck ./t33
========= CUDA-MEMCHECK
Avg. CPU Transpose Time: 0.143087 ms, Bandwidth: 2.795509 GB/s

Avg. GPU Naive Trans Time: 0.028587 ms, bandwidth: 13.992195 GB/s
        Correct: 50000, Wrong: 0

Avg. GPU Trans with SM Time: 0.040328 ms, bandwidth: 9.918678 GB/s
        Correct: 50000, Wrong: 0

========= ERROR SUMMARY: 0 errors
$ ./t33
Avg. CPU Transpose Time: 0.140469 ms, Bandwidth: 2.847594 GB/s

Avg. GPU Naive Trans Time: 0.003828 ms, bandwidth: 104.505440 GB/s
        Correct: 50000, Wrong: 0

Avg. GPU Trans with SM Time: 0.000715 ms, bandwidth: 559.206604 GB/s
        Correct: 50000, Wrong: 0

$

Note: The code attempts to measure bandwidth.  However, you should be aware that the measured bandwidth here is affected by the cache bandwidth.  Your matrix sizes (500x100 = 200Kbytes each for input and output) here are easily small enough to fit in the L2 cache on most GPUs.  This fact, coupled with the fact that you are running the same transpose multiple times (nreps) means that much of the work is operating directly out of the L2 cache.   Therefore, in the "optimized" case above, we see a reported bandwidth number that greatly exceeds the available memory bandwidth of the GPU (this case happens to be a Pascal Titan X, so about ~340GB/s of main memory bandwidth available).  This is due to the fact that this measurement includes some benefit from the L2 cache, whose bandwidth is at least twice as high as main memory bandwidth.  You can eliminate this effect by using a significantly larger matrix size and/or reducing nreps to 1.


text:
Q: QtCreator plugins and templates missing I just downloaded the Ubuntu phone SDK and QT creator according to the instructions.
When I fire up QTCreator, I get a screen as shown

If I ignore this message and hit continue, there are no templates to create the Mobile application as indicated in the tutorial I'm following.

I tried installing qt5-default and qtchooser - problem still persists. Here is the error message
Cannot load plugin because dependency failed to load: RemoteLinux(2.6.82)
Reason: Cannot load plugin because dependency failed to load: QtSupport(2.6.82)
Reason: Cannot load plugin because dependency failed to load: ProjectExplorer(2.6.82)
Reason: /usr/lib/i386-linux-gnu/qtcreator/plugins/QtProject/libProjectExplorer.so:
Cannot load library /usr/lib/i386-linux-gnu/qtcreator/plugins/QtProject/libProjectExplorer.so:
(libQt5Declarative.so.5: cannot open shared object file: No such file or directory)

A: I ran into the same problem earlier today when I updated my system and opened QT Creator. I found that the the ubuntu-sdk package didn't update itself properly. Try running this command to fix the problem:
sudo apt-get install ubuntu-sdk notepad-qml

I am sure you already ran this to initially install QT Creator, but running this again helped me. I cannot guarantee this will fix the issue, it worked for me.
Hope this helps!

Plus run this as well:
sudo apt-get install libbotan*

A: As David mentioned rerunning this command seemed to help:
sudo apt-get install ubuntu-sdk notepad-qml

The only other thing for me was I had to remove the previous install first and run another update before reinstalling would work.
To uninstall run the following command.
sudo apt-get purge ubuntu-sdk notepad-qml

Then update the sources and reinstall the packages.
sudo apt-get update && sudo apt-get install ubuntu-sdk notepad-qml

I am running 12.10 so I don't know if this is the same for each release.

redpajama_set_name: RedPajamaStackExchange


text:
Q: Refresh Form/DaataGridView coming from another windows Pretty new to C# and I'm having a little trouble.
I'm using WinForm and am supposed to, by the click of a button, close the window I'm currently on and refresh an already existing window/the content of the DataGrid it displays. How would one go to solve this?
I've been looking through this site and none of the suggested solutions were helpful to me.


text:
Q: Functional Derivatives and their Units How does taking a functional derivative affect the units of the functional?
As an example, considering $\mathcal{F} = \mathcal{F}[h,\nabla h]$ where $\mathcal{F}$ is the free energy functional of some fluid system and $h$ represents the film height. Then in fundamental units we would have: $[\mathcal{F}]=\frac{ML^2}{T^2}$. My question is what would the units of the variational derivative be: $[\frac{\delta \mathcal{F}}{\delta h}]$? Is it the same as taking a regular or partial derivative of a function? i.e. would it just have the units of energy over length?


text:
Q: How to write a regex code for password criteria? I'm trying to create a regex code to match the following criteria but have thus far been unable.

*

*At least two upper-case letters

*At least two lower-case letters

*At least one number.

Here is my code:
At the moment I have used these 2 expressions to check for length
  if (String.valueOf(string1).length() <= 8){j =0;}
  if (!string1.matches("[A-Za-z0-9]*")){j =0;}

.int j is a variable im using to check if the conditions are met!

A: Well I see a non-regex way to go about it:
public boolean isValidPassword(String s) {
    int lowerCase = 0;
    int upperCase = 0;
    int numeric = 0;
    int special = 0;
    for (int c = 0; i < s.length(); i++) {
        char i = s.charAt(c);
        if (i >= 'A' && i <= 'Z') {
            upperCase++;
        } else if (i >= 'a' && i <= 'z') {
            lowerCase++;
        } else if (i >= '0' && i <= '9') {
            numeric++;
        } else {
            special++;
        }
        //early ending case
        return lowerCase > 1 && upperCase > 1 && numeric > 0;
    }
    return false;
}

If you wanted to you could abstract the early ending predicate using a functional interface:
@FunctionalInterface
private static interface PasswordMatch {
    public boolean match(int lowerCase, int upperCase, int numbers, int special);
}

public boolean isValidPassword(String s, PasswordMatch matcher) {
    //...
        //in loop
        if (matcher.match(lowerCase, upperCase, numeric, special) {
            return true;
        }
    //...
}

Thus making the call mutable to the situation:
if (isValidPassword(/* some pass */, (lower, upper, number, spec)
                -> lowerCase > 1 && upperCase > 1 && numeric > 0)) {
    //etc...
}


text:
Q: Implementing custom sytem.diagnostics for a managed application I am facing a difficult problem. I need to implement a custom diagnostics section just like the system.diagnostics in the app.config for a managed application. The scenario is as follows:
I have an XML file which has a list of trace sources and listeners associated with each of the trace sources.And there is other stuff in the XML file which is not related to the tracing.What I need to do is, read the XML file and set up the trace sources and listeners just like .NET does at application start-up. When I do something like:
TraceSource sample = new TraceSource("SampleTraceSource"); the application should be able to set up the trace source "SampleTraceSource" that is in my XML file, rather than pick it up from the app.config. As of now, .NET does all this behind the scenes so you needn't worry about how it is implemented. I believe this is done in System.dll when the application starts.
Any ideas?
Thanks

A: Your question seems to be a little vague but I'll attempt to help with a couple key areas.
You can find many examples on the net for reading the XML file.  My personal preference is to use the XmlDocument class and children thereof such as XmlNode, XmlElement, etc.  Alternatively you can look to use the XmlReader.  Both approaches have plenty of resources available.
The loading of trace sources will be handled using members of the System.Reflection Namespace.  Have a look at that namespace, probably at methods such as Assembly.Load, and then perhaps come back here with some more specific questions if you get stuck?
Hope that helps.


text:
Q: SSRS collapse empty field and collapse space also My problem in SSRS: address lines 2 and 3 have no data
Pit ltd.
230 Sunset Blvd.
Field: Address Line 2
Field: Address Line 3
Singapore
Solution should be:
Pit ltd.
230 Sunset Blvd.
Singapore
How can I collapse these fields and also save space?
Edit: Alle these fields have to be single fields placed on the canvas. I cannot use a table due to different formattings for the fields.

A: You can add a visibility expression for the rows. You can also do this at a group level. Right click and go to row visibility.

Select "Show or hide based on an expression" radio button.
Click the expression button.
In the expression window, use Fields!AddressLine2.Value = NOTHING
Do the same for Address 3. Note I'm assuming your fields are named as displayed.
Update
For the same idea on a text box, just right-click the text box, go to text box properties, visibility, and use the same expression for the third option - "Show or hide based on an expression."


text:
Q: How to fix 'Failed to initialize end point associated with ProtocolHandler' error? I have installed Tomcat 7 in my Ubuntu 12.04.
Somehow Tomcat is not starting. Tried out all possible steps, checked out forums; nothing helped.
Can anyone tell me what went wrong?
Log from my PC, for the command:
~$ sh  /usr/share/tomcat7/bin/catalina.sh run

Using CATALINA_BASE:   /usr/share/tomcat7
Using CATALINA_HOME:   /usr/share/tomcat7
Using CATALINA_TMPDIR: /usr/share/tomcat7/temp
Using JRE_HOME:        /usr/lib/jvm/java-6-openjdk/jre
Using CLASSPATH:       /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar
21 Jan, 2013 12:34:17 AM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/var/lib/tomcat7/common/classes], exists: [false], isDirectory: [false], canRead: [false]
21 Jan, 2013 12:34:17 AM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/var/lib/tomcat7/common], exists: [false], isDirectory: [false], canRead: [false]
21 Jan, 2013 12:34:17 AM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/var/lib/tomcat7/server/classes], exists: [false], isDirectory: [false], canRead: [false]
21 Jan, 2013 12:34:17 AM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/var/lib/tomcat7/server], exists: [false], isDirectory: [false], canRead: [false]
21 Jan, 2013 12:34:17 AM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/var/lib/tomcat7/shared/classes], exists: [false], isDirectory: [false], canRead: [false]
21 Jan, 2013 12:34:17 AM org.apache.catalina.startup.ClassLoaderFactory validateFile
WARNING: Problem with directory [/var/lib/tomcat7/shared], exists: [false], isDirectory: [false], canRead: [false]
21 Jan, 2013 12:34:17 AM org.apache.catalina.startup.Catalina initDirs
SEVERE: Cannot find specified temporary folder at /usr/share/tomcat7/temp
21 Jan, 2013 12:34:17 AM org.apache.catalina.startup.Catalina load
WARNING: Unable to load server configuration from [/usr/share/tomcat7/conf/server.xml]
21 Jan, 2013 12:34:17 AM org.apache.catalina.startup.Catalina initDirs
SEVERE: Cannot find specified temporary folder at /usr/share/tomcat7/temp
21 Jan, 2013 12:34:17 AM org.apache.catalina.startup.Catalina load
WARNING: Unable to load server configuration from [/usr/share/tomcat7/conf/server.xml]
21 Jan, 2013 12:34:17 AM org.apache.catalina.startup.Catalina start
SEVERE: Cannot start server. Server instance is not configured.

Thanking in advance.
Edited from below.
Reinstalled Tomcat7.
This time the missing "server.xml" file is located in path: /usr/share/tomcat7/skel/conf/server.xml.
Again tried with:
~$ sh  /usr/share/tomcat7/bin/catalina.sh run

Logs are below.
Using CATALINA_BASE:   /usr/share/tomcat7
Using CATALINA_HOME:   /usr/share/tomcat7
Using CATALINA_TMPDIR: /usr/share/tomcat7/temp
Using JRE_HOME:        /usr/lib/jvm/java-6-openjdk/jre
Using CLASSPATH:       /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar
21 Jan, 2013 1:27:40 AM org.apache.catalina.startup.Catalina initDirs
SEVERE: Cannot find specified temporary folder at /usr/share/tomcat7/temp
21 Jan, 2013 1:27:40 AM org.apache.catalina.startup.Catalina load
WARNING: Unable to load server configuration from [/usr/share/tomcat7/conf/server.xml]
21 Jan, 2013 1:27:40 AM org.apache.catalina.startup.Catalina initDirs
SEVERE: Cannot find specified temporary folder at /usr/share/tomcat7/temp
21 Jan, 2013 1:27:40 AM org.apache.catalina.startup.Catalina load
WARNING: Unable to load server configuration from [/usr/share/tomcat7/conf/server.xml]
21 Jan, 2013 1:27:40 AM org.apache.catalina.startup.Catalina start
SEVERE: Cannot start server. Server instance is not configured.

Edited from below.
Forcefully tried to suppress the warnings
Now I have performed the following things:

*

*Get bash authentication pass:
~$ sudo bash

*Create a temp directory at /usr/share/tomcat7/skel
~# mkdir /usr/share/tomcat7/skel/temp

*Tell Tomcat7 where is my conf/server.xml located
~# export CATALINA_BASE=/usr/share/tomcat7/skel

*Run Catalina.sh
~# sh  /usr/share/tomcat7/bin/catalina.sh run

Logs:
Using CATALINA_BASE:   /usr/share/tomcat7/skel
Using CATALINA_HOME:   /usr/share/tomcat7
Using CATALINA_TMPDIR: /usr/share/tomcat7/skel/temp
Using JRE_HOME:        /usr/lib/jvm/java-6-openjdk/jre
Using CLASSPATH:       /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar
21 Jan, 2013 4:04:57 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
21 Jan, 2013 4:04:57 AM org.apache.coyote.AbstractProtocol init
SEVERE: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"]
java.net.BindException: Address already in use <null>:8080
    at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:391)
    at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:554)
    at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:409)
    at org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:119)
    at org.apache.catalina.connector.Connector.initInternal(Connector.java:956)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.core.StandardService.initInternal(StandardService.java:559)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:815)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:594)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:619)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:281)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:449)
Caused by: java.net.BindException: Address already in use
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:353)
    at java.net.ServerSocket.bind(ServerSocket.java:336)
    at java.net.ServerSocket.<init>(ServerSocket.java:202)
    at java.net.ServerSocket.<init>(ServerSocket.java:158)
    at org.apache.tomcat.util.net.DefaultServerSocketFactory.createSocket(DefaultServerSocketFactory.java:49)
    at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:378)
    ... 17 more
21 Jan, 2013 4:04:57 AM org.apache.catalina.core.StandardService initInternal
SEVERE: Failed to initialize connector [Connector[HTTP/1.1-8080]]
org.apache.catalina.LifecycleException: Failed to initialize component [Connector[HTTP/1.1-8080]]
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:106)
    at org.apache.catalina.core.StandardService.initInternal(StandardService.java:559)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:815)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:594)
    at org.apache.catalina.startup.Catalina.load(Catalina.java:619)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:281)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:449)
Caused by: org.apache.catalina.LifecycleException: Protocol handler initialization failed
    at org.apache.catalina.connector.Connector.initInternal(Connector.java:958)
    at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
    ... 12 more
Caused by: java.net.BindException: Address already in use <null>:8080
    at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:391)
    at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:554)
    at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:409)
    at org.apache.coyote.http11.AbstractHttp11JsseProtocol.init(AbstractHttp11JsseProtocol.java:119)
    at org.apache.catalina.connector.Connector.initInternal(Connector.java:956)
    ... 13 more
Caused by: java.net.BindException: Address already in use
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:353)
    at java.net.ServerSocket.bind(ServerSocket.java:336)
    at java.net.ServerSocket.<init>(ServerSocket.java:202)
    at java.net.ServerSocket.<init>(ServerSocket.java:158)
    at org.apache.tomcat.util.net.DefaultServerSocketFactory.createSocket(DefaultServerSocketFactory.java:49)
    at org.apache.tomcat.util.net.JIoEndpoint.bind(JIoEndpoint.java:378)
    ... 17 more
21 Jan, 2013 4:04:57 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 811 ms
21 Jan, 2013 4:04:57 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
21 Jan, 2013 4:04:57 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.26
21 Jan, 2013 4:04:57 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 37 ms
21 Jan, 2013 4:04:57 AM org.apache.catalina.core.StandardServer await
SEVERE: StandardServer.await: create[localhost:8005]:
java.net.BindException: Address already in use
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:353)
    at java.net.ServerSocket.bind(ServerSocket.java:336)
    at java.net.ServerSocket.<init>(ServerSocket.java:202)
    at org.apache.catalina.core.StandardServer.await(StandardServer.java:427)
    at org.apache.catalina.startup.Catalina.await(Catalina.java:727)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:673)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:450)
21 Jan, 2013 4:04:57 AM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-bio-8080"]
21 Jan, 2013 4:04:57 AM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
21 Jan, 2013 4:04:57 AM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["http-bio-8080"]
21 Jan, 2013 4:04:57 AM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["http-bio-8080"]

Although there are SEVERE message logs present, but when I tested http://localhost:8080 in my browser, I see It works!
Need experts advice: should I proceed with my work on Tomcat7 or there are things required to sort out.

A: The Exception is states in the error message,
SEVERE: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"]
java.net.BindException: Address already in use <null>:8080

You have another server instance any other process running on same machine so that you will not allowing you use the same port so that you need to first stop that running instance or change current tomcat port number.
you can stop the running instance as,
ps ax | grep java  //lists all java processes

and then kill the process by,
sudo kill -9 pid  //pid-process id

A: I have installed tomcat7 on Linux Mint 17 Qiana via apt-get and resolved some kind of the same problem using:
cd /usr/share/tomcat7
ln -s /etc/tomcat7 conf
chmod -R 655 /etc/tomcat7/

more info

A: Problem solved by reinstallion from a fresh download(downloaded the *tar.gz compressed version).

*

*Courtesy harpun( https://stackoverflow.com/users/283084/harpun)

A: The following exception already states:
Caused by: java.net.BindException: Address already in use <null>:8080

You have another server started on your local machine that doesn't allow Tomcat 7 to startup.
The "It Works" message doesn't pertain to Tomcat7, instead, one should see a Tomcat manager (with the Tomcat logo) appearing.
Stop any HTTP server running locally and restart Tomcat7.

A: As Buhake Sindi pointed out, the "It works!" message is not Tomcat. For future reference, that particular message is from Apache httpd.
Try run service apache2 stop before you try start Tomcat7. I think that's the most likely culprit for the port 8080 lock.

A: Some of these problems such as "missing "server.xml" and so on can be solved with symbolic links
cd /usr/share/tomcat7
ln -s /etc/tomcat7 conf

That was working for me after fixing similar things, so what I mean is to take care with these links.
I hope it will be useful
My config was Linux Mint 16, tomcat7 via apt-get


text:
Q: How quick must an Object be in is y component to reach a specific peak I want to add a boost to the player (bottom left) that reaches a specific peak(Point (x,y)). The players horizontal speed (v_x) is constant but its vertical speed can vary. I want to calculate the needed end speed v_y that it needs to reach the point (x,y) (this has to be the peak) which is also given. I thought maybe I can get the angle between Vector player and Vector point and calculate with the kinematic formulars but it didnt give me the right result.

A: Hoping I understand your problem correctly.  First you want to calculate how much time you have to climb to that peak.
// distance left to travel horizontally
delta.x = peak.x - player.x

// time left to reach peak
t = delta.x / speed.x

Let's say delta.x is 200px and the player's speed (horizontal) is 40px per second. This gives us a time of 5 seconds to climb from player.y to peak.y.
Now we calculate how far we need to move vertically.
// We have 5 seconds to move vertically by this much
delta.y = peak.y - player.y

// How fast to move per second to reach peak in 5 seconds.
speed.y = delta.y / t

A note about gravity:
If gravity is 3m/s (simplified for easier math), then the force of gravity  will be greater at each second it continues to be applied (because it's accelerating).
second - m/s
1 - 3
2 - 6
3 - 9
4 - 12
5 - 15

This doesn't mean after 5 seconds gravity has pulled your object 15 meters. That's the force at each second in the sequence, so you actually add them all up for the total, which would be 45. So you'd add 45 to the total distance calculated from above.

A: Jump? How high?
Gravity is constant, but the distance to the obstacle is changing as we approach.
At some point there is a distance to the obstacle where the jump velocity is the minimum needed to clear it.
Further back will require a greater velocity to stay in the air long enough to clear it, or closer the velocity must be greater to clear in the reaming time.
It is unclear if you want the optimal jump velocity to clear an object, which will also define when to jump.
So we will just make it Given gravity as acceleration and a horizontal velocity what vertical velocity is required to jump an obstacle of height and distance.
Getting jump velocity
To keep it simple as we go we first normalize the horizontal velocity
Terms used

*

*v is jump velocity, or initial velocity  (positive is up)

*t is time

*a is acceleration due to gravity (negative is down)

*h is obstacle height. Positive is up

*d is distance to obstacle. Equivalent to normalized horizontal velocity

*s is horizontal velocity. Assumes positive towards obstacle.

Steps used to solve
The equation for velocity at time when falling (free fall) is v + at
With the equation f'(t) = v + at the anti-derivative of that equation which is f(t) = vt + (1/2)at^2 tells use how high we are at any time t
Because we have normalized horizontal velocity s the distance to the obstacle is equivalent to time to obstacle t or distance to obstacle d
We can also use the anti-derivative to solve a height for a given distance h = vd+(1/2)ad^2  (replacing t with d)
Now we have all the terms we want in the one equation. h = vd+(1/2)ad^2
We want the jump velocity so rearrange h = vd+(1/2)ad^2 in terms of v we get v = -ad/2+h/d
One last thing. The horizontal velocity is normalized and thus always 1. We need to use any velocity. We can do this by dividing the distance to obstacle d by the horizontal velocity (speed) s. In other words how long in seconds to cover distance d at speed s
The final result is v = -a(d/s)/2+h/(d/s)
As code
Lets put that into code (JavaScript)
The function jumpObstacle(jumper, obstacle) will set the jump velocity of the object jumper to clear the object obstacle only if the jumper is moving towards the obstacle.
The Jumper has a jumpClear to give a clearance and a max jump velocity maxJumpVel.  This means that the jump may not clear the obstacle.
The function returns the jump height or 0 if no jump.
// Assuming +y is up
const gravity = 9.0;
const jumper = {
    vx: 1,
    vy: 0,
    x: 0,
    y: 0,
    maxJumpVel: 3,  // max jump velocity y
    jumpClear: 0.5, // clearance when jumping. May not if max jump to high
};
const wall = {
    x: 4,
    y: 0,   // base
    h: 4,  // height
};
function jumpObstacle(jumper, obstacle) {
    const dist = obstacle.x - jumper.x;
    if (jumper.vx && Math.sign(jumper.vx) === Math.sign(dist)) {
        const nd = dist / jumper.vx;                    // normalize dist (time)
        const h = (obstacle.h + obstacle.y + jumper.jumpClear) - jumper.y; // height to clear
        const vy = -(gravity * nd) / 2 + h / nd;
        jumper.vy = Math.min(vy, jumper.maxJumpVel);
        return jumper.y + vy * nd + gravity * nd * nd * 0.5;
    }
    return 0;
}

Demo
I am not sure if the answer is as clear it should be. The demo implements the above as a interactive demo.
Click or Tap the animation when box is in range of wall. The jumpObstacle function will calculate the jump velocity needed to clear the wall.
I have also added the width of the box, and the x jump clearance to ensure that the jump visually clears the wall.
If the jumper can not clear the wall it will flash red. Try again next time around.
The red box under the ground indicates the approx distance that the jumper is able to clear the wall.
Note that the coordinates are Y+ is down
Note The lowest jump speed I manged to was 271px per second

// Note y is + down the page
requestAnimationFrame(update);
const ctx = canvas.getContext("2d");
ctx.fillStyle = "red";
const h = 120, w = 200;
const ground = h - 10;
canvas.addEventListener("click", () => {
    jumper.onGround && (jumper.jumpNextFrame = true);
});

const gravity = 900.0;
const jumper = {
    vx: 60,
    vy: 0,
    x: 0,
    y: ground,
    h: 24,
    w: 8,
    jumpTime: 0,
    jumpNextFrame: false,
    get onGround() { return this.y >= ground },
    maxJumpVel: -490,
    jumpClear: -5,
    jumpClearX: 5,
    jumpFail: 0,
    draw(ctx) {
        this.jumpFail-- > 0 && ctx.fillRect(this.x - this.w / 2, this.y - this.h, this.w, this.h);
        ctx.strokeRect(this.x - this.w / 2, this.y - this.h, this.w, this.h);
    },
    jump(time, what) {
        this.jumpNextFrame = false;
        this.jumpTime = time;
        const h = jumpObstacle(this, what);
        if (h < 0 || h > what.h) {
            this.jumpTime = 0;
            this.jumpFail = 10;
            this.vy = 0;
        } else {
            console.clear();
            console.log("Jump vel: " + (-this.vy).toFixed(0) + "px per second");
        }
    },
    update(time) {
        this.x = this.vx * time;
        if (this.x > w ) { this.x = this.x % w }
        if (this.jumpTime > 0) {
            const t = time - this.jumpTime;
            this.y = ground + this.vy * t + gravity * t * t * 0.5;
            if (this.y > ground) {
                this.vy = 0;
                this.jumpTime = 0;
                this.y = ground;
            }
        }
    }
};
const wall = {
    x: 140,
    y: ground,
    h: 34,
    draw(ctx) { ctx.strokeRect(this.x - 1, this.y - this.h, 2, this.h) },
};
function jumpObstacle(jumper, obstacle) {
    const dist = (obstacle.x - (jumper.x - jumper.w)) + jumper.jumpClearX;
    if (jumper.vx && Math.sign(jumper.vx) === Math.sign(dist)) {
        const nd = (dist / jumper.vx) ;
        const h = -((obstacle.h + obstacle.y + jumper.jumpClear) - jumper.y);
        const vy = -(gravity * nd) / 2 + h / nd;
        jumper.vy = vy < jumper.maxJumpVel ? jumper.maxJumpVel : vy;
        return (jumper.vy * nd + gravity * nd * nd * 0.5) - h;
    }
    return 0;
}

function update(time) {
    time /= 1000;
    ctx.clearRect(0, 0, w, h);
    ctx.strokeRect(-1, ground, w + 2, h - ground + 2); // draw ground
    ctx.fillRect(wall.x - 50, ground + 2, 30, 2);
    if (jumper.jumpNextFrame) { jumper.jump(time , wall) }
    jumper.update(time);
    jumper.draw(ctx);
    wall.draw(ctx);
    requestAnimationFrame(update);
}
  console.log("Click animation to jump. Will flash red is not able to clear wall");
canvas { border: 1px solid black; }
<canvas id="canvas" width ="200" height="120"></canvas>


text:
Q: Laravel Format AllDirectories() output into tree To start off with, I have checked all resources I could for examples but was not able to find one that brought me close enough so i can resolve this query (simple as it may seem).
I've also seen there is a question that is the same but never resolved here: Get allDirectories() in Laravel and create a tree
I'll also just use the same sample data cause it's the exact same scenario.
I basically get an output from laravel's AllDirectories() function which output's something like this:
array:20 [▼
  0 => "test"
  1 => "files"
  2 => "files/2"
  3 => "files/2/Blocks"
  4 => "files/2/Blocks/thumbs"
  5 => "files/shares"
]

And I want to convert that into a multidimensional array that looks something like this:
[
            ["label" => "test", "path" => "test", "children" => []],
            ["label" => "files", "path" => "files", "children" =>
                [
                    ["label" => "2", "path" => "files/2", "children" =>
                        [
                            ["label" => "Blocks", "path" => "files/2/Blocks", "children" =>
                                [
                                    [
                                        "label" => "thumbs", "path" => "files/2/Blocks/thumbs", "children" => []
                                    ]
                                ]
                            ]
                        ]
                    ],
                    ["label" => "shares", "path" => "files/shares", "children" => []]
                ]
            ],
        ];

How can one go about converting the output from AllDirectories() to a multidimensional array?
Thanks in advance for any tips or tricks :)

A: You could exploit laravel collections and a bit of recursion to achieve what you need.
I wrote a function which works on a preprocessed output (array instead of plain string) and does the following steps:

*

*Take all given paths and create groups based on the first segment of each path.

*For each created group, take its children paths and remove the first segment from each children (filter out empty paths).

*Execute convertPathsToTree function on the children paths, and assign its output result to the children key of the resulting tree structure.
Here is the code:
function convertPathsToTree($paths, $separator = '/', $parent = null)
{
    return $paths
        ->groupBy(function ($parts) {
            return $parts[0];
        })->map(function ($parts, $key) use ($separator, $parent) {
            $childrenPaths = $parts->map(function ($parts) {
                return array_slice($parts, 1);
            })->filter();

            return [
                'label' => (string) $key,
                'path' => $parent . $key,
                'children' => $this->convertPathsToTree(
                    $childrenPaths,
                    $separator,
                    $parent . $key . $separator
                ),
            ];
        })->values();
}

Usage
First of all, let's assume the paths are assigned as a collection to a $data variable:
$data = collect([
    'test',
    'files',
    'files/2',
    'files/2/Blocks',
    'files/2/Blocks/thumbs',
    'files/shares',
]);

You first need to pre-process the array by splitting each path with the directory separator (/ in this example). This can be done with a simple map call:
$processedData = $data->map(function ($item) {
    return explode('/', $item);
});

Then, you can use the above function and provide the transformed input to obtain the requested structure:
convertPathsToTree($processedData);

If you would rather obtain an output array instead of an collection, add ->toArray(); after the ->values() call at the end of the function.

A: STEPS

*

*Convert the paths into arrays.

*Find the maximum path depth.

*Group paths based on their level of depth.

*Merge groupings in a hierarchical format.

*Reset the result's array indices/keys.

*Print output.

$rawPaths = [
    0 => "test",
    1 => "files",
    2 => "files/2",
    3 => "files/2/Blocks",
    4 => "files/2/Blocks/thumbs",
    5 => "files/karma",
    6 => "files/karma/foo",
    7 => "files/karma/foo/bar",
    8 => "files/shares",
];

// 1. Convert the paths into arrays.
$paths = array_map(function ($path) {
    return explode("/", $path);
}, $rawPaths);

// 2. Find the maximum path depth.
$maxDepth = 0;

for ($i = 0; $i < count($rawPaths); $i++) {
    if (($count = substr_count($rawPaths[$i], "/")) > $maxDepth) {
        $maxDepth = $count;
    }
}

// 3. Group paths based on their level of depth.
$groupings = [];

for ($j = 0; $j <= $maxDepth; $j++) {

    $groupings[] = array_filter($paths, function ($p) use ($j) {
        return count($p) === ($j + 1);
    });

}

// 4. Merge groupings in a hierarchical format.
$result = [];

for ($depth = 0; $depth <= $maxDepth; $depth++) {

    array_map(function ($grouping) use (&$result, $depth) {
        setNode($result, $grouping, $depth);
    }, $groupings[$depth]);

}

function setTree(&$grouping, &$depth): array
{
    $pathBuilder = $grouping[$depth];

    for ($i = 0; $i < $depth; $i++) {
        $pathBuilder = $grouping[$depth - ($i + 1)] . "/" . $pathBuilder;
    }

    return [
        "label" => $grouping[$depth],
        "path" => $pathBuilder,
        "children" => []
    ];

}

function setNode(&$result, $grouping, $depth)
{
    $node = &$result[$grouping[0]];

    if ($depth) {

        for ($i = ($depth - 1); $i >= 0; $i--) {
            $node = &$node["children"][$grouping[$depth - $i]];
        }

    }

    $node = setTree($grouping, $depth);
}

// 5. Reset the result's array indices/keys.
$arrayIterator = new \RecursiveArrayIterator(array_values($result));
$recursiveIterator = new \RecursiveIteratorIterator($arrayIterator, \RecursiveIteratorIterator::SELF_FIRST);

foreach ($recursiveIterator as $key => $value) {

    if (is_array($value) && ($key === "children")) {

        $value = array_values($value);

        // Get the current depth and traverse back up the tree, saving the modifications.
        $currentDepth = $recursiveIterator->getDepth();

        for ($subDepth = $currentDepth; $subDepth >= 0; $subDepth--) {
            // Get the current level iterator.
            $subIterator = $recursiveIterator->getSubIterator($subDepth);

            // If we are on the level we want to change, use the replacements ($value), otherwise set the key to the parent iterators value.
            $subIterator->offsetSet($subIterator->key(), ($subDepth === $currentDepth ? $value : $recursiveIterator->getSubIterator(($subDepth + 1))->getArrayCopy()));

        }
    }
}

// 6. Print output.
var_export($recursiveIterator->getArrayCopy());

// Output

array (
  0 =>
  array (
    'label' => 'test',
    'path' => 'test',
    'children' =>
    array (
    ),
  ),
  1 =>
  array (
    'label' => 'files',
    'path' => 'files',
    'children' =>
    array (
      0 =>
      array (
        'label' => '2',
        'path' => 'files/2',
        'children' =>
        array (
          0 =>
          array (
            'label' => 'Blocks',
            'path' => 'files/2/Blocks',
            'children' =>
            array (
              0 =>
              array (
                'label' => 'thumbs',
                'path' => 'files/2/Blocks/thumbs',
                'children' =>
                array (
                ),
              ),
            ),
          ),
        ),
      ),
      1 =>
      array (
        'label' => 'karma',
        'path' => 'files/karma',
        'children' =>
        array (
          0 =>
          array (
            'label' => 'foo',
            'path' => 'files/karma/foo',
            'children' =>
            array (
              0 =>
              array (
                'label' => 'bar',
                'path' => 'files/karma/foo/bar',
                'children' =>
                array (
                ),
              ),
            ),
          ),
        ),
      ),
      2 =>
      array (
        'label' => 'shares',
        'path' => 'files/shares',
        'children' =>
        array (
        ),
      ),
    ),
  ),
)


text:
Q: How to compute optical flow using tvl1 opencv function I'm trying to find python example for computing optical flow with tvl1 opencv function createOptFlow_DualTVL1 but it seems that there isn't enough documentation for it.
Could anyone please let me do that? I've used calcOpticalFlowFarneback mentioned here http://docs.opencv.org/master/d7/d8b/tutorial_py_lucas_kanade.html but it is not giving me accurate results, will tvl1 good enough and if not is there another method I should look for?
[[EDIT]]
I've some regions come from selective search, I want keep only regions with motion in it, so computing the OF for a given frame and then get the avg in each region could do it. It's also described in this paper section 3.1
Thanks.

A: Change this line(Dense Optical example in http://docs.opencv.org/master/d7/d8b/tutorial_py_lucas_kanade.html):
flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0)

By these:
optical_flow = cv2.DualTVL1OpticalFlow_create()
flow = optical_flow.calc(prvs, next, None)

The parameter descriptions can be found here:
http://docs.opencv.org/3.3.0/dc/d47/classcv_1_1DualTVL1OpticalFlow.html


text:
Q: merge two lists with WCF Services and Linq How do I merge two lists when using REST services?  You can't use union with linq and dataservicequery so I'm not sure where to go.
The following doesn't work with dataservicequery...
var docQuery = (from docs in context.Documents.AsQueryable()
                        select new DocsColumns()
                        {
                            Title = docs.Title
                            //     Element = docs.Elements.Title
                        })
                        .Union(from rules in context.Rules.AsQueryable()
                               select new DocsColumns()
                               {
                                   Title = rules.Title
                                   //    Element = rules.Elements.Title
                               });

                               as DataServiceQuery<DocsColumns>;

This link doesn't help much either....
https://stackoverflow.com/questions/720609/merge-two-object-lists-with-linq
Any ideas?  Really struggling on this one.

A: Using Linq, You can merge using below pseudo code :
using (DataContext context = new DataContext("<<Site Url>>"))
{

    EntityList<Item> allDocuments = context.GetList<Item>("Documents");
    EntityList<Item> allRules = context.GetList<Item>("Rules");

    List<Item> allDocumentItems = <<Select relevant docs from allDocuments>>;
    List<Item> allRuleItems = <<Select relevant rules from allRules>>;

    IEnumerable<Item> mergedItems = allDocumentItems.Union(allRuleItems);
}

Note that Your source code must provide a declaration of an Item class, and that class must declare properties for each of the columns in the content type that your queries reference. For e.g.:
[ContentType(Name="Item", Id="0x01")]
public partial class Item
{
    [Column(Name = "Title", FieldType = "Text")]
    public String Title { get; set; }
}


text:
Q: Creating a Nested Dictionary from For Loop I'm trying to create a new dictionary from existing data. I'm not sure the best way to explain so here is an example:
I have the following data:
list_1 = ['aa', 'bb', 'cc']

dict_1 = {'key_1': {1: 'data1', 2: 'aa'},
          'key_2': {1: 'data2', 2: 'aa'},
          'key_3': {1: 'data3', 2: 'cc'},
          'key_4': {1: 'data4', 2: 'bb'},
          'key_5': {1: 'data5', 2: 'bb'},
          'key_6': {1: 'data6', 2: 'cc'}}

I want to loop through list_1 and dict_1 and if the item in list_1 matches the second key value pair in dict_1 it adds it to the new dict.
So the output would be something like:
{'aa': {{1: 'data1', 2: 'aa'},
        {1: 'data2', 2: 'aa'}},
'bb': {{1: 'data4', 2: 'bb'},
       {1: 'data5', 2: 'bb'}},
'cc': {{1: 'data3', 2: 'cc'},
       {1: 'data6', 2: 'cc'}}}

Is there a simple way to do this?

A: Use collections.defaultdict for this.
Note your desired output includes sets of dictionaries. I assume you want lists of dictionaries.
It also does not appear you need to use list_1.
from collections import defaultdict

dict_1 = {'key_1': {1: 'data1', 2: 'aa'},
          'key_2': {1: 'data2', 2: 'aa'},
          'key_3': {1: 'data3', 2: 'cc'},
          'key_4': {1: 'data4', 2: 'bb'},
          'key_5': {1: 'data5', 2: 'bb'},
          'key_6': {1: 'data6', 2: 'cc'}}

d = defaultdict(list)

for k, v in dict_1.items():
    d[v[2]].append(v)

Result:
defaultdict(list,
            {'aa': [{1: 'data1', 2: 'aa'}, {1: 'data2', 2: 'aa'}],
             'bb': [{1: 'data4', 2: 'bb'}, {1: 'data5', 2: 'bb'}],
             'cc': [{1: 'data3', 2: 'cc'}, {1: 'data6', 2: 'cc'}]})


text:
Q: plm cross-validation runtime-error I am trying to cross-validate a panel data analysis, but I am having trouble with the prediction function. The code below is a section of the CV function where the problem occurs. When I try to run this part of the function I get the error message tagged on to the end of the code description.
compare <- data.frame()
train.model <- list()

  for (f in 1:numoffolds)
{
train.model[[f]] <- plm(logit(II1)~.,data=select(filter(train, cv != f),-Incomegroup, -cv),
                       model="within", effect="twoways", index=c("Year", "Country") )

 II1.p <- predict(train.model[[f]], newdata=select(filter(train, cv == f),-Country, -Year, -cv, -Incomegroup), type="response")
II1 <- filter(train, cv == f)$II1
compare <- rbind(compare, data.frame(II1.p = II1.p, II1 = II1))

}

Error in crossprod(beta, t(X)) : non-conformable arguments
Called from: crossprod(beta, t(X))

I've had a look at the prediction function and the X and beta terms are of conflicting dimensions. Does anyone have a suggesting to what I can do to overcome this problem?
function (object, newdata = NULL, ...)
{
tt <- terms(object)
if (is.null(newdata)) {
result <- fitted(object, ...)
}
else {
Terms <- delete.response(tt)
m <- model.frame(Terms, newdata)
X <- model.matrix(Terms, m)
beta <- coef(object)
result <- as.numeric(crossprod(beta, t(X)))
}
result

I've been thinking about resorting to other methods such as tree based regression, but I wanted to use plm to make a specific point. Any helping comments and/or codes would be greatly appreciated.


text:
Q: How to add nan values in specific index positions in numpy arrays? I have 2 np.arrays:
The first one called data:
data= array([ 17. ,  nan,   8.1,  25.1,  nan,   6.9,  nan,  27.1,  46.6,
        34.1,  25.7,  nan,  ... , 25.3 ])

Array of float 64 Size (366,)

To get the second one i did an interpolation. So i should first drop the NaN values:
data = data[~numpy.isnan(data)]

So i have now the data like this:
data = array([ 17. ,   8.1,  25.1,   6.9,  27.1,  46.6,
        34.1,  25.7,  ... , 25.3 ])

Array of float 64 Size (283,)

And after the interpolation i get the second one:
interpolated_data = array([ 16 ,   7.1,  24.1,   7.9,  26.1,  45.6,
            33.1,  27.7,  ... , 24.3 ])

Array of float 64 Size (283,)

Now i want to give it back the nan values in the same index position in both arrays.
Expected values:
data = array([ 17. ,  nan,   8.1,  25.1,  nan,   6.9,  nan,  27.1,  46.6,
            34.1,  25.7,  nan,  ... , 25.3 ])

Array of float 64 Size (366,)

interpolated_data = array([ 16 ,  nan,   7.1,  24.1,  nan,   7.9,  nan,  26.1,  45.6,
            33.1,  27.7,  nan,  ... , 24.3 ])

Array of float 64 Size (366,)

Would you mind to help me? Thanks in advance.

A: First your extract the values from your data array with the mask you created:
data= array([ 17. ,  nan,   8.1,  25.1,  nan,   6.9,  nan,  27.1,  46.6,
    34.1,  25.7,  nan,  ... , 25.3 ])

nan_mask = numpy.isnan(data)
data1 = data[~nan_mask]

From there you get your interpolated_data. Then, you can create an empty array of the same size of the initial data array and then put back your interpolated_data and the np.nan in this empty array:
interpolated_array = np.empty(data.shape)
interpolated_array[~nan_mask] = interpolated_data
interpolated_array[nan_mask] = np.nan

A: Keep index where there is no nan, do computation and recreate an array with the same dimension as the initial filled by nan. Use your index to copy your value to the new array.
# initial array
a = array([ 1.,  2., nan,  4., nan,  6.])

# index where no nan
idx = np.where(~np.isnan(a))

# new array without nan
m = a[idx]
print(m)
array([1., 2., 4., 6.])

# ... interpolation ...
print(i)
array([10, 20, 40, 60])

# replace nan
b = np.array([np.nan] * len(a))
new[idx] = i
print(b)
array([10., 20., nan, 40., nan, 60.])

A: Here you go:
# generate data with nan values
data = np.ones(10)
data[4] = np.nan

# get boolean selection where data is nan
boolean_selection = np.isnan(data)

# apply some interpolation on the data that is not nan
# this is just a placeholder
interpolated_data = data[np.logical_not(boolean_selection)]

# fill back the interpolated data
data[np.logical_not(boolean_selection)] = interpolated_data


text:
Q: Incorrect parameter type for operator '>=' My start/end date was Date and I have changed it Date/Time datatype after doing that my formula is broken:
IF(ISPICKVAL(Employee_Status__c, "ACTIVE") ,
       TODAY() >= Emp_Start_Date__c && TODAY() <= Emp_End_Date__c , FALSE)

Incorrect parameter type for operator '>='. Expected Date, received
  DateTime (Related field: Formula)

A: You need to cast the DateTime value into a Date for use with other Date values. Wrap your Emp_Start_Date__c field in a DATEVALUE function to cast it, or use NOW() as a comparison operator, as it returns a DateTime, which you can use without casts.
From the Working with Date and Date/Time documentation:

Date and Date/Time aren't interchangeable data types, so when you want to perform operations between Date and Date/Time values, you need to convert the values so they are both the same type. Some functions (such as YEAR(), MONTH(), and DAY()) also only work on Date values, so Date/Time values must be converted first.
Use the DATEVALUE( date/time ) function to return the Date value of a Date/Time. For example, to get the year from a Date/Time, use YEAR( DATEVALUE( date/time ) ) ).


text:
Q: syslog-ng syslog connection broken on udp We have following remote logging configuration:
destination d_jenkins { network("x.x.x.x" transport("udp") port(514) template(t_jenkins)); };
log { source(s_system); destination(d_jenkins); };

This is a syslog-ng server to syslog-ng client communication. Occasionally we are getting following errors. Seems like EINVAL (22) is coming from write() call.
2016-10-19T15:12:39.393+00:00 axis-accc8e59xxxx [ ERR     ] syslog-ng[1707]: I/O error occurred while writing; fd='28', error='Invalid argument (22)'
2016-10-19T15:12:39.394+00:00 axis-accc8e59xxxx [ NOTICE  ] syslog-ng[1707]: Syslog connection broken; fd='28', server='AF_INET(172.27.0.34:514)', time_reopen='60'

*

*Is it that network is problematic or am I missing some configuration?

*Could it be that syslog-ng server is being HUPed without our
knowledge, dropping the connections.

*Could using TCP with keep-alive help the problem?


text:
Q: Parsing out body and tables from emails I currently am getting the body/content of the emails in Python using the following:
import email
message = email.message_from_file(open(file))
messages = [part.get_payload() for part in message.walk() if part.get_content_type() == 'text/plain']

This seems to work well in most cases, but I noticed that sometimes there are html tables that don't get picked up. It starts with
<html>
<style type='text/css">

Would it just be to add or part.get_content_tye() == 'text/css'?

A: If I had to guess, I would guess that you need to add 'text/html'.
However, you should be able to figure out what content-type is in the email by examining the content of that variable.
import email
message = email.message_from_file(open(file))

# Remove the content-type filter completely
messages = [(part.get_payload(), part.get_content_type()) for part in message.walk()]

# print the whole thing out so that you can see what content-types are in there.
print(message)

This will help you see what content types are in there and you can then filter the ones that you need.


text:
Q: Como encriptar mi cadena de conexion en proyecto .Net Core 2.2 Hola tengo un proyecto Web API Core 2.2 y en el appsettings.Development tengo varios conectionstring como puedo encriptar para que la clave no se vea en texto plano.
Aqui mi appsettings.Development
{
  "Logging": {
  "LogLevel": {
  "Default": "Debug",
  "System": "Information",
  "Microsoft": "Information"
  }
 },
 "ConnectionStrings": {
 "ClinicDatabase": "Data Source=(localdb)\\Dev;Initial Catalog=Clinic; Trusted_Connection=True;",
 "Damar": "Data Source=localhost;Initial Catalog=Test01;user id=sa;password=1234;MultipleActiveResultSets=true;",
 "Country": "Data Source=192.168.1.5;Initial Catalog=Test02;user id=sa;password=1234;MultipleActiveResultSets=true;"
 }
}

A: Comento un poco de como era antes y como puedes realizarlo ahora son secrets, quitarlo del server con Azure KeyVault o incluso seguir utilizando las variables de entornos. Si bien no es encriptar nuestras configuraciones es la forma que tenemos de resguardarlas en el server de produccion o inluso quitarlas de alli y transmitirlas/compartirlas de manera segura
¿Cómo era antes...? encriptar secciones de web.config
Para resguardar configuraciones en ASP.NET con web.config realizábamos la encriptación de secciones (o incluso los mas kamikazes utilizaban la jerarquía de archivos de configuración que venia con .NET Frameworks). Pero bueno lo recomendado en entornos PROD (Producción) era encriptar las secciones (Y ni hablar la configuración que había que hacer cuando teníamos un cluster de servidores web de producción donde había que compartir la misma key machine para que DPAPI pueda encriptar/desencriptar).
Aquí también teníamos las transformaciones de ese tipo de archivo que luego no lo queríamos ni bien que son los .xml (nos ayudaban e incluso hoy nos ayudan estas transformaciones para proyectos con esta tecnología)
Ahora... NET 5 / Core secrets & variables de entornos...
En nuestros nuevos proyectos NET5 (o anteriores NET Core) tenemos los archivos de configuración que son archivos json (y los merge de json son muy buenos y faciles de entender y manipular), nuestros queridos appSettings.*.json.
Es muy fácil utilizarlos. Y se lleva muy bien con el versionado de código. Pero allí es el primer problema de no tener dentro de nuestros repositorios de código, credenciales o "datos sensibles". Para eso en modo de desarrollo tenemos los "secrets", que básicamente es una extensión/modulo de NET que nos ayuda a "mantener separado" configuraciones sensibles. Que se integra a nuestro IDE (o también se lo puede generar/configurar/administrar por linea de comandos)

*

*Mantener seguro los secretos en ASP.NET Core
Alli en el enlace anterior puedes verlo y es recomendable que utilices esto en modo desarrollo. Lo habilitas con un simple click en VS2019 o por linea de comando. Y se guarda el archivo de configuracion en un ubicación "especial" para el usuario (los secretos por usuario)
¿Y en PROD?
Lo que comente mas arriba verás que también se puede realizar en PROD (o sea tener un secreto por el usuario que ejecuta la aplicación).
Un ejemplo general y sencillo es aplicaciones on-premise que se ejecutan en el IIS se ejecutan con el usuario del App Pool (todas los app pool en el IIS tienen un usuario que se genera en cada servidor como APP POOL{nombre del apppool}, o puedes modificar el usuario con que se ejecuta cambiando la propiedad AppPoolIdentity en el App Pool (asi es un usuario especifico de la aplicacion o que te asignaron el area de Operaciones/Infraestructura).Casi siempre lo tenia asi proyectos que recuerde (no utiice con el usuario que se genera el IIS sino uno creado en el Active Directory para la app, por los permisos que se requiere). Pero si este es tu ambiente avisanos y profundizamos.
Entonces... es igual que el entorno DEV pero en PROD

*

*Allí podrás definir variables de secret por linea de comandos (asi no estas con archivos json)
dotnet user-secrets set "ConnectionStrings:ClinicDatabase" "ejemplo de valor"

*O podrías ir al archivo especifico que en windows es %APPDATA%\Microsoft\UserSecrets<user_secrets_id>\secrets.json > Mas info Como funciona el administrador de secretos
En IIS hay un issue de donde lee que lo puedes ver aqui, pero es en

*C:\WINDOWS\system32\config\systemprofile\AppData\Roaming
y allí deberías colocar la carpeta del secret (el ID que genera con el archivo secret.json dentro) y darle el permiso que tu app lo pueda leer (el usuario del APP POOL). Si este es tu escenario avisanos y profundizamos
¿No esta encriptado? No. Pero puedes utilizar Azure Key-vault
Y como veras en entorno DEV, este archivo no esta encriptado (pero no esta dentro del repositorio, ni en los archivos de deploy de tu app/api/etc.) y esta resguarda en un lugar seguro solo accesible para administradores o personas con el permiso adecuado.
Pero si aun quieres encriptar, puedes probar utilizar Azure KeyVault, que es resguardar externamente TODO (y literalmente es todo, credenciales, certificados etc). Pero allí tendrías que tener una cuenta en Azure (es mínimo los precios) pero es una alternativa valida para sacar "de PROD" (e incluso de las maquinas de DEV) todos los datos sensibles.
Lo bueno, que se integra a variables de ENTORNOS (Enviroment Variables)
Esto que comente mas arriba de secrets y appSettings se integra a variables de entornos (eso que ya conocemos en IIS y entornos windows) o sea "SE INTEGRA", se puede utilizar tambien para sobreescribir. Agregando esto "AddEnvironmentVariables" al realizar el build de nuestra configuracion de la app
Ejemplo
var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();

Y alli podremos sobre escribir cualquier configuración
Encontre un post relacionado (del 2016) pero valido

*

*ASP.NET Core: No more worries about checking in secrets
y de ultima! Un proveedor de configuracion "personalizado"
Pero esta parte ya es muy interna, pero se podría construir uno que si encripte/descripte. Y alli tendriamos que evaluar la estrategia para armarlo. Solo lo comento que se puede extender (como todo en .NET). Por ejemplo:

*

*Create a Custom Configuration Provider in ASP.NET Core
Espero que te sirva de ayuda o guía.

A: lo primero que debes tener en cuenta es como encriptar y desencriptar teniendo en cuenta que hay encripción simétrica y asimétrica para el primer caso puedes recuperar la cadena original teniendo la clave de encripción para el segundo caso no es posible y también debes tener en cuenta como modificar  los parámetros del appsettings.
Si lo que necesitas es que no se vea en texto plano puedes usar codificación base 64 así para que no te compliques mucho.

// Asi podrias codificar facilmente usando string base 64
public static string Codificar(this string _cadena)
{
  string result = string.Empty;
  byte[] codificada = System.Text.Encoding.Unicode.GetBytes(_cadena);
  result = Convert.ToBase64String(codificada );
  return result;
}

// y asi lo decodificarías

public static string Decodificar(this string _cadena)
{
  string result = string.Empty;
  byte[] decodificada = Convert.FromBase64String(_cadena);
  //result = System.Text.Encoding.Unicode.GetString(decodificada , 0, decodificada .ToArray().Length);
  result = System.Text.Encoding.Unicode.GetString(decodificada );
  return result;
}

Si lo que buscas es mayor seguridad para tu cadena de conexión esto te puede servir:
https://elburgues.wordpress.com/2010/03/13/encriptacion-simetrica-en-c/
para lo segundo en esta url encontraras información de como modificar en tiempo de ejecución los valores del appsetings
introducir la descripción del enlace aquí


text:
Q: Accessing the index operator of an auto-property struct I've got a "Vector" struct which stores a x, y, and z values. In an effort to make them easier to access I added an index operator, like this:
public struct Vector
{
    public double x;
    public double y;
    public double z;

    public double this[int i]
    {
        get
        {
            if(i == 0) return x;
            else if(i == 1) return y;
            else if(i == 2) return z;
            else throw new ArgumentOutOfRangeException("i", i, "Index must be 0, 1, or 2");
        }
        set
        {
            if (i == 0) x = value;
            else if (i == 1) y = value;
            else if (i == 2) z = value;
            else throw new ArgumentOutOfRangeException("i", i, "Index must be 0, 1, or 2");
        }
    }
}

Note: Normally I would use an array instead of x, y, z but I'm working with pre-existing code and it'll be difficult to refactor at this point.
Now if I use a struct as an auto-property in a class and then try to modify its xyz values, I get this
public Vector v{ get; set; }
void funct(){
    for(int i = 0; i < 3; i++)
    {
        v[i] = 3.0;
    }
}

I get a compiler error:

"Cannot modify the return value of "v" because it is not a variable"

I've tried googling for the error, but I don't think anyone has done this the way I did (it's a pretty interesting case I guess, and probably really ugly). What would be the right way to solve this problem? Is it wrong to use the index operator like this?
Note: yes, I know this is a mutable struct. Again, pre-existing code

A: You got some options:

*

*Make it a class instead of a struct

*Obtain a copy of the struct value first, modify it, then put it back:
Vector temp = v;
for(int i = 0; i < 3; i++)
{
    temp[i] = 3.0;
}
v = temp;

*Make it immutable and add helper/factory methods:
public struct Vector
{
    ....

    public Vector WithX(double x)
    {
        return new Vector(x, this.y, this.z);
    }

I would personally go for nbr. 3 as mutable structs (as you've seen) is a pain in the a**.


text:
Q: Две среды разработки одном компьютере Можно ли на одном и том же компьютере пользоваться разными IDE, к примеру, wxDev-C++ и Visual C++? Не возникнут какие-то конфликтные ситуации?
A: Да, можно.
Но по умолчанию (через оболочку даблкликом или пунктом "открыть") все открываться будет в последней установленной.
Иначе - нужно править ассоциации файлов.
Это в случае, если оболочки для одного языка. Если для разных, то и проблем с ассоциациями файлов быть не должно.
Единственное, что не нужно мешать разные версии одной оболочки. Например, MS VS'98 + MS VS.NET уживаются, а вот VS .NET 2001 и VS .NET 2005 - уже нет :-(
A: Да, можно, да, возникнут - нельзя двум IDE сделать ассоциации с одним файлом, но можно добавить в меню мыши открытие второй. Я бы не рекомендовал связываться с wxDev-C++: уж лучше тогда Notepad++ и консольный компилятор к нему прикрутить. И зачем он, если есть бесплатная студия, которая сделана для людей, а не от нечего делать.


text:
Q: Views and Associations - Show or Index action I think I'm overcomplicating this..
I have a person model (id, first_name, last_name, city_id) and a city module (id, city_name).  I have an association where the person belongs_to the city and the city has_many people.  I built a _form.html.erb with simple_form and added a drop-down on the person side so the user can pick the city from a drop-down menu and stored the city ID in the city_id in the person model (int).  All of this works well.
My goal is to modify the show and index action where I show a manifest of all people such that the city name is show vs. the city_ID integer.
Right now it looks like this:
First    Last    City
Adam     Smith   1
I want it to look like this
First    Last    City
Adam     Smith   Chicago
Any ideas?  I know this is totally doable - I'm just out of ideas and can't seem to come up with an answer.
Thanks in advance.
Adam

A: Do as @emrahbasman suggested
<%= person.city.city_name %>

And, in your controller in the index method, do the following
@people = Person.includes(:city)

To avoid querying on each row when you render in the view


text:
Q: Modal Contact Form - Shopify I've built a modal contact form, but when user hit the submit button, the modal closes. I want the modal to stay open so users can see the success message.
I see there's this code in my CSS file... But if I take that out, then the modal will show on the page at all times.

.mfp-hide {
    display: none !important; }


text:
Q: How to save a file in Outlook from Add-in [taskpane or otherwise]? I have built an Office/Outlook add-in that interacts with my user's email messages.
I have the need/requirement to build/create and save a file to the desktop for my add-in user.
I have tried various methods with typical JavaScript solutions for the web. These solutions work only when the add-in is being used through OWA. These solutions also test successfully in Safari, FF, Chrome, etc ... but will not work in Outlook for Mac/Windows.
I have been searching [unsuccessfully,] the OfficeJS API documentation for a possible API method to save a file instead of writing my own in JavaScript.
Does such a thing exist?
Can an Outlook add-in create and save a file to the desktop for a user from "Outlook for Mac/Windows"?
Edit: While the documentation here (https://learn.microsoft.com/en-us/office/dev/add-ins/concepts/browsers-used-by-office-web-add-ins) suggests (for example) Outlook for Mac is running an embedded safari... it appears it is a much more locked down sandboxed version of Safari. Which leads me to the hypothesis, no amount of custom JavaScript will get me to my feature implementation.
Note: WRT to implementing my own JS to download a file (still unsuccessful, maybe a working hack exists here somewhere?)
// testing with a simple test image though will need to support more than images
const testBase64Image = '<insert_base64_string_of_a_jpg_image>';

downloadTempFile1(): void {
  const newFileName = 'test_download_image.jpg';
  const a = document.createElement("a"); // Create <a>
  a.target = "_blank"; // open in new tab
  // need special handling of certain content types?
  a.href = "data:octet-stream;base64," + testBase64Image; // Base64 Goes here
  a.download = newFileName; //File name Here
  a.click(); //Download file
}

downloadTempFile2(): void {
  window.location.href = 'data:application/octet-stream;base64,' + testBase64Image;
}

downloadTempFile3(): void {
  const blob = this.b64toBlob(testBase64Image, 'octet-stream');
  const blobUrl = URL.createObjectURL(blob);
  const link = document.createElement('a');
  link.download = 'test_download_image3.jpg';
  link.target = '_blank';
  link.href = URL.createObjectURL(blob);
  link.click();
}

b64toBlob = (b64Data, contentType='', sliceSize=512): Blob => {
  const byteCharacters = atob(b64Data);
  const byteArrays = [];
  for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    const slice = byteCharacters.slice(offset, offset + sliceSize);
    const byteNumbers = new Array(slice.length);
    for (let i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);
    }
    const byteArray = new Uint8Array(byteNumbers);
      byteArrays.push(byteArray);
  }
  const blob = new Blob(byteArrays, {type: contentType});
  return blob;
}

Edit2: There exists Office.context.ui.openBrowswerWindow for some Client versions apparently, but I was able to use window.open() in this fashion.
openNewWindow(): void {
  const wnd = window.open('data:application/octet-stream;base64,' + base64Image);
  setTimeout(function() {
    wnd.close();
  }, 3000);
}

This will download the file in OWA, but will still not download the file in the desktop client.
Edit3: I was able to figure out that I needed to lock down my manifest to Office SDK Mailbox 1.8 (Requirements Set).  I was also able to figure out that Outlook for Mac has a little toggle button in the top to switch from old to new UI and back. New UI does not support 1.8.   1.8 was required for window.open to operate correctly, but I have yet to be able to use window.open to download a file.   I am still unable to use openBrowserWindowApi 1.1 yet as well.
Edit4: I decided to try using Office.context.ui.displayDialogAsync() and sending a message to the child window. While the displayDialogAsync will open a new window from the desktop client, it will not let me save a base64 file to the desktop for the user.

A: openBrowserWindow is the workaround for this. (We cannot download files directly from the Sandboxed IE or Edge Process). This should open an html page in the default browser, that the user can then download files via a link/etc.
https://learn.microsoft.com/en-us/javascript/api/office/office.ui?view=excel-js-preview#openBrowserWindow_url_
window.open() functions differently depending on if IE or Edge is the underlying rendering engine which can vary based on Office and Windows Version, which is whey OpenBrowserWindow was created.
See this link for information about IE/Edge WebView: https://developer.microsoft.com/en-us/office/blogs/microsoft-edge-webview-for-office-add-ins/

A: It seems that Office.context.ui.openBrowserWindow() only works with urls beginning with http:// or https://. If the url looks like data:<mime-type;base64,<base64 encoded content> or blob:https://<url>/<uuid> (created by URL.createObjectURL()), it does not open a new window and it does not throw any error message.
For windows client the method should work. I tested FileSaver.js to do this job for me, which works perfectly fine in windows client, but not in mac client.
Are there any other possibility to download base64 content in "outlook for mac" without sending the data to a remote service?


text:
Q: There are no GPS data after connecting to the server I'm trying to get GPS data and send it to the server via socket.
I'm using Arduino Uno and GPS/GSM module A7 Ai-Thinker.
And for Internet I use Ethernet shield connected to Wi-Fi router.
But I got trouble when I joined getting GPS data and sending it.
Separately that functions work fine. How I start A7, there is a loop where I type AT commands and send it to A7. About socket, I want to set connection at start function and keep it alive all the time.
But the problem is that when I initialized A7 and set socket connection, I go to the loop function and after first iteration there are no data at A7.
If I try to set connection with server first, and start A7 after it, A7 doesn't react to AT commands.
How can I keep connection with my server and don't lost connection with A7?
Code:
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

#include <SPI.h>
#include <Ethernet.h>

//-------------------------------------------------------
static const int RXPin = 10, TXPin = 11;
static const uint32_t GPSBaud = 4800;
const String START_GPS = "START_GPS";

TinyGPSPlus gps;
SoftwareSerial gps_serial(RXPin, TXPin);

//-------------------------------------------------------
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "192.168.0.100";
int port = 8090;

IPAddress ip(192, 168, 0, 177);
EthernetClient client;
//-------------------------------------------------------

void initGpsConnection();
void startGps();

void initSocketConnection();
void sendData();

void setup()
{
  Serial.begin(9600);
  Serial.println();

  initSocketConnection();
  initGpsConnection();

  sendData();
}

void loop()
{
  while (gps_serial.available() > 0) {
    if (gps.encode(gps_serial.read()))  {
      displayInfo();
      //delay(1000);
    }
  }

  if (millis() > 120000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    //while(true);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("Socket connection has been lost.");
    client.stop();

    //while (true);
  }
}

void displayInfo()
{
  Serial.print(F("Location: "));
  if (gps.location.isValid())
  {
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F("  Date/Time: "));
  if (gps.date.isValid()) {
    Serial.print(gps.date.month() + "/" gps.date.day() + "/" + gps.date.year();
  } else {
    Serial.print(F("INVALID"));
  }

  Serial.print(F(" "));
  if (gps.time.isValid()) {
    Serial.print(gps.time.hour() + ":" + gps.time.minute() + ":" + gps.time.second());
  } else {
    Serial.print(F("INVALID"));
  }
  Serial.println();
}

void initGpsConnection() {
  Serial.println("In init gps");
  gps_serial.begin(GPSBaud);

  Serial.println("after gps begin");
  delay(1000);
  startGps();

  Serial.println("GPS connection has been set");
}

void startGps() {
  Serial.println("Enter at commands, after that enter `start_gps`");
  String response = "";

  while (true) {
    if (gps_serial.available()) {
      Serial.write(gps_serial.read());
    }
    if (Serial.available()) {
      char c = Serial.read();
      response += c;

      if (response.indexOf(START_GPS) > 0)
        break;

      gps_serial.write(c);
    }
  }
}

void initSocketConnection() {
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }

  delay(1000);
  Serial.println("connecting...");

  if (client.connect(server, port)) {
    Serial.println("Connection with server has been set");
  } else {
    Serial.println("Connection with server has been failed");
  }
}

void sendData(){
  client.print("TEst ard");
}

A: The problem was at A7 connection. It was connected via 10, 11 pins at Ethernet shield. But 10-13 pins are reserved by A7. It's just needed connect A7 vie 8, 9 or others pins.


text:
Q: SQL Server 2005 - Understanding ouput of DBCC SHOWCONTIG I'm seeing some slow performance on a SQL Server 2005 database. I've been doing some research regarding SQL Server performance but I'm having difficulty fully understanding the output of SHOWCONTIG and would be very grateful if someone could have a look and offer some suggestions to improve performance.
TABLE level scan performed.

*

*Pages Scanned................................: 19298

*Extents Scanned..............................: 2424

*Extent Switches..............................: 3829

*Avg. Pages per Extent........................: 8.0

*Scan Density [Best Count:Actual Count].......: 65.16% [2439:3830]

*Logical Scan Fragmentation ..................: 8.40%

*Extent Scan Fragmentation ...................: 35.15%

*Avg. Bytes Free per Page.....................: 912.1

*Avg. Page Density (full).....................: 88.41%

A: It gives information about how the table looks on disc - and is extremely useless do locate your performance bottleneck. Your db could need some optimization, but I doubt it is the source of your problems.
http://www.sql-server-performance.com/articles/dba/dt_dbcc_showcontig_p1.aspx
has info how to understand it.


text:
Q: Webpack load fonts I have this code in a React element:
require('../../Style/fonts/SomethingStrange.ttf')

In my Webpack.config:
{ test: /\.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/,
      loader: 'file-loader?name=fonts/[name].[ext]' }

Problem: no font on my webpages...
Doesn't Webpack let me automatically require fonts? Do I have to program anything else to load fonts?

A: You have to require your fonts inside css files, not inside js/jsx files. For example:
@font-face {
    font-family: 'SomethingStrange';
    src: url('../../Style/fonts/SomethingStrange.ttf');
}