Why is Rust's default sort function slightly slower than my Selection Sort for small arrays?

Question: I'm quite new to Rust, so I might be missing something simple. I am using Rust 1.70.0-nightly. Here is the necessary code:
fn selection_sort(original_vec: &mut Vec<i32>) -> Vec<i32> {
    for i in 0..original_vec.len()-1 {
        let mut smallest: usize = i;
        for j in i+1..original_vec.len() {
            if original_vec[j] < original_vec[smallest] {
                smallest = j;
            }
        }
        if smallest != i {
            original_vec.swap(i, smallest);
        }
    };
    original_vec.to_vec()
}

// helper function for testing (uses builtin sort function)
fn rust_sort(mut array: A) -> A
where
    A: AsMut<[T]>,
    T: Ord,
{
    let slice = array.as_mut();
    slice.sort();

    array
}

const TEST_VECS: [[i32; 10]; 6] = [
    [1, 3, 2, 9, 6, 7, 4, 10, 8, 5],
    [1, 2, 7, 2, 9, 9, 7, 10, 2, 1],
    [0, 4, 1, 3, 9, 12, 3, 0, 13, 8],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    [-1, -5, -10, 1, 10, 2, -123, -34, 0, -32],
    [i32::MAX, -32984, i32::MIN, 31648, 5349857, -30954, -2343285, 0, 1, i32::MIN],
];

#[bench]
fn bench_rust_sort(b: &mut Bencher) {
    b.iter(|| {
        for i in TEST_VECS {
            (&mut i.to_vec()).sort()
        }
    })
}

#[bench]
fn bench_selection_sort(b: &mut Bencher) {
    b.iter(|| {
        for i in TEST_VECS {
            selection_sort(&mut i.to_vec());
        }
    })
}

When I run cargo bench:
$ cargo bench
  Compiling rust-algs v0.1.0 (/home/josia/projects/rust-algs)
    Finished bench [optimized] target(s) in 0.25s
     Running unittests src/lib.rs (target/release/deps/rustalgs-ae260c07593c3aad)

running 3 tests
test test_selection_sort ... ignored
test bench_rust_sort      ... bench:         106 ns/iter (+/- 8)
test bench_selection_sort ... bench:         102 ns/iter (+/- 9)

test result: ok. 0 passed; 0 failed; 1 ignored; 2 measured; 0 filtered out; finished in 7.65s

I tried this many times, and even renamed the testing function to change the order of testing. No matter what, my custom selection sort function still performs faster. I'm guessing the problem lies within the fact that I had to call a function to wrap the main default sort function. Calling the actual function wouldn't work, because even if I clone the TEST_VECS constant in the benching function as a vector, the sorting function would just keep sorting it, which won't let the other benching iterations to sort it. If I cloned the constant within the benching closure, it would affect the bench iteration's performance greatly and I wouldn't be able to just benchmark the code I'm trying to run.
Is there a problem with the way I am benchmarking these functions, or is my custom function somehow just faster?
Comment: Making an algorithm non-terrible for large cases often makes it slightly slower for tiny cases.  Here you have a sort that's optimized purely for tiny cases, using an O(N^2) algorithm that's fast-ish for 10 elements.  The standard library sort probably checks the size before eventually doing an Insertion Sort or something as a base-case for small problems. It also has extra call/return overhead unless Rust's sort fully inlines, but that should be under a nanosecond.  So this looks plausible even if nothing got optimized away.  100 ns is probably reasonable for six 10-element sorts.
Comment: Try creating a vector with 1000, 10 000, 100 000 elements and see if this still holds. Spoiler - it won't.
Comment: I don't know Rust well at all, but how does `selection_sort(&mut i.to_vec());` prevent the result from being optimized away?  Are you compiling with less than full optimization, or are you just getting lucky that the compiler fails to optimize away the useless work?
Comment: Also create vectors from arrays **outside** of `b.iter`'s closure, because now you are measuring heap allocation and copying memory in addition to just sorting.
Comment: @AleksanderKrauze: You do need to at least copy inside the iter, unless you *want* to keep sorting the same sorted array.  (Selection Sort doesn't take advantage of that; Insertion Sort does, so that might make `.sort()` much faster if that's what it uses for a base-case.)  And making tons of copies ahead of time and looping over them will give you cache misses, which is worse than a couple AVX vectors of load/store that hit in L1d cache.  (10x 32-bit elements is 40 bytes, one YMM vector and one scalar 64-bit copy if compiling for modern x86, like `-C target-cpu=x86-64-v3` or `native`)
Comment: @AleksanderKrauze I tried creating vectors outside of the closure, but now the default sort is around `33ns` and the custom sort is around `201ns`. I get that the default sort should be faster than most algorithms, but I'm still not believing that it would have this much of difference, especially with what @PeterCordes said about the test vectors being small and Rust's sort being optimized for larger cases. I think the problem is with the cloning required for each iteration of the benchmarking. If don't clone, it would just solve it once and solve already solved vectors
Comment: @PeterCordes how would I prevent the code being optimized away, if it is? I have tried researching and learned about `black_box`. It apparently fixes the code being optimized when benchmarking, but since I am new to Rust, I couldn't really understand much about it. How would I use something like that in this scenario?
Comment: Probably `black_box(sorted_vec[2]);` or something to force it to load an element of the result, or maybe just to have it in memory without forcing it to load into a register.  It's possible it could optimize away work that it can prove doesn't lead to arr[2] if it's super clever, but probably fine.  Perhaps `black_box(sorted_vec)` to tell it the whole array is an input to the black box.  (So assign the sorted array to a variable so you can reference it, or I guess use the `.sort()` return value.)  I briefly skimmed https://gendignoux.com/blog/2022/01/31/rust-benchmarks.html
Comment: https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort confirms that for short slices, an in-place Insertion Sort is used.  If this does inline, it might be fully unrolling that sort the way it is for Selection Sort.  Or not, since Selection does less data movement but more compares, so is maybe easier for the compiler to keep more values in registers as it compares.  (Insertion Sort has an O(N) best case for already-sorted data, unlike Selection Sort.  When you aren't hitting anywhere near that best case, maybe not surprising fully-unrolled Selection with `-C opt-level=3` is faster.)
Answer: TL:DR:

Rustc -C opt-level=3 fully inlines and fully unrolls your selection_sort for this compile-time constant size=10.  With opt-level=2 it inlines but compiles to a loop like the source.

Rust's .sort uses Insertion Sort for sizes this small, but for some reason it doesn't inline.  So that's actually running a not-unrolled loop with a size that's a runtime-variable (as far as it knows).  Note the caller doing mov  esi, 10 / ... / call  core::slice::sort::insertion_sort_shift_left.  I think this is the key thing that's making .sort slightly slower.

Copying from TEST_VECS is fine although maybe a bit small; modern CPUs might be learning the pattern of branching with the branch predictors and doing better than you'd expect as part of a larger program for unsorted inputs of that size.  You could have more than 6 slices and still not be getting and L1d cache misses.
It would be better to avoid alloc/dealloc inside the repeat loop.  (Use .clone_from_slice (as described here) into a slice or vector that gets allocated outside the repeat loop).  Both loops are paying for the same alloc/dealloc overhead, and that should balance out.  The compiler already does the actual copying efficiently here.
Rust's .sort doesn't internally alloc anything for slice sizes this small (using Insertion Sort), and passing/returning a Vec optimizes away to just in-place sorting, no extra copying.

black_box could make this more robust against potential optimizations that defeat the repeat-loop, but it looks like that isn't happening with current versions.

Selection Sort has O(N^2) time complexity (best and worst case). Insertion Sort is O(N) for already sorted arrays, which is one reason it's the standard choice for small problems (and sub-problems of divide-and-conquer algorithms for larger arrays), but in general is O(N^2) average and worst case.  For large problems, all O(N^2) sorts are garbage, so .sort and .sort_unstable use O(N log N) algorithms that are much faster than selection_sort. Murasame's answer measured a factor of 15 speed difference for size=1000.  (https://doc.rust-lang.org/std/vec/struct.Vec.html#method.sort / https://doc.rust-lang.org/std/primitive.slice.html#method.sort_unstable)

If your CPU is Skylake-family, you should look for a Rust equivalent of How can I mitigate the impact of the Intel jcc erratum on gcc? - without controlling for that factor, one microbenchmark might get bitten by it while another doesn't.

So we can say that Selection Sort compiles to faster asm with current Rustc with -C opt-level=3, for small compile-time-constant Vecs that aren't (close to) already sorted.  Based on what we're seeing in the asm as the apparent reason for these results, I expect it would be different if any of those factors were different.  Perhaps even different tuning defaults for different ISAs could make it not fully unroll.  Also, it's somewhat important that mainstream x86-64 CPUs can handle such large loops fully efficiently (via a large-enough uop cache).

selection_sort inlined and fully unrolled, .sort didn't
Your measurements are probably correct for this specific case, of size=10 as compile-time constant that the compiler can see.  rustc -C opt-level=3 is fully inlining and fully unrolling your selection_sort, but emitting a call to the standard library's Insertion Sort.  So .sort() includes the loop overhead of dealing with a runtime-variable slice size.
(I don't know how to get the Matt Godbolt's compiler explorer to show me asm for the actual #[bench] functions, so I made my own simple counted-loop caller on the assumption that inlining choices would be basically the same into b.iter(|| { ... }) loops.  Godbolt - change which call is uncommented to see asm for the other one.)
Normally Insertion Sort is just as good as Selection Sort for unsorted data like yours (both N^2 with similar constant factor), but it does have more data movement which maybe doesn't lend itself as well to fully unrolling for a compile-time-constant .len().  Or some difference in the way it comes from the standard library vs. your selection_sort being simple and fully there in the source file, and written for i32 specifically?  But .sort does inline and optimize away the logic to choose insertion_sort_shift_left without any of the large-slice code still being present, so nothing is inherently stopping the compiler, it just chose not to.
Anyway, I think fully unrolling the outer and inner loops is what's making selection_sort fast here.  After copying a TEST_VEC into a newly-allocated Vec, the asm goes right into comparing, like
# rustc nightly -C opt-level=3  for x86-64
# inside the main repeat loop in  bench_selection_sort
...
        mov     edi, 40
        mov     esi, 4
        call    rbp              # call alloc
        test    rax, rax
        je      .LBB0_4          # check for null
# pointer to a slice of TEST_VECS in R15
# pointer to the newly-allocated Vec<i32> in RAX
        movups  xmm0, xmmword ptr [r15]
        movups  xmm1, xmmword ptr [r15 + 16]
        movups  xmmword ptr [rax], xmm0          # with -C target-cpu=x86-64-v3
        movups  xmmword ptr [rax + 16], xmm1     # just one YMM 32-byte copy is needed, vs. 2x 16-byte with baselien SSE2
        mov     rdx, qword ptr [r15 + 32]
        mov     qword ptr [rax + 32], rdx      # copy last 8 of the 40 bytes
# and now the sorting
        mov     ecx, dword ptr [rax]
        mov     edi, dword ptr [rax + 8]
        xor     esi, esi
        cmp     dword ptr [rax + 4], ecx
        setl    sil                  # 0 or 1 according to vec[1] < vec[0]
        mov     r9d, 2
        cmp     edi, dword ptr [rax + 4*rsi]   # compare at a data-dependent address; surprising for Selection Sort
        jl      .LBB0_9
        mov     r9, rsi            # strange that this isn't a CMOV; the only branch to .LBB0_9 is the preceding line
.LBB0_9:
        mov     esi, dword ptr [rax + 28]   # load several elements
        mov     edi, dword ptr [rax + 24]   # that it's going to cmp/branch on later
        mov     r10d, dword ptr [rax + 16]
        mov     r8d, dword ptr [rax + 20]
        mov     ebx, dword ptr [rax + 12]
        mov     r11d, 3
        cmp     ebx, dword ptr [rax + 4*r9]
        jge     .LBB0_10
  ... over 400 more instructions before the bottom of the loop, mostly mov / cmp/jcc

Test data
Sorting the same data repeatedly is more or less fine, although "only" 6x 10-element sorts might be small enough for your CPU to learn the pattern of branching.  (Modern IT-TAGE predictors use previous history to index a prediction make it possible to predict patterns for the same branch, so this doesn't necessarily give the fully-unrolled selection_sort a big advantage, especially since you're sorting 6 different slices so even full unrolling doesn't make a branch go the same way every time.)  Your testing with perf stat finding only about 1.5% mispredict rate seems lowish, but that is including loop branches.  Try making the arrays a bit longer and/or adding another few.
A few years ago, I was playing with a hand-written asm Bubble Sort from a code-golf exercise, and my Skylake was able to learn its pattern of branching for input sizes up to about 14 elements IIRC, near-zero mispredicts until I made the size larger.  And that's Bubble Sort, one of the worst O(N^2) sorts, plus its loop branching.  I was benchmarking it with a caller that just did a couple vmovdqa ymm stores from vector regs, which is the cheapest possible way to re-generate a small input for an in-place sort.  Rust is loading that data each repeat-loop iteration since there are 6 different sources, but that's only negligibly more expensive.  Scalar loads inside the sort function can load right away without even waiting for the vector stores to commit to L1d cache; modern x86 CPUs have good store-to-load forwarding for scalar reloads of vector stores.  Everything hits in L1d cache.
Another option would be a vectorized xorshift+ like in What's the fastest way to generate a 1 GB text file containing random digits? to get fresh data to sort every iteration.  Using the sorted array as the seed for an LFSR (like Murasame's answer) would be a way to benchmark latency instead of throughput, but will also have a store-forwarding stall when you do a SIMD vector load of the array that was just written by scalar stores.  (Assuming the compiler auto-vectorizes your loop.)

O(N^2) sorts like Selection Sort are slow on bigger arrays
I assume you already know this, but Selection Sort has O(N^2) time complexity so it's garbage for larger sizes.  Rust's .sort and .sort_unstable use O(N log N) sorts for larger problems (iterative merge "inspired by timsort", or pattern-defeating quicksort respectively), but use Insertion Sort for small sizes, or sub-problems of the larger sorts. Here the size is a compile-time constant and Rust's .sort function can inline enough to optimize away branching on the size to pick a strategy, so it's just your selection_sort vs. the standard library's Insertion Sort (core::slice::sort::insertion_sort_shift_left) for problems of this size.
(Surprisingly, .sort_unstable inlines less (Godbolt), calling core::slice::sort::recurse which checks the size at run-time for being less than 21, otherwise continuing into its quicksort.)
Insertion Sort and Selection Sort do well for small arrays where more complex  algorithms would spend too much time sub-dividing an already-small problem.  That's why real-world sorts use Insertion Sort once they've sub-divided and partially-sorted enough to create small sub-problems.  (Or a sorting network, perhaps using SIMD vectors.)
Stable sorting is irrelevant when the objects being sorted are simple integers, not a struct with a key and some other data.  Equal i32 objects are indistinguishable.  But it seems .sort doesn't try to detect that since we get different asm from .sort and .sort_unstable.

black_box
You don't do anything to stop the sort from being optimized away; the resulting vector is unused.  But it appears current versions of rustc -C opt-level=3 miss that optimization so your benchmark appears valid.
Using black_box(sort_result) would be safer / future-proof, although if the compiler did fully see through your benchmark it could just copy from already-sorted arrays to satisfy the black_box requirement to materialize a sort result somewhere.  So to be fully safe, you'd use black_box on the input slice or vector and then on the output; hopefully black_box makes the compiler forget what values are there, i.e. treat it like a function call that might have modified the slice, not just read it.
And avoiding alloc/dealloc
The copying is super cheap, just 2 vector (movups) and 1 scalar (mov rdx, [r15 + 32] / mov [rax+32], rdx) load+store.
But the alloc/dealloc could be avoided with a better benchmark.  I was wrong about this in comments; I was looking for a call some_longer_name but actually Rustc likes to load library function pointers into registers (if they're going to be called inside loops) so you have to watch for the call mnemonic to spot instructions like call rbp.
How can I copy a vector to another location and reuse the existing allocated memory? describes how to use .clone_from or .clone_from_slice() to copy the data from a slice into an already-allocated Vec.  I looked at the asm to confirm that there were no call instructions inside the for i in TEST_VECS { loop, unlike the original.
use core::hint::black_box;

// I don't know how to get it to show asm for the #[bench] version
pub
fn xbench_selection_sort(a: i32) {
    // allocate correct amount of storage once, and redundantly copy into it because I don't know Rust very well
    let mut test_vec = TEST_VECS[0].to_vec();
    for _b in 0..a {  // just a counted repeat loop
        for i in TEST_VECS {
            //let mut test_vec = i.to_vec();  // would alloc/free inside the loop
            test_vec.clone_from_slice(&i);    // copy into already-allocated space
            black_box(&mut test_vec.as_slice());  // force the data (but not the pointers and size) to exist in memory
            selection_sort(&mut test_vec);
            black_box(&mut test_vec.as_slice());  // force the result data to exist.  Apparently costs at least 1 extra asm instruction somewhere, unfortunately.
     // actually this does store the pointers+length to the stack, not avoiding it like I hoped.

            //black_box(&mut test_vec[4]);   // or just ask for one element; often enough to stop a compiler from optimizing away a whole loop.
        }
    }
}

(Godbolt)
If I understand correctly .as_slice() on a Vec is like C++ .data() on a std::vector, except it's a slice so it has a length.  Anyway, the idea is to avoid asking the compiler to actually store pointer and length to stack space.  But I'm not sure that worked: I am still seeing stores like mov  [rsp + 24], r14 ; mov  qword ptr [rsp + 32], 10 etc.
Using Vec in the first place is unnecessary; for these small sizes, just a slice in stack space would be fine, and probably would have avoided any alloc/dealloc overhead even if we had been cloning from constant slices since the compiler would know it could reuse stack space.
This function does a 240-byte memcpy of TEST_VECS to stack space every outer iteration.  (It copies from there to the Vec object.)  I don't know why that's happening, but I barely know Rust.  It happens without any use of black_box so it's not being triggered by that.
Answer: Correct way to test
Thanks to @PeterCordes
#![feature(test)]
#![allow(dead_code)]

use rand::Rng;

extern crate test;
fn selection_sort(original_vec: &mut Vec<i32>) -> Vec<i32> {
    for i in 0..original_vec.len() - 1 {
        let mut smallest: usize = i;
        for j in i + 1..original_vec.len() {
            if original_vec[j] < original_vec[smallest] {
                smallest = j;
            }
        }
        if smallest != i {
            original_vec.swap(i, smallest);
        }
    }
    original_vec.to_vec()
}

#[bench]
fn bench_rust_sort_10(b: &mut test::Bencher) {
    let mut rng = rand::thread_rng();
    let mut v = (0..10)
        .map(|_| rng.gen_range(-2000..2000))
        .collect::<Vec<i32>>();

    b.iter(|| {
        v.iter_mut().for_each(|i| {
            *i ^= *i << 13;
            *i ^= *i >> 17;
            *i ^= *i << 5;
        });

        v.sort()
    })
}

#[bench]
fn bench_selection_sort_10(b: &mut test::Bencher) {
    let mut rng = rand::thread_rng();
    let mut v = (0..10)
        .map(|_| rng.gen_range(-2000..2000))
        .collect::<Vec<i32>>();

    b.iter(|| {
        v.iter_mut().for_each(|i| {
            *i ^= *i << 13;
            *i ^= *i >> 17;
            *i ^= *i << 5;
        });
        selection_sort(&mut v);
    })
}

#[bench]
fn bench_rust_sort_1000(b: &mut test::Bencher) {
    let mut rng = rand::thread_rng();
    let mut v = (0..1000)
        .map(|_| rng.gen_range(-2000..2000))
        .collect::<Vec<i32>>();

    b.iter(|| {
        v.iter_mut().for_each(|i| {
            *i ^= *i << 13;
            *i ^= *i >> 17;
            *i ^= *i << 5;
        });
        v.sort()
    })
}

#[bench]
fn bench_selection_sort_1000(b: &mut test::Bencher) {
    let mut rng = rand::thread_rng();
    let mut v = (0..1000)
        .map(|_| rng.gen_range(-2000..2000))
        .collect::<Vec<i32>>();

    b.iter(|| {
        v.iter_mut().for_each(|i| {
            *i ^= *i << 13;
            *i ^= *i >> 17;
            *i ^= *i << 5;
        });
        selection_sort(&mut v);
    })
}

fn main() {
    let mut rng = rand::thread_rng();
    let mut v = (0..10)
        .map(|_| rng.gen_range(-2000..2000))
        .collect::<Vec<i32>>();

    let mut max_counter = vec![0; 10];

    (0..10000).for_each(|_| {
        v.iter_mut().for_each(|i| {
            *i ^= *i << 13;
            *i ^= *i >> 17;
            *i ^= *i << 5;
        });
        let max = v.iter().max().unwrap();
        let index = v.iter().position(|&r| r == *max).unwrap();
        max_counter[index] += 1;
        println!("{:?}",v);
        v.sort();
    });
    println!("{:?}", max_counter);
}

Test Result:
➜  bench git:(master) ✗ cargo bench
   Compiling bench v0.1.0 (/home/Murasame/Dev/bench)
    Finished bench [optimized] target(s) in 0.41s
     Running unittests src/main.rs (target/release/deps/bench-c25271d7fcaf578a)

running 4 tests
test bench_rust_sort_10        ... bench:          85 ns/iter (+/- 0)
test bench_rust_sort_1000      ... bench:      28,793 ns/iter (+/- 231)
test bench_selection_sort_10   ... bench:         160 ns/iter (+/- 10)
test bench_selection_sort_1000 ... bench:     425,774 ns/iter (+/- 2,055)

test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured; 0 filtered out; finished in 1.59s

➜  bench git:(master) ✗ cargo bench
    Finished bench [optimized] target(s) in 0.00s
     Running unittests src/main.rs (target/release/deps/bench-c25271d7fcaf578a)

running 4 tests
test bench_rust_sort_10        ... bench:          85 ns/iter (+/- 0)
test bench_rust_sort_1000      ... bench:      28,685 ns/iter (+/- 144)
test bench_selection_sort_10   ... bench:         162 ns/iter (+/- 1)
test bench_selection_sort_1000 ... bench:     426,345 ns/iter (+/- 2,757)

test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured; 0 filtered out; finished in 1.37s

➜  bench git:(master) ✗ cargo bench
    Finished bench [optimized] target(s) in 0.00s
     Running unittests src/main.rs (target/release/deps/bench-c25271d7fcaf578a)

running 4 tests
test bench_rust_sort_10        ... bench:          85 ns/iter (+/- 0)
test bench_rust_sort_1000      ... bench:      28,681 ns/iter (+/- 860)
test bench_selection_sort_10   ... bench:         161 ns/iter (+/- 3)
test bench_selection_sort_1000 ... bench:     425,643 ns/iter (+/- 6,462)

test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured; 0 filtered out; finished in 1.43s

The Cargo run:
➜  bench git:(master) ✗ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/bench`
[1032, 951, 1023, 985, 976, 1018, 986, 992, 1000, 1037]
➜  bench git:(master) ✗ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/bench`
[1151, 972, 952, 1006, 969, 995, 988, 953, 989, 1025]
➜  bench git:(master) ✗ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/bench`
[981, 946, 1006, 1031, 983, 999, 1003, 1069, 1047, 935]

Another problem answer
If you are seeing this post, please read the comment for more information about this post.
Old answer is totally wrong.
Thanks to @PeterCordes;
#![feature(test)]
#![allow(dead_code)]

extern crate test;
use rand::Rng;
fn selection_sort(original_vec: &mut Vec<i32>) -> Vec<i32> {
    for i in 0..original_vec.len() - 1 {
        let mut smallest: usize = i;
        for j in i + 1..original_vec.len() {
            if original_vec[j] < original_vec[smallest] {
                smallest = j;
            }
        }
        if smallest != i {
            original_vec.swap(i, smallest);
        }
    }
    original_vec.to_vec()
}

#[bench]
fn bench_rust_sort_10(b: &mut test::Bencher) {
    let mut rng = rand::thread_rng();
    b.iter(|| {
        let mut v = (1..10).map(|_| rng.gen_range(0..2000)).collect::<Vec<i32>>();
        v.sort()
    })
}

#[bench]
fn bench_selection_sort_10(b: &mut test::Bencher) {
    let mut rng = rand::thread_rng();

    b.iter(|| {
        let mut v = (1..10).map(|_| rng.gen_range(0..2000)).collect::<Vec<i32>>();
            selection_sort(&mut v);
        }
    )
}

#[bench]
fn bench_rust_sort_1000(b: &mut test::Bencher) {
    let mut rng = rand::thread_rng();
    b.iter(|| {
        let mut v = (1..1000).map(|_| rng.gen_range(0..2000)).collect::<Vec<i32>>();
        v.sort()
    })
}

#[bench]
fn bench_selection_sort_1000(b: &mut test::Bencher) {
    let mut rng = rand::thread_rng();

    b.iter(|| {
        let mut v = (1..1000).map(|_| rng.gen_range(0..2000)).collect::<Vec<i32>>();
            selection_sort(&mut v);
        }
    )
}

fn main() {}

result:
➜  bench git:(master) ✗ cargo bench
   Compiling bench v0.1.0 (/home/Murasame/Dev/bench)
    Finished bench [optimized] target(s) in 0.43s
     Running unittests src/main.rs (target/release/deps/bench-c25271d7fcaf578a)

running 4 tests
test bench_rust_sort_10        ... bench:         125 ns/iter (+/- 3)
test bench_rust_sort_1000      ... bench:      33,482 ns/iter (+/- 196)
test bench_selection_sort_10   ... bench:         133 ns/iter (+/- 3)
test bench_selection_sort_1000 ... bench:     424,931 ns/iter (+/- 18,055)

test result: ok. 0 passed; 0 failed; 0 ignored; 4 measured; 0 filtered out; finished in 0.83s

Old answer below

you are converting Type and Coping memory.

Convert Array to Vector needs time.
If you are coming from other languages, you should know the constant cannot be modify.

So you have to define the test set in the bench code.

The b:&mut test::Bencher will be used for bench.
The code out-side of b.iter will not be count in bench.

So you have to prepare your code like this:
#![feature(test)]
#![allow(dead_code)]
extern crate test;
fn selection_sort(original_vec: &mut Vec<i32>) -> Vec<i32> {
    for i in 0..original_vec.len()-1 {
        let mut smallest: usize = i;
        for j in i+1..original_vec.len() {
            if original_vec[j] < original_vec[smallest] {
                smallest = j;
            }
        }
        if smallest != i {
            original_vec.swap(i, smallest);
        }
    };
    original_vec.to_vec()
}

const TEST_VECS: [[i32; 10]; 6] = [
    [1, 3, 2, 9, 6, 7, 4, 10, 8, 5],
    [1, 2, 7, 2, 9, 9, 7, 10, 2, 1],
    [0, 4, 1, 3, 9, 12, 3, 0, 13, 8],
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    [-1, -5, -10, 1, 10, 2, -123, -34, 0, -32],
    [i32::MAX, -32984, i32::MIN, 31648, 5349857, -30954, -2343285, 0, 1, i32::MIN],
];

#[bench]
fn bench_rust_sort(b: &mut test::Bencher) {
    let mut test_vecs: Vec<Vec<i32>> = TEST_VECS.iter().map(|v| v.to_vec()).collect();
    b.iter(|| {
        for i in test_vecs.iter_mut() {
            i.sort()
        }
    })
}

#[bench]
fn bench_selection_sort(b: &mut test::Bencher) {
    let mut test_vecs: Vec<Vec<i32>> = TEST_VECS.iter().map(|v| v.to_vec()).collect();
    b.iter(|| {
        for i in test_vecs.iter_mut() {
            selection_sort(i);
        }
    })
}

fn main(){

}

Here is the result:
➜  bench git:(master) ✗ cargo bench
    Finished bench [optimized] target(s) in 0.00s
     Running unittests src/main.rs (target/release/deps/bench-5bf7ec1e7001cf33)

running 2 tests
test bench_rust_sort      ... bench:          43 ns/iter (+/- 0)
test bench_selection_sort ... bench:         215 ns/iter (+/- 14)

test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured; 0 filtered out; finished in 6.40s

My laptop running this bench:
MacBook Pro 2020 i5, 4 core 2.0Ghz.
Comment: Thank you all for your contributions. You are giving me lots of ideas for ways to benchmark correctly, however I feel that you are starting to drift away from the actual question. I simply asked if my code was benchmarking correctly and why selection sort seems to be faster, which both seem to be answered by @PeterCordes in the comments, not the _best_ way to benchmark sorting functions. I think this answer is better suited in a question asking the best way to benchmark a sorting algorithm, or just in the comments of this post for extra info.
Comment: @Murasame I unmarked the answer. Does that mean that the way I did it in the question post was correct? I tried to do a similar thing I did in the post but with moving the vector conversion inside the function but outside the closure, but it gave a similar result to yours.
Comment: Re: your update: now you're sorting 1001 element arrays so built-in `.sort()` is of course vastly faster than `selection_sort()`.  That's not what @yees_7 was testing.  They found that their `selection_sort` was about the same speed or slightly faster than `.sort()` for 10-element arrays, and I think that was a correct measurement.  Your answer gives the impression that there was something wrong with that.
Comment: I posted an answer with some details confirming that the OP's measurement makes sense, and about how to avoid alloc/dealloc overhead while copying using `.clone_from_slice()`.  I was wrong earlier and missed the `call` in the original asm.
Comment: [Selection Sort](https://en.wikipedia.org/wiki/Selection_sort) doesn't get faster when the data is already sorted (still O(N^2) comparisons), but Rust's built-in sort probably does (perhaps Insertion Sort for small problems, which does one pass in the already-sorted case).  You've changed the benchmark to keep sorting already-sorted arrays after the first iteration of the outer loop.  So of course Selection Sort is a lot slower than the best case of the builtin sort; the OP was trying to test its average case.
Comment: @PeterCordes Oh, thanks. I checked the bench's document. It will keep bench over and over again. but the test set has already changed after the first run.
Comment: Does `iter_mut` copy the vector in this function? It must always at least be copied, or else it will just sort the same sorted array. Unless there is some other way to only test the specific function call itself and not the copying required.
Comment: @yees_7: https://doc.rust-lang.org/std/slice/struct.IterMut.html - it just gets an iterator that gives mutable references to the members of the collection.  Looping over it lets you modify the original.  It's like C++ `std::vector` `.begin` (as opposed to `.cbegin` to get a const (non mutable) iterator).  As I commented, this answer looks like it's sorting the already-sorted data, which explains why it's so much faster.
Comment: @yees_7 Please don't think this answer is right. You can think the `iter_mut` is making every field in array/vector is mutable referenced by default
Comment: @PeterCordes I think we can done it better with rand.
Comment: Generating fresh random numbers between sorts is slower than a couple `movups` load/store instructions!  With a counted repeat loop (instead of the bench loop) around the OP's selection_sort loop so I can get https://rust.godbolt.org/z/a6zYzh5dv to show me the asm, it looks like overhead is very minimal, just the 40-byte copy (which will hit in L1d cache).  It takes 3 loads and 3 stores, 2 of them using 16-byte XMM vectors, one using an 8-byte integer reg.  Store-forwarding makes this work nicely when the sort code starts reloading it, no stalling.  (Yes, @yees_7, your results should be good)
Comment: Re-randomizing every time means that branch-prediction isn't going to learn the patterns, if that was happening with 6 different arrays.  If you used a fast PRNG like xorshift+, you wouldn't be adding much constant overhead to the sort times.  e.g. in C++ with AVX2 intrinsics, you could be generating 32 bytes of randomness at a time with a few instructions, as in [What's the fastest way to generate a 1 GB text file containing random digits?](https://unix.stackexchange.com/a/324520) (it doesn't have to be high-quality randomness to test a sort).
Comment: It sounds like a good way to test, and was one of the things I thought about while making the testing function. My main concern with it was that it would generate different vectors for each testing function, so it wouldn't really compare the two for the same input. Should I worry about this or should I just care that it's testing an average of the sort?
Comment: @PeterCordes But that's okay because we are testing the sort algorithm.
And the thread_rng's performance is suit for this case.
Comment: @yees_7: Check with `perf stat` to see if branch mispredict rate is very low.  If so, then your CPU is "learning" the pattern of branching across all 6 sorts.  That's unrealistic for most cases except when already-sorted arrays are common, and then you'd avoid Selection Sort like the plague.  If you want to see what difference that makes, make TEST_VECS smaller, like maybe only one 10-element vector.  (IIRC, my Skylake CPU learned the pattern of branching in a hand-written asm Bubble Sort for sizes up to about 14 elements re-sorting the same unsorted input repeatedly, with near 0 mispredicts)
Comment: @PeterCordes I think make four test is suit for our target.
Comment: Yes, testing both small 10-element and 1000-element sorts makes more sense, showing the obvious point that an O(N^2) algorithm gets really slow for larger inputs.  Your numbers don't seem very stable between edits, though; 424,931 ns / iter this time for 1000 element selection_sort, vs. 316,458 ns/iter for 1001 element selection_sort.  But I'm still not convinced that there was anything really wrong with @yees_7 's original way of testing.  Doesn't `.as_vec()` make a copy?  You complain that converting an array to a vector needs time, but so does generating random numbers with ChaCha12!
Comment: @PeterCordes I'm guessing you want me to `perf stat cargo bench` (I might be wrong). If so, it gave me two results for the amount of branch misses: `98.52%` for `cpu_core/branch-misses/u` and `1.54%` for `cpu-atom/branch-misses/u`. I don't really know much about computer architecture so I have no clue what the difference is, but what would these results mean?
Comment: @PeterCordes He wants to know why his custom sort is 'faster' than rust sort. He needs to test a large sets but he didn't. I get the main problem wrong, I think the memory copy is the main problem. Now I think current answer is still have problem. Rand is not a stable way to create test set. And collect to vector still have cost. I think I can make a mutable vector for contain testset. and use something like XOR with bit-shift for generate random to stable the time.
Comment: @yees_7: `perf stat` on my Skylake just shows a `branches` and `branch-misses` event, not `cpu_core` vs. `cpu-atom`.  Maybe that's an Alder Lake P-core vs. E-core think since the E cores are derived from Silvermont Atom?  1.54% looks plausible; some of the branching is loops with fixed counts.  But it's still pretty low, try a larger TEST_VECS and/or larger arrays to see if it changes much. 98.5% is not plausible; even random guessing would be right about 50% of the time, and it's unlikely that you randomly found a pessimal case that tricks branch prediction into being worse than random guess.
Comment: @Murasame: Copying some test vectors is the fastest way to re-generate the same inputs for a sort.  I looked at the asm on Godbolt (in an earlier comment) and it really is just 3 loads and 3 stores.  (Or two with `-C target-cpu=x86-64-v3` to allow AVX: https://rust.godbolt.org/z/ob7o7119Y see the `vmovups ymm0, [r14]`.)  It's not allocating new space every time, it's allocating stack space once at the top of the function.  The only downside is the same pattern of branching every time, but if you have enough different vectors the CPU won't learn the pattern.
Comment: If you did want to use a PRNG, generating numbers in a limited range is slower: to avoid bias, it has to discard some samples.  https://github.com/al-jshen/alea/tree/master/benches has some benchmarks of a custom prng vs. the standard one, which is ChaCha12, a CSPRNG.  (https://rust-random.github.io/rand/rand/rngs/struct.ThreadRng.html).  It's fast for a CSPRNG, but PRNGs that aren't trying to be cryptographically secure can be even faster.
Comment: Using AVX2 to do some xors and shifts on 32 bytes at once is also fast if you want to sort different data each time; that's basically the xorshift+ PRNG, which my answer on [What's the fastest way to generate a 1 GB text file containing random digits?](https://unix.stackexchange.com/a/324520) shows (with C++ intrinsics).  Running four generators in parallel in the 64-bit elements of AVX2 vectors gives us excellent throughput, only a few instructions per 32 bytes of randomness (8x i32).  Only a bit more expensive than just copying, but gives different data each time.
Comment: Since you mention "stable", you could use the same seed for the same PRNG in both Rust and selection sorts.  Apparently  rand::rngs::ThreadRngCopy reseeds itself from OsRng every 64KiB, so that's definitely not what you want.  Besides making some runs slower when it reseeds, it won't be the same sequence.  Use something simple and very fast like XorShift+, preferably in parallel so it can vectorize.
Comment: @yees_7: https://manpages.ubuntu.com/manpages/jammy/man1/perf-stat.1.html confirms that `cpu_core/` and `cpu_atom/` are indeed the P cores and E cores of a hybrid CPU.  Probably best to `taskset -c 1  perf stat  cargo bench` or something so your code doesn't bounce around between CPU cores.  (Doesn't have to be core #1, pick a P core or an E core.)
Comment: Re: your last edit, to ADD and XOR the sort results with two loop-invariant numbers.  Depending on what you randomly generate, that might or might not change their order much, so you're probably still testing the most-sorted case for .sort, not the average case which is O(N^2) like Selection Sort.  With full-range `i32` values (and .wrapping_add) you would sometimes be making a positive value negative which creates more sorting work.  Adding the same number to every element does nothing except for interacting with the XOR; it's the only thing that might change the order.
Comment: Randomly generating `x` and `y` seems bad; if you get a pair that rarely changes the order of elements, you're stuck with that for the whole benchmark function.  Picking some good ones like perhaps `y=0xffffcccc` might be good to always flip lots of bits.  Or do `(x - v[i]) ^ y` since negating each element will tend to make a reverse-sorted array that needs a maximal amount of data movement.  (Selection Sort doesn't care, it always does the same amount of work, so that would make Selection look good compared to Insertion Sort where that is the worst case.)
Comment: @PeterCordes I think I find the best way to generate the numbers.
Comment: Ok, so you're doing one iteration of an [LFSR](https://en.wikipedia.org/wiki/Linear-feedback_shift_register).  If it auto-vectorizes, it'll have a store-forwarding stall from a SIMD load on a small array just written with scalar stores, unlike using a vectorized PRNG, but it's saving a bit of front-end throughput in not having to store back to a separate seed vector as well as storing a result.  [Can modern x86 implementations store-forward from more than one prior store?](https://stackoverflow.com/q/46135766)
Comment: `(0..9).for_each` seems weird to me, having to hard-code the length somewhere else.  Can't you do something like `v.for_each(|x| { x ^= x << 13;  ... });` (or `mut &x`?) or [How to loop over a vector of integers in Rust?](https://stackoverflow.com/q/66092442)
Comment: @PeterCordes thats a typo. It needs to be fix. Correct answer is (0..10)
Comment: @PeterCordes I think the `iter_mut` is slower? I don't know why.
Comment: @PeterCordes And I have to let you know: (0..10) means 0-9 not 0-10. I think you may not be clear on how Range works in rust.
Comment: Thanks, I guessed wrong that it would include both ends, but it's actually like a `for(i=start ; i<end; i++)` not including the end.  https://doc.rust-lang.org/reference/expressions/range-expr.html .  I barely know Rust, but I understand performance of the machine code / asm it compiles to (and I know `rustc` uses LLVM's optimizer so it normally compiles similarly to the way C or C++ would when you write stuff that does the same work.)  Questions like this are fun way to learn some about languages I'm interested in.
Comment: IDK why `iter_mut` would be slower; perhaps a quirk of code layout in how it compiled?
 Or that was because my wrong guess had the previous version not doing a shift/xor on the last element, so doing less work.  And unfortunately it didn't vectorize in a way that did the last 1 or 2 elements with SIMD to make the speed about the same; compilers tend to be bad at using narrow vectors for cleanup.  Your 2020 i5 MBP should be an Ice Lake if review sites are right, so [How can I mitigate the impact of the Intel jcc erratum on gcc?](https://stackoverflow.com/q/61256646) won't affect it.
Comment: @PeterCordes I have a lots of friends doing Programming language research. We all agree that Rust is a well designed language. If you interested in it, you should learn it.
Comment: @PeterCordes And I find an issue talks about `iter_mut`. https://github.com/rust-lang/rust/issues/31890


Properly defining environment variables in Cloud Run for my React App

Question: I am currently deploying a react application (front-end only) using Cloud Run, I have created a trigger for a cloud build to run which deploys the app using Cloud Run.
However, when I'm trying to create some environment variables to access in my components using cloud run UI I cannot access them due to the fact (from my understanding) that the environments are defined within the instance of the app and not the user's browser.
So my question is - How should I properly approach this issue?
I've tried perhaps building sort of "config.json", but I wasn't able to find a proper way to mount the files in different environments.
Comment: are you using `create react app`?
Comment: Yes, i'm using CRA
Comment: Does this answer your question? [How to use ENV Variables Declared on Google Cloud Run Dashboard in React](https://stackoverflow.com/questions/64016442/how-to-use-env-variables-declared-on-google-cloud-run-dashboard-in-react)
Comment: @LluísMuñoz This describes my problem yes, however I did not manage to find a solution there.
I didn't understand what processing on cloud run mean?
Comment: As I understand it the environment variables declared in the Cloud Run console are used when running a process in Cloud Run deployment. Since your application is front-end only you are not executing any processes in Cloud Run, besides serving the static files. Therefore the processing is being done in the users' browser, which is where your React app is looking for environment variables, not your Cloud Run deployment.
Answer: You'll want to do the following. First, install https://www.npmjs.com/package/env-cmd
Next, you can specify the .env to be used depending upon your environment as so. The below code allows you to specify configuration declaratively rather than imperatively. Now, when you say process.env.REACT_APP_ENV1 process.env.REACT_APP_ENV2 process.env.REACT_APP_ENV3, then depending on env the correct values will be transparently picked up.
"scripts": {
    "start": "env-cmd -f env_anotherenv.env react-scripts start",
    "start_vscode": "env-cmd -f env_vscode.env react-scripts start",
    "build_staging": "env-cmd -f env_staging.env react-scripts build --profile",
    "build_production": "env-cmd -f env_production.env react-scripts build --profile"
}

Example of env_production.env will be
REACT_APP_ENV1 = someenv1
REACT_APP_ENV2 = someenv2
REACT_APP_ENV3 = someenv3

Example of env_staging.env will be
REACT_APP_ENV1 = someotherenv1
REACT_APP_ENV2 = someotherenv2
REACT_APP_ENV3 = someotherenv3

Comment: I want to avoid pushing .env files to the repository, how will this work when I deploy my app using cloud build + run?
Comment: @kishorekumaru you can't to anything about that, it is a react app and runs in the browser and as such env variable that the react app running in the browser needs will always be exposed.

If you want to hide env variables then run it on the server
Comment: This would expose our env variables in the repository
Answer: I would suggest you bake in your environment variables in your build artifacts which you then access using process.env.VARIABLE_1 as described here.
Use a multi-stage docker build. In your build stage populate a .env file from build-args (e.g using envsubst) before running npm run build. Here is a sample using a template that you can push to your git repo.
ARG VARIABLE_1
ARG VARIABLE_2
COPY .env.template .
RUN /bin/sh -c "envsubst '\$VARIABLE_1 \$VARIABLE_2' < .env.template > .env"

.env.template
VARIABLE_1=${VARIABLE_1}
VARIABLE_2=${VARIABLE_2}

Depending on how your cloud build triggers are set up, the values for the build args can be sourced from substitution variables.
The final stage can be based on the nginx image, here you copy the built static files bundle and serve using nginx. See a full docker example here
This way you do not expose a .env file and there is no environment variables configuration to be done from the cloud run end.


How do I change my navbar from "left to right" to "right to left"?

Question: I built an English language web site, and now I'm working on the Arabic version.
How do I change the navigation bar to be displayed right to left?
I'm using the Bootstrap theme 3.3.5.
Comment: Are you using the Bootstrap theme's default navbar(which has logo/site-name/menu-items/My-account/logout). Or you have navigation with just items?
Comment: yes i'm using the default one
Comment: Your need can be done using CSS ro nav-bar. But I think you should see also RLT bootstrap theme here(http://stackoverflow.com/questions/19730598/right-to-left-support-for-twitter-bootstrap-3).
Comment: Since for D8 there is only a 3.0 release, and for D7 there is a 3.6 release (already), my best guess is that this question is about D7. However, to answer questions "I" do not like to guess about the release of Drupal. Please edit your question to avoid we have to guess ...
Answer: Note: Following answer is for D7, but the concept is same for D8 also I think, if you want to do using CSS, Just you need to apply the same logic where to apply CSS float: left; and float: right;.
This you can do with CSS.
If there is just a menu with list of items, you can do easily with float: right at appropriate place.
But, bootstrap's default nav-bar contains Logo Site name primary-menu and Secondary-menu, Hence css will be some complicated.
Now, I have used the following set (LTR Default),

And after looking their HTML I have applied following CSS,
.navbar {
    .navbar-header {float: right;
        a.logo { float: right !important;}
    }
    .navbar-collapse{
        nav {

            ul.menu.navbar-nav {float: right;
                li {
                    float: right;
                }
            }
            ul.menu.navbar-nav.secondary {float: left !important;
                li {
                    float: right;
                }
            }

        }
    }
}

After applying the CSS, It has arranged in such a way.

If you slightly practice, you can by yourself come to know how it works, it is easy.
Comment: I have the impression that this solution could work for either D7 or D8, am I right? I think you should add something about that (an extra "note" or so) to clarify that also ...
Comment: @Pierre.Vriens Thanks for pointing out. This answer is for D7, I will add that one. Thanks again.


Link to audio in XHTML/EPUB

Question: I'm looking into synchronizing an ebook in epub format (so the content is in XHTML) to an audio file. I'm thinking of putting something along the lines of:

into the body of the document, and then have a custom epub reader that recognizes those tags and synchronizes the audio accordingly. That does seem like a bit of a hack  to me though, especially the use of a special class name.
Does anyone have any pointers to how this may be done in a more standards-compliant manner (or somewhere where it has been done before)? Ebooks with audio annotation seem like an idea that may already be out there.
Answer: Yes, it's an old idea, and yea-- it's a lot of work. See the DAISY Talking Book (DTBook) specification:
A subset of the DAISY format has been adopted by law in the United States as the National Instructional Material Accessibility Standard, and K-12 textbooks and instructional materials are now required to be provided to students with disabilities.
The EPUB spec includes the Open Publication Structure (OPS) spec which describes the content markup, which can be either XHTML, or ... wait for it... DTBook. The good thing for you is that DTBook is based on HTML, XHTML, and SMIL.
So you could use DTBook and some of the publishing toolchains that are available for authoring DTBooks, and then just wrap it up into an epub.
Synchronized reading is being pursued by an iphone startup, Enhanced Editions: http://www.enhanced-editions.com/blog/2010/02/enhancing-the-ebook/, so you'll have company.
Good luck.
Comment: Fantastic answer, thanks. Great resources for me to look at here.
Comment: Did you succeed? Which readers support DTBook audio? Are the others violating the EPUB spec?

Link to audio in XHTML/EPUB


Entity Framework Power Tools Beta 3 throws exception "sequence contains no matching element" when trying to generate views

Question: Struggling to create Entity Framework precompiled views for database and need help.
While using Visual Studio 2010, I have been unable to generate precompiled views using latest version of the Entity Framework Power Tools Beta 3.  When I right mouse click on the dbContext based file and select "Generate Views" option, it pops up a "Sequence contains no matching element" dialog error box.
Uninstalling the power tools extension, rebooting Visual Studio and computer has not helped.
Question 1:  What could be causing this error.  Entity Framework Power Tools is worthless until I can get it working?
So up until now the only way I'm able to generate views for this sequence is by using a T4 template (from Internet) but it often times-out and fails to complete.  The database has about 350 tables and the number of views it creates is about 670 views.  However, it fails to create the views the majority of the time and I have to repeatedly try and sometimes shut down and reboot to get it to work.  CPU  utilization during this time is about 12% on an i7 Quad core with SSD and 8GB of memory so it's strange.  However when the views are generated (no timeout) they work fine.
Question 2:  Is there anything that can be done to keep this template from timing-out and failing?
Now unable to just give up I've looked into trying other ways, I was able to generate the views by creating a database first project and adding an Entity Framework Data Model created from an existing database.  The model is created fine and I'm able to create the views as expected but they are not recognized when placed in the final assembly and entity tries to query the database.  When placing in the assembly I replaced the view file that works when generated using T4 template the file created by the Power Tools context menu.
Is the namespace causing this problem?
I think it has something to do with the namespace but I don't know the rules on how it finds out the compiled views and what does the namespace and views have to be called in order to be found and used.  The views created from the DbContext file is in a different project than the views created using the Edmx model of the same database.  I have a DbContext project a Model Project a Domain Classes project and Domain Class Mapping project.  I place the views file in the same assembly (project) as the Domain Classes.
Here is an example of the generated view output.  The one on the left was created using a T4 template and "Run Custom Tool"  and the one on the right was created with Entity Framework Power Tools Beta 3 and selecting "Create Views" from context menu while cursor on the EDMX file.
Working one:
SELECT VALUE [xxx.yyy.Provider.DataContext.RouteStop](T1.RouteStop_RouteStopId, ...
FROM (
     SELECT   T1.RouteStopId as RouteStop_RouteStopId, .....
     FROM     CodeFirstDatabase.RouteStop AS T
     ) AS T1

Not working one:
SELECT Value [xxx.yyy.Provider.DataModel.Store.RouteStop](T1.RouteStop_RouteStopId, ...
FROM (
     SELECT   T1.RouteStopId as RouteStop_RouteStopId, .....
     FROM     ZeeZorProviderEdmx.RouteStops AS T
     ) AS T1

The namespace for the project that contains the Domain classes and also contains a copy of the generated views is "xxx.yyy.Provider.DomainClasses" if that helps.
Question 3:  How do I get the Power Tools generated views from EDMX working like the other view files create from T4 Template.
I've been losing hear over this and I don't have much left anyway so I could really use some good advice.  I need a solution and I see one of 3 ways:

Use Entity Framework Power Tools Beta 3 and the "Create Views"
context menu option when DbContext based object file is selected.
Use the T4 template and just grin and bear the timeout issues and
constant failures when building the views.
Use the Model project with EDMX file and use the Power Tools Beta 3
"Create Views" context menu and solve the namespace or view
discovery issue.

Please help me figure out one of these 3 ways and get it working.
Thanks,
Blake
Comment: Got another possible solution after having similar problem while working on another project.  When connecting to **Oracle** using the ODP.NET managed driver, I did not have a reference to that Oracle DLL (Oracle.ManagedDataAccess.dll) in my project containing the context and "Create pre-compiled Views" kept saying "Sequence contains no elements".  I added the reference and the problem went away and views could be compiled.  So it appears, that not only do you have to makes sure you connection string is right but also that you have a reference to the proper 'providerName' in the app.config.
Answer: OK - I've just had this issue when trying to view the View Entity Model from the power tools menu. Sequence contains no elements. Looks like things are still under construction with the Tools Beta 3. The cause is apparently that the power tools cant find the connection string. Anyway, here is a temporary solutions that works.
Add a new Class Library project to your solution. You can remove the default class1 if you want. Now, add a App.config file with a connection string element. (I just copied mine from the real App.config. Then, set this project as the Startup project. Now, when you click on the View Entity Model as readonly menu item, it will display.
I guess the guys are working on this issue. Julie Lerman blogged about it in the link mentioned above.
Hope this solves some of your issues.
Answer: Have you checked your connection string?
This article should help explain your problem:
http://thedatafarm.com/blog/data-access/entity-framework-power-tool-tips-to-view-model-a-feature-i-depend-on
Comment: Yes, the connection string was fine even though that was a good thing to check based on the link you sent.
Comment: I'm still investigating your response along with the Greg's above.  In the meantime, what I've been able to to is run the T4 script over and over again until it finally successfully works (option 2).  I've also done some other reading and from what I'm hearing to make option 2 work for the long term it would probably result in me breaking up the T4 script and chaining the steps together somehow.  I'll update on the connection string issue as soon as I can prove it one way or the other.
Comment: Is the connection string in a project that's within a project folder? By this I mean that you've added a folder into your solution to organise the projects.
Comment: I want to mark Karl's response as an answer because of a small thing he mentions that is so important.  The key is the connection string is found in the project that is marked as "the startup" project.  If you are like me, you have many and sometimes you change the startup project.
Answer: Had the same problem, but different cause.
Context: EF was used with Oracle database
One of the columns was mapped to CLOB column type, which EF Power Tolls didn't like, although when working with database EF was able to use it no problem.
Property(l => l.MyField).HasColumnName("MY_FIELD").HasColumnType("CLOB");


How to access the OSM API

Question: I am making an app where I need access to the maxspeed in the OSM database. I found the api http://overpass-api.de/api/ in the OSM wiki http://wiki.openstreetmap.org/wiki/Overpass_API but I can't find a way to access it and when I search the api online it reaturns the Forbidden error.
How can I program my app to access the api so I can query the database json to find the maxspeed for my android app.
Comment: There are several existing Android apps serving exactly the same purpose, as one example: https://github.com/plusCubed/velociraptor - I'd recommend to study the source code and come back with more specific questions. **Important point**: Overpass API has a usage policy which you have to adhere to. Also OSM API and Overpass API are two completely different APIs.
Comment: And of course, for testing your queries, I'm always recommending the excellent overpass turbo: https://overpass-turbo.eu
Comment: The endpoint to use would be overpass-api.de/api/interpreter
Comment: http://stackoverflow.com/questions/8194233/open-street-maps-api-for-android
Comment: @AnixPasBesoin, I already have libraries and imported it into my app. I need access to their database. The http address is the api according to the wikipedia page but when I click it, it gives me the access denied message. I don't understand why if it's supposed to be open sourse
Comment: @mmd  I actually downloaded the app a few weeks ago and analysed the code as thoroughly as I could and used what I learned in my app, but I couldn't find the address for the api used. I tried this from the overpass/osm wiki page, http://overpass-api.de/api/, but it prompts an `Access denied` screen.
Comment: Take a look at osmbonuspack, it provides functions for accessing Overpass API.
Answer: OSMBonusPack (an addon lib to osmdroid) has an OverpassAPIProvider, ready to use, without your "Forbidden" issue.
If it doesn't exactly fit your need, looking at the source code should help you to implement what you want.
EDIT
Using OverpassAPI may not be straightforward to get the "current" maxspeed ...
Following mmd suggestion, here is the piece of code from velociraptor to build their OverpassAPI request string:
private String getOsmQuery(Location location) {
    return "[out:json];" +
            "way(around:15,"
            + location.getLatitude() + ","
            + location.getLongitude() +
            ")" +
            "[\"highway\"];out body geom;";
}

This may help...
Comment: thank you for the Update. I actually went through most of the code in Velociraptor while building the app, and I used this piece of code, and I made a query and an HTTP get function, but the api never gave me the information I needed. Trying to get the right query from beginning to end.

How to access the OSM API


Find the area of $S=\{(x,y)|\rm{\exists ~}\theta,\beta,x=\sin^2{\theta}+\sin{\beta},y=\cos^2{\theta}+\cos{\beta}\}$

Question: Let $S$ be the domain defined by $$S=\{(x,y)|\rm{\exists ~}\theta,\beta,x=\sin^2{\theta}+\sin{\beta},y=\cos^2{\theta}+\cos{\beta}\}$$
find the area of $S$
This is middle school problem,so I think it can be solved without integral methods?
$$(x-\sin^2{\theta})^2+(y-\cos^2{\theta})^2=1$$
Comment: in Latex "exist" -> "\exists"
Answer: $S$ is defined as the set of points $N(x,y)$ such that $\vec{ON}=\vec{OM}+\vec{MN}$.
$M(x=\sin^2{\theta},y=\cos^2{\theta})$ is such that $x+y=1$ with $x,y \geq 0$, thus describes line segment $AB$ where $A(1,0)$ and $B(0,1)$.
For each position of $M$, point $\vec{MN} (\sin{\beta},\cos{\beta})$ describes a circle with radius 1 centered in $M$. The area swept by all these circles hen $M$ moves from $A$ to $B$ generates set $S$  similar to an horserace area, with medium axis $AB$ (see figure). Otherwise said, the union of all these circles generates a limit set like a machining tool would do by "digging" all the area $S$ (what is called a "dilation" of line segment $AB$).
Thus the area  $S$ can be split into 3 parts : two half disks (with radius 1) + a rectangular part DCEF ; this makes a whole disk (area $\pi$) +  rectangle with area equal to diameter $\times$ length(AB) $\  = 2 \times \sqrt{2}$. Grand total :
$$\pi+2\sqrt{2}$$

Remark: In order to have a fully rigorous proof, a "reciprocal" would be needed, but I have tried here to give a description of the solution set and its area that is accessible to a middle school student.
Comment: I have made a figure + tried to have a more thorough presentation.
Answer: From $(x - \sin^2\theta)^2 + (y - \cos^2\theta)^2 = 1$ we get that $S$ is a set of circles with radius $1$ and centers on the points of $(\sin^2\theta, \cos^2\theta)$, i.e. on the segmet from point $(1,0)$ to point $(0,1)$ (see figure below).
So the area of $S$ is sum of areas of two semicircles and area of rectngle with sides lengths $2$ and $\sqrt{2}$:
$$
\frac{1}{2}\pi\cdot 1^2 + 2\cdot\sqrt{2} + \frac{1}{2}\pi\cdot 1^2 = \pi + 2\sqrt{2}.
$$
Comment: maybe this not right
Comment: @geromty Actually you're right, I've thought about it a bit more and realized that we have also subtract area of the middle segment where two circles on the figure intersect. I'll edit my post, thanks.


Flash cs4 local security sandbox

Question: I'm testing a flash script that calls a JavaScript function (both, the swf and the HTML file are local). The flash movie is not allowed to access the HTML file that contains the js-function.
I've learned that I have to put both files into a security sandbox, so I added the path to both files (HTML+swf) to a file test.cfg in C:\WINDOWS\system32\Macromed\Flash\FlashPlayerTrust.
But still the same problem. What to do?
thanks!
Answer: The requirement for calling from Flash to JS is that you have the allowScriptAccess parmeter set in your embedding code of your HTML document. Iirc, you can specify always or sameDomain and it will work. The second option obviously require the swf to be coming from the same domain.

Flash cs4 local security sandbox


Existence of $L^{2}(\mathbb{R})$ function satisfying retarded argument equation

Question: This question arose when I tried to discuss the spectrum of a special $L^{2}(\mathbb{R})$-operator. Let $g(x) = e^{-x}$. For which $\lambda \in \mathbb{C}$ is there a function $f \in L^{2}(\mathbb{R})$ satisfying
$$g(x)f(x-2) = \lambda f(x)$$
almost everywhere?
Comment: Presumably, you're hoping for a non-zero function $f$?
Answer: If $\lambda=0$ then clearly $f$ must be zero, so let us assume $\lambda\neq 0$.  Then given any function $f_0:[0,2)\to \mathbb{C}$, there is a unique function $f:\mathbb{R}\to\mathbb{C}$ extending $f_0$ and satisfying the functional equation.  Let us take $f_0$ to be the constant function $1$, and let $f$ be the unique extension satisfying the functional equation.
We can rewrite the functional equation as $f(x+2)=\lambda^{-1}e^{-x-2}f(x)$.  In particular, when $x>0$, by induction we find that $\log |f(x)|=\Theta(-x^2)$ (for $x\in[2n,2n+2)$, $\log |f(x)|$ is roughly $-2-4-6-\dots-2n\approx -n^2$ plus terms that are linear in $n$).  In particular, this means $f$ is square-integrable as $x\to +\infty$.
On the other hand, for $x<0$, we use the functional equation in the form $f(x-2)=\lambda e^xf(x)$.  Again by induction, we find that $\log|f(x)|=\Theta(-x^2)$ (for $x\in [-2n,-2n+2)$ we again get a sum like $-2-4-6-\dots-2n\approx -n^2$ plus linear terms).  So $f$ is also square-integrable as $x\to-\infty$.
Thus we conclude that $f\in L^2(\mathbb{R})$.  So a nonzero $f\in L^2(\mathbb{R})$ satisfying the functional equation exists for all $\lambda\neq 0$.


A matrix with characteristic polynomial that can bewritten as product of linear factors is similar to an upper triangular matrix

Question: Prove that if $A\in M_{n}\left(\mathbb{F}\right)$ matrix with a characteristic polynomial that can be written as a product $\left(x-\lambda_{1}\right)^{r_1}\dots\left(x-\lambda_{k}\right)^{r_k}$ of linear polynomials, then $A$ is similar to an upper triangular matrix.
My attempt:
Well first I showed that if $\lambda$ is an eigenvalue of $A$ then it can be written as
$P^{-1}AP=\left(\begin{matrix}\lambda & * & \cdots & *\\
0 & \ulcorner &  & \urcorner\\
\vdots &  & B\\
0 & \llcorner &  & \lrcorner
\end{matrix}\right)$
for some invertible $P \in M_{n}\left(\mathbb{F}\right)$.
Then by induction on n I showed that for a $2\times 2$ matrix the claim holds using the above lemma.
I've assumed correctness for some $n$.
And then I tried proving it for $n+1$:
I used the lemma for the $\left(n+1\right) \times \left(n+1\right)$ matrix (because we assume that its characteristic polynomial can be reduced).
Then I tried using the induction hypothesis on the inner $B$ of the $\left(n+1\right) \times \left(n+1\right)$ matrix. So I've concluded that there's some matrix $Q\in M_{n}\left(\mathbb{F}\right)$ so that $Q^{-1}BQ$ is upper triangular.
The next step was to use it for my current matrix so I've decided to cunstruct the following matrices:
$ Q^{'}=\left(\begin{matrix}1 & 0 & \cdots & 0\\
0 & \ulcorner &  & \urcorner\\
\vdots &  & Q\\
0 & \llcorner &  & \lrcorner
\end{matrix}\right) $ and $ Q^{'-1}=\left(\begin{matrix}1 & 0 & \cdots & 0\\
0 & \ulcorner &  & \urcorner\\
\vdots &  & Q^{-1}\\
0 & \llcorner &  & \lrcorner
\end{matrix}\right) $
So that now I can claim that
$Q^{'-1}\left(\begin{matrix}\lambda & * & \cdots & *\\
0 & \ulcorner &  & \urcorner\\
\vdots &  & B\\
0 & \llcorner &  & \lrcorner
\end{matrix}\right)Q^{'} = \left(\begin{matrix}\lambda & * & \cdots & *\\
0 & \ulcorner &  & \urcorner\\
\vdots &  & Q^{-1}BQ\\
0 & \llcorner &  & \lrcorner
\end{matrix}\right) $
which is essentially an upper triangular matrix.
But I'm having hard time determining whether it's correct at all because to be honest a lot of indices confuse me :) and I would really like to know whether my proof is correct and is there a more solid\clear\intuitive way to explain the last conclusion.
Edit 1: I'll try to make my question clearer: We are usually required to be extremely rigorous, so the $Q^{'-1} P^{-1} A P Q^{'}$ part needs to be explained as well but I can't seem to find a better explanation then something like "well it has identity in the upper left corner so that's what happens" but I can't figure a nice way of writing this mathematically without babbling too much.
Comment: It looks good and crystal clear to me.
Comment: @user1551 thank you, I've tried to clarify my question.
Answer: Just so that this question does not remain unanswered, I'll paraphrase the proof given in the question in a less technical way.
A matrix $A$ is similar to an upper triangular matrix if and only if for the linear operator $\phi$ of $V=F^n$ defined by $A$ there exist a chain of $\phi$-stable subspaces
$$
  \{0\}=V_0\subset V_1\subset V_2\subset\cdots\subset V_n=V
$$
where $\dim(V_i)=i$ for all $i$. This is so because if $\def\B{\mathcal B}\B=[b_1,\ldots,b_n]$ is any basis, then $\operatorname{Mat}_\B(\phi)$ is upper triangular if and only if each of the subspaces $\langle b_1,\ldots,b_d\rangle$ is $\phi$-stable, for $d=1,2,\ldots,n$.
Now a proof that if the characteristic polynomial $\chi_A$ splits into a product of linear factors $X-\lambda_i$, then $A$ is similar to an upper triangular matrix (by the way, the converse is obvious). Proceed by induction on $n=\dim V$, the case $n=0$ being obvious. Now let $n>0$, and let $X-\lambda$ be one of the factors of $\chi_A$, then there is some eigenvector $v$ of$~A$ for $\lambda$. Put $V_1=\langle v\rangle$, then since $V_1$ is $\phi$-stable, $\phi$ induces a linear operator $\overline\phi$ of the quotient space $V/V_1$, which is of dimension$~n-1$. The characteristic polynomial of $\overline\phi$ is $\chi_A/(X-\lambda)$, so it also splits into a product of linear factors. Then by induction there is a chain of $\overline\phi$-stable subspaces
$$
  V_1/V_1=W_0\subset W_1\subset W_2\subset\cdots\subset W_{n-1}=V/V_1
$$
with $\dim W_i=i$ for all $i$. Being a subspace of the quotient space
we can write $W_i=V_{i+1}/V_1$ for a uniquely defined subspace $V_{i+1}$ of$~V$, which has dimension $i+1$, and is $\phi$-stable (because $W_i$ is $\overline\phi$-stable). Together with $V_0=\{0\}$ this provides our chain
of subspaces that shows that $A$ is similar to an upper triangular matrix, completing the proof.


Can I delete nuget directory on azure app service

Question: I am running a 5 year old ASP.Net Core application on an Azure App Service on the D1 (Shared) app service plan. I have taken care of regular framework and package updates and have recently tried to update to .Net 6.  Everything works fine on the local machine but when trying to deploy through Azure Repos, I get the message:
<code>Repository 'UpdateSiteSourceControl' operation failed with System.Net.WebException: There is not enough space on the disk.
</code>
Looking at my disk usage I see I am indeed using almost all of the allotted 1 GB.  Investigating with SFTP, I see the .nuget directory has 764 MB in 3,354 directories...
So this is clearly bloated and the result of incremental upgrades over 5 years.
My question is if I can safely delete this directory and let the build process pull whatever it needs for compilation and build? Or is there some other process that needs to be used?
Answer: Well, I went ahead and deleted the .nuget directory, and was then able to run a successful deployment.  Interestingly, the new .nuget directory is still large - ~452 MB.


Images from my WP blog doesn't show in other sites

Question: why does the images from my site http://allbusters.in doesn't load in other sites like http://jsfiddle.net or http://jsbin.com
Here is the JSBin Link to the check. it shows an image from another site which loads perfectly
http://jsbin.com/afahec
Comment: Why are you doing ``? `` has no closing tag.
Comment: Do you have any Wordpress extensions installed... like [Hotlink Prevention](http://wordpress.org/extend/plugins/wordpress-automatic-image-hotlink-protection/), for example?
Comment: it doesn't matter. but why isn't it loading. is it some wp hack?
Comment: @wylie no it's a default installation.
Answer: There is no such thing as .
Remove it and properly close the <img tag with a />, then your image appears:

http://jsbin.com/otayuy/
Why?
Because non-compliant code is unpredictable.
Comment: @Shahal Tharique:  It's pretty simple.  Compliant code works as expected.  Non-compliant code is non-functional or unpredictable.
Comment: then pls explain why this happening: [jsfiddle link](http://jsfiddle.net/P48Xe/). Here the image is not shown??
Comment: Because your `` was empty (zero size).  [I added some text and this is working.](http://jsfiddle.net/P48Xe/2/)
Comment: wat?? maybe try again or check in different browser?
Comment: @Shahal Tharique:  You should setup a test page someplace else as I think you might be seeing something odd with JSFiddle.  Your [image link](http://allbusters.in/wp-content/uploads/2011/07/facebook_32.png) is not broken and [I'm not seeing the problem](http://jsfiddle.net/BbkBf/5/) you describe.


Solving a PDE possibly with method of characteristics or other methods

Question: For a PDE $$(x-y^{2}) u_{x} + u_{y} = 0$$ I've tried to use method of characteristics. But I've failed to do so. It was because of the term $x-y^{2}$; I don't know how to integrate this on the characteristic line. Should I try another method than method of chracteristic? Or is there a clever trick for this?
Related equatons:
$dx/ds = x-y^{2}$, $dy/ds = 1$, $du/ds = 0$.
Comment: If you integrate the $dy/ds=1$ and obtain $y=s$ then $dx/ds = x-s^2$ which is integrable by partial fractions.
Comment: ah I think it becomes $dx/ds = x-s^2$...
Comment: strike the partial fractions.
Comment: hm.. I'm not sure how that really works.. since $s$ is not a constant
Comment: Integrating factor method. Look at it as $dx/ds-x=-s^2$.
Comment: Okay I know how to do integrating factor method using exponential. Thanks.
Comment: @JamesS.Cook Ah.. May I ask you one more question? The guy below assumed $y =s$ but shouldn't it be $y = s + y_{0}$? If I use the latter, the calculation becomes much complex. How should I resolve it?
Comment: $y=s+y_0$ gives you exactly the same curves, just shifted in $s$.
Answer: Solving your "related equations" should give you the solutions: $u$ is constant on the curves $y = s$, $x = 2+2 s+s^2+ c \exp(s)$, i.e. $u = F((x - 2 - 2 y - y^2) \exp(-y))$.
Comment: Thanks for your answer. But how did you get the $x$ part?
Comment: Ah.. just one short question. Is it okay to use $y=s$ rather than $y=y_{0} +s$ because if I use the latter, the problem gets much harder...


How does cut with breaks work in R

Question: I am trying to understand how cut divides and creates intervals; tried ?cut but can't be able to figure out how cut in r works.
Here is my problem:
set.seed(111)
data1 <- seq(1,10, by=1)
data1
[1]  1  2  3  4  5  6  7  8  9 10
data1cut<- cut(data1, breaks = c(0,1,2,3,5,7,8,10), labels = FALSE)
data1cut
[1] 1 2 3 4 4 5 5 6 7 7

1. Why did 8,9,10 not included in data1cut result?
2. why did summary(data1) and summary(data1cut) produces different result?
summary(data1)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
1.00    3.25    5.50    5.50    7.75   10.00

summary(data1cut)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
1.00    3.25    4.50    4.40    5.75    7.00

How should i better use cut so that i can create say 4 bins based on the results of summary(data1)?
bin1 [1 -3.25]
bin2 (3.25 -5.50]
bin3 (5.50 -7.75]
bin4 (7.75 -10]

Thank you.
Comment: What cut does is indeed put your data into bins and, for each of your vector values, it gives the "code" of the associated bins. You can do `table(data1, data1cut)` to better understand which value falls in which bin
Comment: no justification needed, they are not the same. You should use `labels=paste0("bin", 1:7)` in the `cut` call, it may make it clearer to you. By the way, there is absolutely no need for a `set.seed` call here
Comment: why do you think some of the values are not included ? what did you expect as the `cut` result ? maybe try `cut` on values that are not the first integers to avoid confusion, and read carefully the paragraph **Value** from `?cut` : *A factor is returned, unless labels = FALSE which results in an integer vector of level codes.*
Comment: may be my understanding of `cut` is very limited. What i expect when i do a `cut` was a bin in the ranges created and corresponding values(factors) in the bins? So, assumed distribution metric results such as `summary(data1cut)`  and `summary(data1)` be similar?
Comment: Thank you and it is making sense now; but if we do a `boxplot(data1)` and `boxplot(data1cut)`, we get  different quartile  and median visualization. How should one go about justifying the two plots are same (assuming they mean the same thing again)?
Answer: cut in your example splits the vector into the following parts:
0-1 (1); 1-2 (2); 2-3 (3); 3-5 (4); 5-7 (5); 7-8 (6); 8-10 (7)
The numbers in brackets are default labels assigned by cut to each bin, based on the breaks values provided.
cut by default is exclusive of the lower range. If you want to change that then you need to specify it in the include.lowest argument.

You did not assign labels and default argument in this function is FALSE so an integer vector of level codes (in brackets) is used instead.
summary(data1) is a summary of raw data and summary(data1cut) is a summary of your splits.

You can get the split you need using:
data2cut<-
  cut(data1, breaks = c(1, 3.25, 5.50, 7.75, 10),
      labels = c("1-3.25", "3.25-5.50", "5.50-7.75", "7.75-10"),
      include.lowest = TRUE)

The result is the following:
> data2cut

 [1] 1-3.25    1-3.25    1-3.25    3.25-5.50 3.25-5.50 5.50-7.75 5.50-7.75 7.75-10   7.75-10
[10] 7.75-10
Levels: 1-3.25 3.25-5.50 5.50-7.75 7.75-10

I hope it's clear now.
Comment: bin 3-5 holds both `4` and `5`. When you use `data1cut[5]` it will return `4` which is the fourth bin, i.e. bin3-5. Please re-read @Cath 's responses, she explained `cut` very clearly. I think you are confusing the actual values with default bin labels.
Comment: Many thanks, and all except the first line is clear. Why did the bin at 3-5 holds only value `4` and not `5`; 5-7 only `5` but not `6 , 7` (because the upper values are included by default in the bin and assumed bin 8-10 holds `9, 10`; but obviously that is not the case) the same goes for the other bins? Could you elaborate a bit how `cut` splits and returns a factor? Are there any lose of data in the process? Thanks again.

How does cut with breaks work in R


Why $\hat{r}$ and $\hat{\theta}$ of a vector that is sitting on the z-axis corresponds to $\hat{z}$ and $\hat{x}$ respectively?

Question:
In the picture, the components of the vector is $\overrightarrow{v}=(vcos\theta)\hat{r}+(vsin\theta)\hat{\theta}$
Someone tipped me that since the vector is sitting on the z-axis
$\hat{r}$ corresponds to $\hat{z}$ and $\hat{\theta}$ corresponds to $\hat{x}$
and I don't get that.
To clarify, the vector is situated $z$ distance from the $z=0$ and it makes an angle $\theta$ with the perpendicular to the plane
Why is that?
I don't intuitively get it.
Answer: In spherical coordinates, $r$ is the length of the vector, $\theta$ is the angle between it and the $z$ axis, and $\phi$ is the angle between the vector's projection into the $xy$-plane and the $x$-axis. The unit vectors point in the direction the coordinate is increasing. So $\hat{r}$ points directly outward, $\theta$ will be tangent to a semicircle from $+\hat{z}$ to $-\hat{z}$, and $\hat{\phi}$ will be tangent to a circle around the $z$-axis.
Now, if $\theta = 0$ and $\phi = 0$, the vector will be parallel to the $z$ axis. Then $\hat{r}$ will be equal to $\hat{z}$, since the direction of increasing $r$ is in the same direction as the vector. $\hat{\theta}$ will be equal to $\hat{x}$, because at that point the semicircle that $\hat{\theta}$ is tangent to is horizontal, and since $\phi = 0$, it is parallel to the $x$-axis. This correspondence only happens when $\theta = 0$. For any other value of $\theta$, $\hat{r}$ and $\hat{\theta}$ will be some other linear combination of $\hat{x}$ and $\hat{z}$ (and $\hat{y}$ if $\phi \ne 0$).
Comment: If $\theta=0$, then $\phi$ can be anything and you would still be on the positive $z$-axis, so $\hat{\theta}$ isn't well-defined there.
Comment: @HansLundmark True, but if you allow for $\phi$ to have a defined value even though $(r,0,\phi)$ describes the same point for all $\phi$, you can still talk about a definite $\hat{\theta}$. In a lot of applications, continuity will imply which value of $\phi$ you should be considering.
Comment: Just as a heads up for future readers. The convention for which angle is measured where isn't uniform and varies text by text and author by author. Some have $\theta$ be the angle in the $xy$-plane and $\varphi$ be the angle from $z$ and vice versa. I think the IEEE standard is $\theta$ from the $z$, but not everyone adheres to it.
Comment: @CameronWilliams Also a good point. In particular, I find that mathematicians tend to have $\phi$ be the polar angle (angle with respect to $z$) while physicists prefer $\theta$. My convention here betrays which side I'm on.


Visualizing differences between alternative clusterings?

Question: Does anyone know of a good way to visualize differences between alternative clusterings?
I know there are some measures which can be used to quantify the similarity between two partitionings, e.g.:

Jaccard Index
Adjusted Rand Index
Weighted Kappa

Each of these can provide me with a single number in the range 0-1 or so (although there may negative values with the later two methods).
This is useful, but doesn't help to build an intuition about how the clusterings differ from one another -- for instance, are the two clusterings grouping elements completely differently? Or are they just dividing things up at difference scales?
For small hierarchical clustering comparisons, there is an R package dendextend which can be used to compare alternative dendrograms.
Are there any other methods that can be used that aren't specific to hierarchical clustering, and would work well for larger datasets? (e.g. 10,000 elements).
Answer: We have two publications in this domain, but I am not entirely convinced in the approaches. But you should have a look.
In the publication below, we explored a circular layout to visualize the overlap of clusters. You could click segments to see them colored in a scatterplot (but of course scatterplots do not scale to high-dimensional data).
This allows to see how clusters related to each other.

The circular layout is beneficial, because the complexity of the visualization is higher on the outer circle, where we have more space. A "linear" display is less readable. The size of the segments is based on pairs like many of the evaluation indexes (e.g. ARI). In above image, the red and yellow clusters are one cluster in the outer area, which causes additional pairs (the bottom segment, which does not exist in the inner circle - because these are two pairs there, they do not "pair"). Understanding how the "pairs" work is pretty non-intuitive, sorry.
This approach can use more than two clusterings, but usually at three clusterings it already becomes rather incomprehensible.

E. Achtert, S. Goldhofer, H.-P. Kriegel, E. Schubert, A. Zimek
Evaluation of Clusterings – Metrics and Visual Support
  In Proceedings of the 28th International Conference on Data Engineering (ICDE), Washington, DC: 1285–1288, 2012.

The code in ELKI is expected to work, but I have not verified this in a looong time.
The second reference I can suggest is this:

E. Schubert, A. Koos, T. Emrich, A. Züfle, K. A. Schmid, A. Zimek
A Framework for Clustering Uncertain Data
  Proceedings of the VLDB Endowment, 8(12): 1976–1979, 2015.

While this is titled "Clustering Uncertain Data", it is very closely connected to alternative clustering. Because uncertain data can be clustered by looking at alternatives obtained when taking "certain" samples from the uncertain data, and then running a traditional clustering algorithm.

The tau value shows how similar the clusterings in each cluster are. The confidence probability is an estimate how many clustering results will look similar to the representative. The numbers may not add up to 1 - in this screenshot, we expect some 7% of clusterings to be dissimilar from the examples that we show here.
With uncertain data, you need to look at many samples. But then you have 50-100 (or even many more) clusterings. Thus, we cluster clusterings based on their similarity, to get some representative clusterings the user can then explore. Again, this functionality is available in ELKI, but you need to set some 30-40 parameters to reproduce these results (there is no "standard format" for uncertain data, many of these parameters are to specify the uncertainty that is associated with the data points).
Details on representative clusterings are here:

A. Züfle, T. Emrich, K. A. Schmid, N. Mamoulis, A. Zimek, M. Renz
Representative Clustering of Uncertain Data
  In Proceedings of the 20th ACM International Conference on Knowledge Discovery and Data Mining (SIGKDD), New York, NY: 243–252, 2014.
Comment: Thanks for sharing these methods. I haven't come across either of papers before and will definitely take a look. The main limitation in my case is probably the scale of that data - with ~10,000 datapoints and several hundred clusters, the methods may not work too well. One possibility would be to partition the original data into smaller "robust" partitions, and then visualize the differences within each of these (assuming they exist). Not ideal, but it may help make the probably more manageable, at least at the scale of my problem.


Drupal 7 Trying to create a "Today in History" view

Question: I'm sure there's an easy way to do the following but I've not come up with a safe Drupal way to do so:
My current front page has a view which generates a list of the most recent three articles on my site using the date stored in  field_published attached to each article. The SQL which the view generates looks like this:
SELECT field_data_field_published.field_published_value AS field_data_field_published_field_published_value, node.nid AS nid FROM node node LEFT JOIN field_data_field_published field_data_field_published ON node.nid = field_data_field_published.entity_id AND (field_data_field_published.entity_type = 'node' AND field_data_field_published.deleted = '0') WHERE (( (node.status = '1') )) ORDER BY field_data_field_published_field_published_value DESC LIMIT 3 OFFSET 0;
( You can see what this looks like live on my website )
Running the query on mysql CLI generates this:
+--------------------------------------------------+-----+
| field_data_field_published_field_published_value | nid |
+--------------------------------------------------+-----+
| 2017-01-23 00:00:00                              | 427 |
| 2017-01-01 00:00:00                              | 426 |
| 2016-08-07 00:00:00                              | 424 |
+--------------------------------------------------+-----+
I want to now create a similar view that searches only the month and day field of every article in the DB, matches the month and day against 'today', and generates a list of article nid. The SQL for this is:
SELECT field_data_field_published.field_published_value AS field_data_field_published_field_published_value, node.nid AS nid FROM node node LEFT JOIN field_data_field_published field_data_field_published ON node.nid = field_data_field_published.entity_id AND field_data_field_published.entity_type = 'node' WHERE (( (field_data_field_published.field_published_value LIKE (select date_format(curdate(),'____-%m-%d%'))) )) AND (( (node.status = '1') ))
ORDER BY field_data_field_published_field_published_value DESC LIMIT 3 OFFSET 0;
This correctly generates a list of article nid on the CLI:
+--------------------------------------------------+-----+
| field_data_field_published_field_published_value | nid |
+--------------------------------------------------+-----+
| 2015-05-24 00:00:00                              | 369 |
| 1999-05-24 00:00:00                              | 222 |
| 1997-05-24 00:00:00                              | 150 |
+--------------------------------------------------+-----+
My question is, how do I get my functional SQL statement into a view?
I have tried using Views Contextual Filters and Granularity, but that does not allow me to omit the year when filtering dates.
I have tried to use hook views_pre_execute in a module but it barfs on my query: Something like
<?php
// TMG 3-21-09
// call to hook_views_pre_execute to override the query for the groups browser
function views_views_pre_execute(&$view) {
   if($view->name=="future_front_page2") {
       $kp_tih = date('____-m-d%');
       $view->build_info['query']="SELECT field_data_field_published.field_pub
lished_value AS field_data_field_published_field_published_value, node.nid AS ni
d FROM node node LEFT JOIN field_data_field_published field_data_field_published
 ON node.nid = field_data_field_published.entity_id AND field_data_field_publish
ed.entity_type = 'node' WHERE (( (field_data_field_published.field_published_val
ue LIKE (select date_format(curdate(),'____-%m-%d%'))) )) AND (( (node.status =
'1') )) ORDER BY field_data_field_published_field_published_value DESC LIMIT 3 O
FFSET 0";
       $query = db_select('node','n')->addTag('node_access');
       drupal_set_message(t($query));
Any suggestions?
Thanks in advance for any advice.
Answer: Try to use hook_views_query_alter.
Even though, I think you can do it using only views without altering the query.
Maybe using Date Views module would help.
Comment: I started off trying hook_views_query_alter but it similarly rejected the date calc in the SQL. Unable to find the Date Views module. Pointer?
Comment: I'm not sure if you need the select at 'select date_format'. A similar query is : (DATE_FORMAT(ADDTIME(FROM_UNIXTIME(my_field.created), SEC_TO_TIME(3600)), '%Y-%m-%d') = '2017-04-04'). If you want to add this module try to enable it using drush. drush en date_views -y


1-to-1 Proactive messaging using Bot Framework 4

Question: I am using C# (.NET Core) with Bot Framework v4 to develop a bot service. I am a beginner to Bot Framework.
The problem statement goes as follows :

I want to be able to Store some questions on o365 SharePoint
subscription in a list. The data structure of the list will have a
column containing users who are the intended audience for the
question.
When a question is added to the list, I want my bot to send out the
questions to intended users on Microsoft Teams.
Even if the user is not online to teams, I would want to send out a
notification to the user on Teams chat, something like "Missed
conversation".

Now, I understand, on a very beginner level, that the key here is Proactive messaging using the Bot. What I can't figure out or find on any forum articles is that how do I write my bot to go to SharePoint (could be using polling or event driven) and fetch information and then send out the fetched data to Users on personal 1-to-1 team chat?
What I have done so far is :

I have written my SharePoint business logic to fetch information.
The bot calls this business logic when it receives an activity from
User. i.e. say, a user says "Hi" and the bot goes and fetches
information from SharePoint and returns it to the user in the chat
window.

Any help would be greatly appreciated.
Cheers,
Yogesh
Comment: I think you should have made SharePoint the subject of your question, not the Bot Framework. You say "you can't figure out how to write my bot to go to SharePoint (could be using polling or event driven) and fetch information". Your current question subject will likely attract Bot Framework specialists, not those with SharePoint experience
Comment: Also, Bot Framework on .net is a regular .net web app, so you can load data you usually do in a .net web app
Comment: "you can't figure out how to write my bot to go to SharePoint" : Microsoft Graph API ? https://learn.microsoft.com/en-us/graph/api/resources/sharepoint?view=graph-rest-1.0
Comment: I have already figured out the part of fetching data from sharepoint, I want to implement the part where bot pro-actively calls the fetch logic. Rather than on user Activity.
Answer: My guess is that you should be able to accomplish the goals, but make sure to break this up into smaller problems when looking for assistance. You might need some other layers between the two to accomplish as well.
As you have found, proactive messages will be what you want to use from the bot side to message users without them engaging the bot first. You won't find any success (especially in Teams) if the users have never interacted with the bot before as there will need to be some sort of way for the bot to be able to know how to contact them (former conversation, etc). Please find more information here and here.

What I can't figure out or find on any forum articles is that how do I
write my bot to go to SharePoint (could be using polling or event
driven) and fetch information and then send out the fetched data to
Users on personal 1-to-1 team chat?
I have written my SharePoint business logic to fetch information. The
bot calls this business logic when it receives an activity from User.
i.e. say, a user says "Hi" and the bot goes and fetches information
from SharePoint and returns it to the user in the chat window.

So; it seems like the part you are missing is an intermediate layer that does polling or such. The proactive sample, is just using turn events to then 'proactively' message the user back. You might have to get creative. Have some sort of other service/timerjob/serverless function that might be able to assist.
This is an article for v3, but the general idea about proactive messages is good and helps understand how to best approach proactive message paradigm and ensure a good user experience:
https://learn.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-proactive-messages?view=azure-bot-service-3.0#send-a-dialog-based-proactive-message
Comment: Thanks @Dana V, I will look into the source links you provided and try and implement pro-active messaging using Framework 4.
Apologies for flooding too many questions altogether in a single post. I will try and break down the problem statement next time I post.
Comment: that suggestion to break-down a larger problem statement to smaller bits actually improved the implementation and bring it in line with SOLID principles.
Thanks a ton!
Answer: I was able to achieve the above mentioned tasks. For starters I was thinking too much and the solution was simple.
I wrote an infinite loop with asynchronous call to SharePoint at regular intervals using simple Task.Delay(milliseconds) and push the questions proactively when found.
As for 1-to-1 messaging, I am using Teams extension for Bot Framework to store individual channel information and send messages on private chat to users as and when there is something to send based on polling functionality above.
Hope that clears the air for someone, who might be looking to achieve anything similar.
Cheers,
Y
Comment: "wrote an infinite loop with asynchronous call to SharePoint at regular intervals using simple Task.Delay(milliseconds) and push the questions proactively when found."

-- As long as that doesn't negatively impact your application resources, then I'm glad it works!
Comment: For anyone, still looking for a way to do this. User Microsoft Flows for triggering proactive messaging. Though the infinite loop method is just fine, but it could hugely impact your web server resources if polling is not judiciously configured.


Is the a way to internally monitor the transition to the first state in an Akka FSM?

Question: Considering an FSM actor that starts at state Idle
startWith(Idle, IdleData)

I'd like to monitor the transition to this first state (from no state?)
I tried
onTransition {
    case _ -> Idle => // Wasn't called
}

According to the relevant FSM documentation:

It is also possible to pass a function object accepting two states to
  onTransition, in case your transition handling logic is implemented as
  a method:

onTransition(handler _)

def handler(from: StateType, to: StateType) {
  // handle it here ...
}

Given that the from type is StateType and not Option[StateType] I think that it may not be possible, but maybe I'm missing something.
Answer: I was searching for something similar recently.
If I understand your question correctly, Here is one way to monitor when you start off your FSM from the initial state:
class ExampleFSM extends Actor with FSM[State, Data] {
  // A FSM begins its existence in Idle state and can move to different states
  startWith(Idle, NoData)

  when(Idle) {
    case Event(SetData(something), NoData) =>
      goto(SomeOtherState) using Data(something)
  }

  onTransition {
    case Idle -> Idle =>
      stateData match {
        case _ =>
          println("Initial transition")
      }
  }
}

In the above example, When We instantiate the FSM pass-in the message SetData(something), It starts at the state Idle and there is a transition that you can monitor which is Idle -> Idle.
In the above case when we start the FSM we can see an output Initial transition printed, you can leverage this to do more complex stuff as per your needs
So basically if you update your existing transition from case _ => Idle to case Idle -> Idle it should work
Note: There might be more than one way to do this, I am also still exploring the power of Akka FSM so my answer is just having one possible way of getting this.


What is the Message Exchange Patterns?

Question: Can someone please explain what the MEP is? I can't grasp the concept.
From what I figure, is just a concept for two parties to understand each other (i.e. agree on a type of format that the messages they exchange between them must have).
Is this it, or is there more?
Comment: http://en.wikipedia.org/wiki/Message_Exchange_Pattern
Comment: @org.life.java: I have already read that
Answer: In the context of SOAP, it is used to define the behavior of an operation; whether it takes an input, returns an output, and how exceptions are handled. This is defined in the wsdl. See the pattern attribute of the operation element in the example wsdl snippet below (taken from Wikipedia):
<code><interface name="RESTfulInterface">
  <operation name="Get" pattern="http://www.w3.org/ns/wsdl/in-out">
    <input messageLabel="In" element="tns:request"/>
    <output messageLabel="Out" element="tns:response"/>
  </operation>
</interface>
</code>
In this service, the operation takes an input and returns and output (in-out).
Comment: No, this is the MEP definition of WSDL. It differs from the MEP definition of SOAP. There are two SOAP MEPs standardized (request-response and response), they basically provide the superset of interactions that have to be supported by underlying transports.

What is the Message Exchange Patterns?