Nested JSON random name objects parse with Gson

Question: im looking to a simple way to parse this JSON into a Gson java class for easily way to put him into a adapter and fill some listviews the problem is the part of items,i dont know the items objects names, as you can see i have random objects names inside items, is possible do to this with GSON or i need another code logic, this is for Android, thanks for your help :
[{
    "id": 1001,
    "name": "Super1",
    "user": {
        "name": "The Super 1"
    },
    "items": {
        "987987M7812b163eryrt": {
            "id": 1,
            "strong": 456,
            "active": true,
            "sell": "te"
        },
        "90812bn120893juuh": {
            "id": 2,
            "strong": 4700,
            "active": true,
            "sell": "tt"
        },
        "981273jn19203nj123rg": {
            "id": 3,
            "strong": 3000,
            "active": true,
            "sell": "ti"
        }
    }
}]

Add the code of GSON format to JAVA Class:
import com.google.gson.annotations.SerializedName;
public class test_de {

/**
 * id : 1001
 * name : Super1
 * user : {"name":"The Super 1"}
 * items : {"987987M7812b163eryrt":{"id":1,"strong":456,"active":true,"sell":"te"},"90812bn120893juuh":{"id":2,"strong":4700,"active":true,"sell":"tt"},"981273jn19203nj123rg":{"id":3,"strong":3000,"active":true,"sell":"ti"}}
 */

private int id;
private String name;
private UserBean user;
private ItemsBean items;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public UserBean getUser() {
    return user;
}

public void setUser(UserBean user) {
    this.user = user;
}

public ItemsBean getItems() {
    return items;
}

public void setItems(ItemsBean items) {
    this.items = items;
}

public static class UserBean {
    /**
     * name : The Super 1
     */

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public static class ItemsBean {
    /**
     * 987987M7812b163eryrt : {"id":1,"strong":456,"active":true,"sell":"te"}
     * 90812bn120893juuh : {"id":2,"strong":4700,"active":true,"sell":"tt"}
     * 981273jn19203nj123rg : {"id":3,"strong":3000,"active":true,"sell":"ti"}
     */

    @SerializedName("987987M7812b163eryrt")
    private _$987987M7812b163eryrtBean _$987987M7812b163eryrt;
    @SerializedName("90812bn120893juuh")
    private _$90812bn120893juuhBean _$90812bn120893juuh;
    @SerializedName("981273jn19203nj123rg")
    private _$981273jn19203nj123rgBean _$981273jn19203nj123rg;

    public _$987987M7812b163eryrtBean get_$987987M7812b163eryrt() {
        return _$987987M7812b163eryrt;
    }

    public void set_$987987M7812b163eryrt(_$987987M7812b163eryrtBean _$987987M7812b163eryrt) {
        this._$987987M7812b163eryrt = _$987987M7812b163eryrt;
    }

    public _$90812bn120893juuhBean get_$90812bn120893juuh() {
        return _$90812bn120893juuh;
    }

    public void set_$90812bn120893juuh(_$90812bn120893juuhBean _$90812bn120893juuh) {
        this._$90812bn120893juuh = _$90812bn120893juuh;
    }

    public _$981273jn19203nj123rgBean get_$981273jn19203nj123rg() {
        return _$981273jn19203nj123rg;
    }

    public void set_$981273jn19203nj123rg(_$981273jn19203nj123rgBean _$981273jn19203nj123rg) {
        this._$981273jn19203nj123rg = _$981273jn19203nj123rg;
    }

    public static class _$987987M7812b163eryrtBean {
        /**
         * id : 1
         * strong : 456
         * active : true
         * sell : te
         */

        private int id;
        private int strong;
        private boolean active;
        private String sell;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getStrong() {
            return strong;
        }

        public void setStrong(int strong) {
            this.strong = strong;
        }

        public boolean isActive() {
            return active;
        }

        public void setActive(boolean active) {
            this.active = active;
        }

        public String getSell() {
            return sell;
        }

        public void setSell(String sell) {
            this.sell = sell;
        }
    }

    public static class _$90812bn120893juuhBean {
        /**
         * id : 2
         * strong : 4700
         * active : true
         * sell : tt
         */

        private int id;
        private int strong;
        private boolean active;
        private String sell;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getStrong() {
            return strong;
        }

        public void setStrong(int strong) {
            this.strong = strong;
        }

        public boolean isActive() {
            return active;
        }

        public void setActive(boolean active) {
            this.active = active;
        }

        public String getSell() {
            return sell;
        }

        public void setSell(String sell) {
            this.sell = sell;
        }
    }

    public static class _$981273jn19203nj123rgBean {
        /**
         * id : 3
         * strong : 3000
         * active : true
         * sell : ti
         */

        private int id;
        private int strong;
        private boolean active;
        private String sell;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getStrong() {
            return strong;
        }

        public void setStrong(int strong) {
            this.strong = strong;
        }

        public boolean isActive() {
            return active;
        }

        public void setActive(boolean active) {
            this.active = active;
        }

        public String getSell() {
            return sell;
        }

        public void setSell(String sell) {
            this.sell = sell;
        }
    }
}}

Comment: try looking into this https://stackoverflow.com/questions/20442265/how-to-decode-json-with-unknown-field-using-gson
Comment: Why does the backend need to send you gibberish keys, rather than them just being an array, if the items themselves already have IDs?
Comment: Thanks @Submersed if with gibberish keys you mean 987987M7812b163eryrt, 90812bn120893juuh and 981273jn19203nj123rg is becouse is another id_numeric_string that the backend have.
Answer: You can use GSON. Just define
@SerializedName("items")
Map<String, Inner> items;

in your outer class where Inner is class like
public class Inner {
    private int id;
    private boolean active;
    private int strong;
    private String sell;
}

Or you can use a custom Gson JsonDeserializer.
Comment: Thanks, i will try this, anticipate i'm looking for something like this, that is, a simple java class that i can be able to call for fill a listview, not matter the quantity and don't matter the objects name. PD: i'm not to acquainted with custom Gson JsonDeserializer.
Comment: Sorry this is actually not working, can't access to values


Internet Explorer (IE) takes over entire screen on server 2012

Question: I have a few Windows 2012 servers that I've been tasked with setting up for my developers.
We interact with the servers using RDP.
One of the servers is behaving *very* strangely. If I start internet explorer by:

Start
Click on IE icon under the start listing

When I do this IE starts but it takes over the *ENTIRE* desktop and has a non-standard appearance -- the address bar is at the bottom and there doesn't seem to be a way to get back to the desktop short of starting the task manager and killing the IE process.
When this happens it looks like the following:

However, I've found that if I start IE by

Start
Search for Internet
Click on the return result

Then IE starts as expected in a window on the desktop in a standard format.
I'm assuming this is some obscure (horrible, IMO) interface that somehow got activated. How do I fix this to behave normally, as a standard desktop window?
Comment: You don't remember Metro?
Comment: Nope. Never knew about metro. What is that. How do I get rid of it. It's horrid.
Answer: Credits to https://superuser.com/a/727606/163596 by ElectronicGeek

Go to the Start Screen, and type in "Internet Options". Go to the Programs tab, and click the checkbox with "Open Internet Explorer tiles on the desktop."
Also, going to Internet Options from the Control Panel works if you can't search.
Answer: Internet Explorer 10, which was the default browser on Windows 8/Windows Server 2012 came with two display modes, the classical desktop UI and the "Metro UI", nowadays called "Modern UI". By default it started Metro UI, which is shown on your screenshot. You have two possibilities to prevent this:

Change the pin on the start screen to the other UI. To do that, search for it (as you already did), then right click on the search result and click on "Pin to Start".

To remove the Metro UI icon, just right click on it and click on "Unpin from Start"

The second option is to upgrade to Internet Explorer 11. It doesn't come with the Modern UI and has only the desktop interface. Actually, it is strange that your system hasn't updated to IE11 yet, IE comes as a Windows Update and was released in 2013(!).
Comment: The pin/unpin thing didn't do anything at all. :(


angle between polynomials

Question: let $v$ be the space of polynomials less than or equal to three and let

$$\langle p,q\rangle =  p(0)q(0)+p'(0)q'(0)+p(1)q(1)+p'(1)q'(1)$$

What is the angle between the polynomials $2x^3-3x^2$ and $1$?
I was thinking $3\pi/4$ or $4\pi/3$. I was not sure which one it was. I tried drawing a picture and thought it might be $\pi/3$ too. Can someone help me please? I am a self learner and worked on this for days. I came to exhaustion. (real analysis)
Comment: What do you mean by "angle"? I'm guessing that you have an inner product, and you're trying to use the fact that the cosine of the angle is related to the inner product; can you please clarify?
Comment: @2929: He is aware of what an inner product is, he is asking you to specify it (as was I).
Comment: @T.Bongers yes that is it. sorry about that
Comment: We need an inner product...
Comment: @2929 Can you clearly specify the inner product?
Comment: it is the generization of a dot product. it can be used in vector spaces.
Comment: A typical one for polynomials is $\langle p,q \rangle = k \int_a^b p(t)q(t) dt$. Assuming this is the case, we need $k,a,b$.
Comment: @2929 Yes, that's what an inner product is in general. What is the particular inner product that you are using *in this problem*?
Comment: Drawing a picture?
Comment: p(0)q(0)+p'(0)q'(0)+p(1)q(1)+p'(1)q'(1)=
Answer: It depends on how you defined angle. If it is in the standard way we define in vector spaces with inner product. Let $V$ be an $\mathbb{R}$ or $\mathbb{C}$ vector space with inner product $\langle,\rangle$ the angle $\angle(u,v)$ between vectors $u,v\in V$ is defined by
$$\angle u,v=\cos^{-1}\left(\dfrac{\langle u,v\rangle}{|u||v|}\right)$$
which is well defined because $|\langle u,v\rangle|\leq |u||v|$. In that case, assuming you are dealing with the space of polynomials $P_3(\mathbb{R})$ with the usual inner product we will proceed just computing this thing. Recall that we define
$$\langle f,g\rangle=\int_0^1f(t)g(t)dt,$$
hence, for $f(x)=2x^3-3x^2$ and $g(x)=1$ we have
$$\langle f,g\rangle = \int_0^1 2t^3-3t^2dt = -\dfrac{1}{2},$$
$$\langle f,f\rangle =\int_0^1 4t^6-12t^5+9t^4dt = \dfrac{13}{35}\Longrightarrow |f|=\sqrt{13}/\sqrt{35},$$
$$\langle g,g\rangle = \int_0^1 dt=1\Longrightarrow |g|=1.$$
Because of that
$$\angle (f,g)=\cos^{-1}\left(-\sqrt{\dfrac{13}{140}}\right)\approx1,88 \ \text{rad}$$
Comment: +1, but an explicit (and very different) inner product was added recently.
Comment: @T.Bongers can you help me solve this?
Answer: Set $p(x) = 2x^3 - 3x^2$ and $q(x) = 1$. Note that $q'(x) = 0$, so we can compute
$$\langle p, q\rangle = p(0) + p(1) = -1$$
Now we use that
$$\cos\theta = \frac{\langle p, q \rangle}{\sqrt{\langle p, p \rangle \langle q, q \rangle}}$$
Now $\langle q, q\rangle = 2$, and $$\langle p, p\rangle = p(0)^2 + p'(0)^2 + p(1)^2 + p'(1)^2$$
Using the fact that $p'(x) = 6x^2 - 6x$, we see that $p'(0) = p'(1) = 0$, so that
$$\langle p, p\rangle = (-1)^2 = 1$$
Thus $$\cos{\theta} = -\frac{1}{\sqrt{2}}$$
The solutions are $\theta = 2n\pi - \frac{3\pi}{4}$.
Comment: would the answer be something like pi/3 in terms of pi?
Comment: @2929 No. The answer would be $2n\pi - 3\pi/4$ for any integer value of $n$ (in particular, $-3\pi/4$ works)
Comment: ahh i see. haha that is what i was thinking in the first place as was my first guess in the problem. thanks!
Answer: Let $p_1(x) = 2x^3-3x^2$, $p_2(x) = 1$. Let $\phi: \mathbb{P}^3 \to \mathbb{R}^4$ be given by $\phi(p) = (p(0), p'(0), p(1), p'(1))$. Then
$\langle p, q \rangle_{\mathbb{P}^3} = \langle \phi(p), \phi(q) \rangle_{\mathbb{R}^4}$, so we see that it is indeed an inner product.
We have $\phi(p_1) = (0,0,-1,0)$, $\phi(p_2) = (1, 0,1,0)$, so we have $\|p_1\|_{\mathbb{P}^3} = 1, \|p_2\|_{\mathbb{P}^3} = \sqrt{2}$, and $\langle p_1, p_2 \rangle_{\mathbb{P}^3} = \langle (0,0,-1,0),  (1,0,1,0) \rangle_{\mathbb{R}^4} = -1$.
The angle is usually defined by $\cos \theta = \frac{\langle p_1, p_2 \rangle_{\mathbb{P}^3}}{\|p_1\|_{\mathbb{P}^3} \|p_2\|_{\mathbb{P}^3}} = -\frac{1}{\sqrt{2}}$. Some solutions are $\pi \pm \frac{\pi}{4}$.
Comment: why can it be 3pi/4? why is the answer pi/4 for this question. can you help me see this better.
Comment: T.Bongers got 3pi/4
Comment: @2929: What would $\pi-\frac{\pi}{4}$ be?
Comment: Are you asking why $\cos (\pi \pm \frac{\pi}{4}) = -\frac{1}{\sqrt{2}}$????

angle between polynomials


How upload docx, pdf file with renaming?

Question: Hi i create a coding to upload docx file and pdf file. Following are the coding for this.
$allowedExts = array("pdf", "docx");//array("mp4", "WebM", "ogg");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["size"] < 90000000000000000000000000000000000) && in_array($extension, $allowedExts)){
    if ($_FILES["file"]["error"] > 0){
        echo "Return Code: " . $_FILES["file"]["error"] . "
";
    }else{
        echo "Upload: " . $_FILES["file"]["name"] . "
";
        echo "Type: " . $_FILES["file"]["type"] . "
";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";
        if (file_exists("upload/" . $_FILES["file"]["name"])){
            echo $_FILES["file"]["name"] . " already exists. ";
        }else{
            move_uploaded_file($_FILES["file"]["tmp_name"],
            "docmument_upload/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "docmument_upload/" . $_FILES["file"]["name"];
        }
    }

But i want rename the file before upload to folder. How can i do this?
Comment: who is 'you'? you as a programmer in the code or you as the user uploading the file? always or only when duplicate is found? anything happens if the new name exists but the old one don't?
Answer: First of all you  get the file extension using
$temp = explode(".", $_FILES["file"]["name"]);
$extension= end($temp);// file extension

Rename your file name as
$new_name=time().".".$extension;// new name with current timestamp
move_uploaded_file($_FILES["file"]["tmp_name"],"docmument_upload/" .  $new_name);

Answer: Just change your line of code
From
move_uploaded_file($_FILES["file"]["tmp_name"],
            "docmument_upload/" . $_FILES["file"]["name"]);

To
$newFileName="newName".".".$extension;
move_uploaded_file($_FILES["file"]["tmp_name"],
            "docmument_upload/" . $newFileName);

$newFileName is the variable where you can create your new file name. Always care for overwriting issue , so try to make file name unique .
$extension is the variable what you are using to validate.
Answer: This will produce the desired results for you:
$ext = end((explode(".", $_FILES["file"]["name"])));
$filename = time(). rand(). "." . $ext;
move_uploaded_file($_FILES["file"]["tmp_name"], "docmument_upload/" . $filename);


Deleting multiple records in laravel using foreach loop

Question: I am trying to delete multiple images using product_id, I am able to delete one image at a time but am figuring out how i can insert a variable like $i=0 to a loop over but it does not work in laravel.
so far this is my code
   public function ImageDelete($slider_id)
{
    //int $i=0; Had introduced this but doesn't work
    $slider = Products::findOrFail($slider_id);
       foreach($slider as $product){
          $product_images=$slider->images()->get();
           $image_path = public_path().'\images2\\'.$product_images[0]->filename;
           File::delete($image_path);
         //$i++;
    }
    $slider->delete();

    return response()->json(['success'=>'Pics deleted successfully!']);
}

Comment: Hi dear friend. try `$slider = Products::where('id',$slider_id)->get();` if you have multiple records with the same value of `id`.
Comment: @Mohammad Thanks the images are in the folder, one product can have multiple images so my problem is deleting all the images of that product in its path.
Comment: Have you tried `foreach` on `$product_images`? Because you have multiple images for one product.  use `foreach` on `$product_images ` and change `$image_path` like this:
`$image_path = public_path().'\images2\\'.$product_image->filename;`
Comment: @MohammadAliyari, If I try that I get an error Undefined variable: product_images but when i put in the foreach loop and add $product_images[0]->filename it works but delete only one image
Answer: The findOrFail method return only one item. So you cannot iterate over it.
You can use the each method
$product = Products::findOrFail($id);
$images = $product->images()->get();

$images->each(function ($file, $key) {
  $filePath = public_path("images2/") . $file->filename;
  File::delete($filePath);
});

// Delete the product
$product->delete();

Comment: Thanks but how to implement this part ($value, $key)
Comment: I updated my answer. Please read the [Laravel documentation](https://laravel.com/docs/8.x/collections#available-methods) regarding collection and their methods.
Answer: Try this:
   public function ImageDelete($slider_id)
{
    $product = Products::find($slider_id);

    $product_images=$product->images()->get();

       foreach($product_images as $product_image){
           $image_path = public_path().'\images2\\'.$product_image->filename;
           File::delete($image_path);
        }
    $product->delete();

    return response()->json(['success'=>'Pics deleted successfully!']);
}


Can one construct a homeomorphism with noncompact periodic point set?

Question: Can one construct a compact metric space $X$, and a homeomorphism $f\in \text{Homeo}(X)$, such that $f$ has noncompact periodic point set?
I am trying to find a homeomorphism on a cylinder $S^1\times [0,1]$, but not succeeded yet. Looking forward to your brilliant answer. Thanks
Here are some of my attemptations
Let $f\in\text{Homeo}(S^1\times [0,1])$, which sends $(x,y,h)$ to $(-x,-y, g(h))$, i.e the antipodal map on the circle, then lift $g(h)$, so it suffices to find a $g:[0,1]\to[0,1]$ which is surjective,continuous  and has dense periodic points (or just fixed points is Okay, I guess).
Because no matter whether $g$ is a homeomorphism, $f$ is a bijective continuous map, thus in this case, a homeomorphism, if the periodic points of $g$ is dense in $[0,1]$, but not the whole interval, we can deduce that the periodic points set of $f$ is not even closed, thus a noncompact one, so now the problem is to construct that peculiar $g$.
Answer: Consider the map $x\to 2 x \mod 1.$ (think of it as a homeomorphism of $S^1$). Then, every rational number with odd denominator is a periodic point.
Comment: nice answer,thank you very much !
Comment: My pleasure....


What is the time complexity of StringBuilder.append() in java?

Question: A program I'm working on converts an array of integers to a string using string builder. I'm trying to determine the time complexity of this approach.
Comment: It's also worth pointing out that you need to define efficiency of one approach as being with respect to another feasible approach. As in, it takes vast amounts of fuel to launch a satellite into orbit, [it's not very efficient](https://space.stackexchange.com/a/17925) (as in power usefully expended); but in the absence of an alternative, the "inefficient" approach is all you can do, so the inefficiency is largely irrelevant. So, what is your alternative to string builder?
Comment: What exactly do you mean by "efficiency"? What is the criterion you are trying to optimize for?
Answer: Check out: https://stackoverflow.com/a/7156703/7294647
Basically, it's not clear what the time complexity is for StringBuilder#append as it depends on its implementation, so you shouldn't have to worry about it.
There might be a more efficient way of approaching your int[]-String conversion depending on what you're actually trying to achieve.
Answer: If the StringBuilder needs to increase its capacity, that involves copying the entire character array to a new array.  You can avoid this by initially setting the capacity so it won't have to do this.  (This should be easy since you know the length of the int array and the maximum number of characters in the String representation of an int.)
If you avoid the need to increase the capacity, the complexity would seem to just be O(n).  When you append, you're just copying the character array from the String to the end of the character array in the StringBuilder.
(Yes, it depends on the implementation, but it would be a rather poor implementation of StringBuilder if it couldn't append in O(n) time.)
Comment: It's most likely even faster than that; it could use something like a `LinkedList` for O(1) appending.
Comment: I'm not sure about that.  If it's O(1) that means you're copying the linked list itself instead of the values - and wouldn't you then run into problems if you wanted to append something to the end?  It would change the original.


turn video frames to streaming video

Question: The server is sending video frame. I would like to use them in order to do a streaming. I wonder how I could assemble the frames to create a streaming video. So far, I could display the frames as pictures. Below is my angular code
component angular
 getVideo() {
    interval(250).switchMap(() => this.appService.getPictures())
      .subscribe(data => {
        const file = new Blob([data], {type:'image/png'});
        this.url = this.sanitizer.bypassSecurityTrustResourceUrl(URL.createObjectURL(file));
      })
  }

template html

I am trying to change the picture using the frame rate of the camera. But my picture is not updated and it freezes my browser due to the high number of http requests.
What I want to achieve is to buffer the frame in order to use the video tag instead of the img tag the same way I would connect to a live streaming send by a server using the video tag with src set to the url of the server.
Github link: https://github.com/kedevked/frameProcessing
Comment: Because, the server is sending images, not video. `` will not display anything
Comment: Using another server for processing is not an option I could consider
Comment: why not just render video as well? instead of return frame image?
Comment: I don't think it'll work that way. It'll be slow and consume a lot of bandwith.  Maybe try lowering framerate. Is having another server reading these frames and convert tem back to a video an option?
Comment: @edkeveked, interesting. Can you provide api  return  image or better sample project to github?
Comment: I updated the question with the github link
Comment: A video from picture frames is basically a gif which are tens or hundreds of times larger in file size than a compressed video. Video files are not simply a collection of picture frames.
Comment: @Mongoose, actually I could use frames to recreate the original video server side for instance by using ffmpeg. Now my question is about how to do the same process client side.
Comment: Why don't you use canvas?
Comment: @Tschallacka, could you please provide a code where the canvas can be used as a buffer for the videos?
Comment: http://techslides.com/demos/image-video/create.html
Comment: Thanks @Tschallacka, but I have already seen the website. On the [github](https://github.com/muaz-khan/WebRTC-Experiment/tree/master/ffmpeg), the sources are not showing how to convert images to have a video.
Comment: Have you like.. right clicked and then view source? from line 92 ish?
Comment: Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167262/discussion-between-edkeveked-and-tschallacka).
Answer: Instead of triggering http request at a certain interval of times, using websocket is better.
By displaying the images frame by frame, it gives the impression of a live video. The only thing though is that there might be latency in the network which makes the frame rate not to be constant. However, it works well for use cases where buffering the video server side could not be considered such as when we need to send data along each frame.
service using websocket
createStream(url) {
  this.ws = webSocket<any>(url);
  return return this.ws.asObservable()
}

component
constructor (private streamingService: StreamingService) {}
ngOnInit(): void {
  this.getStream()
}

getStream() {
  this.streamingService.createStream().subscribe(data => {
    const file = new Blob([data], {type:'image/png'});
        this.url = this.sanitizer.bypassSecurityTrustResourceUrl(URL.createObjectURL(file));
 })
}

This solution is well suited when we are sending not only image but also data along the image. In this scenario, the image needs to be sent as base64.
getStream() {
      this.streamingService.createStream().subscribe(data => {
            this.url = data.base64ImageData
     })
    }

Since the base64 encoding has a payload of 133% of the initial image, it might be costly to use it in all settings. In case the image needs to be served along with other data, the whole data can be sent as a binary. This will require encoding the data server-side and decode it client-side.
Since this might require a bit of computation, using a web-worker can be considered for not only decoding the data but also for displaying the image.
If the image is sent as a binary, using canvas for rendering will be faster
canvas.getContext('2d').putImageData(imageBinaryData, 0, 0)

Comment: thanks for spotting the mistake, I edited the answer
Comment: Wouldn't the base64 have 133% increase, and not 1.33%?
Answer: I see two problems here, that may improve this solution.
First of all, how do you know that the request will spend less than 250 milliseconds? if not, you know that switchMap will cancel it? Maybe exhaustMap or concatMap could fit better if you don't want to lose the current request.
The second point is that the interval(X) and the HTTP request are inside the angular zone, so they will fire change detection... This could bring you several performance issues. So you should run the interval and the request outside the zone and detect changes manually.
Said that I will purpose a solution like this:
private subscription: Subscription;
cosntructor(public ngZone: NgZone, public cd: ChangeDetectorRef){
   getVideo();
}

getVideo() {
    this.subscription = this.ngZone.runOutsideAngular(() =>
         interval(250).pipe(exhaustMap(() => this.appService.getPictures()))
           .subscribe(data => {
              const file = new Blob([data], {type:'image/png'});
              this.url = this.sanitizer.bypassSecurityTrustResourceUrl(URL.createObjectURL(file));
              // Do change detection in orther to actualize [url] binding
              this.cd.detectChanges();
           })
    );
}
ngOnDestroy() {
    // remember to unsubscribe from interval subscription
    this.subscription.unsubscribe()
}

I don't think that with this code you would have much performance issues... although this type of long polling is never good, I don't think it would freeze your app. Try it and let me know.
Hope this helps.
Comment: What I really wants to do is to buffer the frames in order to have a video that I can use in a `video` tag.
Comment: The code throws this error: `interval().concatMap() is not a function` . Could you please provide a working example when using a WebRTC we can fetch frame from the server and not from the device camera and then turn that into a video?
Comment: Hi @edkeveked, given your situation, the preferred solution is to use an image tag, you can't buffer the getPictures() etc. and transofmr it in video format, although it has no sense. If you want to do that, just change your service and set some WebRTC connection or sov providing video content. Is my recommendation.
Although, try my solution, it may work well.
Comment: @edkeveked sorry, I forget the pipe operator.
I can't provide you an example, is too much. It means changes on server side, client side etc... check out kurento https://www.kurento.org, they have good docs and already done code in order to work with webRTC on both server and client.

turn video frames to streaming video


Is there a module that can display items in database?

Question: after a few rounds of research, seems like Drupal is the way to go to manage data, but I'm fairly new to this...
I want to build a website that displays entries in the form of a table from a database.
For example,
1. User type in words of interest in the search bar
2. Data will be retrieved and displayed, which they can use for comparison (ie between data 1 and data 2)
One very similar example is like,
http://www.eigenfactor.org/
Your help is greatly appreciated
Comment: No no @rooby, he's looking for something specific and there's only one word to describe it.  On three.  One...Two...Three...`Views`
Comment: This question appears to be off-topic because it seems to be asking how to build a whole site based on a vague spec, and also how to replicate functionality of another site.
Answer: You can use views module to display list of content.

Views module provide functionality to admin to display list of content
  in any format without using code.

If you want to implement autocomplete functionality with views, then you can use views autocomplete filters module.

Views autocomplete filters module provide functionality to set
  autocomplete to node title or any fields.


Is this a standard way to describe this random variable?

Question: People:
I am reading a paper on probability in which the authors describe a [real-valued] random variable $X$ as symmetric, with $-1 \leq X \leq 1$, $E(X)=0$,  and $E(X^2)=1$.  For non-probabilists like me out there, "symmetric" means what you probably think it means: for any $a<b$, $P(a<X<b)=P(-b<X<-a)$.  Of course the condition $E(X)=0$ is redundant, because $X$ is symmetric with $-1 \leq X \leq 1$.
The conditions above are equivalent to $X$ equaling $1$ with probability $1/2$ and $-1$ with probability $1/2$.  I think the paper would be more readable if the authors had just described $X$ that way.  They use the above wording over and over again throughout the paper.
So my question is, is the way the authors describe $X$ standard in the probability world, or is it a sign that the authors overlooked something that should have been obvious and that maybe the paper is not so good?
Comment: @Stefan: It's hard to answer your question without actually being able to see the context. e.g. the paper could naturally be interested in studying a probability distribution with those properties, and proving the one you mention is the only possibility is interesting. Or, it may have already been clear from context exactly what probability distribution was meant, and now they are making that description because they're actually going to use those properties.
Comment: Sinces $X$ is **real-valued**, it doesn't have to be an integer. Thus, $X$ has a probability distribution over $[-1,1]$, instead of over $\{-1,0,1\}$.
Comment: @Ragnar: The distribution the OP describes is the only one with the correct variance.
Comment: @Hurkyl, You're right, hadn't really thought about it that much. In that case, maybe the author proved/assumed some things about $X$ (like $E(X)=0$ and $E(X^2)=1$ and $-1\leq X\leq 1$), and then just uses $X$, without stating the obvious.
Comment: @Hurkyl : the authors don't really give a context, they just write, if $X$ is a symmetric random variable with these properties, yada, yada, yada. I suspect that maybe they didn't notice that those properties determine $X$ completely, which is a serious oversight.  If they did know that $X$ is a Rademacher random variable (see the answer I accepted), then I think describing it the way they did is very bad style (this is why I asked if probabilists do this all the time.  Batman's answer suggests that they don't).
Answer: The conditions that $-1 \leq X \leq 1$ and $E[X^2]$ are sufficient to characterize the random variable as being a Rademacher random variable (i.e. takes on values $\pm 1$ with equal probability), since if $X$ took values in $(-1,1)$ with positive measure, $E[X^2]$ would be strictly less than $1$.
They should have just called it a Rademacher random variable and moved on.
Edit: You also sometimes see the name "Symmetric Bernoulli".
Comment: That distribution has a *name*?!?!?!
Comment: Yes, its very commonly used in statistical learning theory/probability on banach spaces/concentration inequalities/empirical process theory for a technique known as "symmetrization". Also, look up Rademacher Complexity/Averages.


iOS Array of Colors Retrieval Error

Question: I am trying have a button that switches the color of a button. For some reason the code below isn't working...
**//Header (.h)**
    @property UIColor *imageColor1;
    @property UIColor *imageColor2;
    @property UIButton *button1;
    @property UIButton *button2;
    @property CGRect viewBounds;
    -(IBAction)changeColor:(id)sender;
    -(IBAction)createButton;

**//Implementation (.m)**
    @synthesize imageColor1;
    @synthesize imageColor2;
    @synthesize button1;
    @synthesize button2;
    @synthesize viewBounds;

    -(IBAction)createButton{
        self.viewBounds = self.view.bounds;

        self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button1.frame = CGRectMake(CGRectGetMidX(self.viewBounds)-30, CGRectGetMidY(viewBounds)-15, 60, 30);
        self.button1.backgroundColor = self.imageColor1;
        [self.view addSubview:button1];

        self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button2.frame = CGRectMake(CGRectGetMidX(self.viewBounds)-15, CGRectGetMidY(viewBounds)-30, 30, 60);
        self.button2.backgroundColor = self.imageColor2;
        [self.view addSubview:button2];

    }

    -(IBAction)changeColor:(id)sender{

       int changeCount = 0;
       int changeCount1 = 1;
       NSArray *colors = [[NSArray alloc] initWithObjects:[UIColor redColor],[UIColor blueColor],[UIColor yellowColor],[UIColor                  magentaColor],[UIColor greenColor],[UIColor orangeColor],[UIColor purpleColor], nil];
       if(changeCount < 7){
          imageColor1 = [colors objectAtIndex:changeCount];
       }
       else{
          changeCount = changeCount - 5;
          imageColor1 = [colors objectAtIndex:changeCount];
       }
       if(changeCount1 < 7){
          imageColor2 = [colors objectAtIndex:changeCount1];
       }
       else{
          changeCount1 = changeCount1 - 5;
          imageColor2 = [colors objectAtIndex:changeCount1];
       }
       changeCount++;
       changeCount1++;
    }

Basically, whenever the user taps the "Change Color" button, the variables changeCount and changeCount1 increase their count, and the values in self.imageColor1 and self.imageColor2 are changed to the subsequent values in the array colors. For some reason, this code isn't working and the colors don't change. The createButton method works because whenever I press it, a new button appears, but if I create a button and then press the change color button and then create another button, the color of the new button is still the same. So basically I need to know what's wrong with my code. Thanks in advance.
Answer: That's because you reset the changeCount and changeCount1 variables every time.  Notice you recreate them and set them to 0 and 1 at the beginning?  So of course they are never going to change.  You are creating them as if they are static variables, but they are not.  They are local variables.  The NSArray could be static, but the count variables are better off as member variables (i.e. defined in your interface).

iOS Array of Colors Retrieval Error


Lagrange for Economic model with random variable

Question: I am working on a simple 1 period economic model with random return. The model is:
Period 1: $c_0 = y_0 - s$
Period 2: $c_1 = y_1 + \tilde{r} \cdot s $
Where:

$y_0, y_1$: are lum sum payment (it means it is given in each period), deterministic given value

$c_0, c_1$: are consumption

$s$: saving/investment in period 0 (which will growth to $(\tilde{r} \cdot s)$ at period 1)

$\tilde{r}$: random return. For example if $\tilde{r} = 1.05$ means \$1 will growth to \$1.05

The model objective is to maximize Utility:
Utility (U) = $\mathbb{E}[u(c_0) +  \cdot u(c_1)]$
Where:

$u(\cdot)$: is utility function, for simplicity take $u(\cdot) = ln(\cdot)$

$$: discount for future consumption, deterministic given value

Normally, to solve this problem, we use Lagrange multiplier to solve for $c_0$, $c_1$, and $s$
$\mathcal{L} = u[c_0] +  \cdot \mathbb{E}[u(c_1)] - \mu \cdot \left( c_0 - y_0 + s \right) - \lambda \cdot \left( c_1 - \tilde{r} \cdot s - y_1 \right) $
Now taking derivatives, we have:
(1) $ \frac{\partial\mathcal{L}}{\partial c_0} $ → easy
(2) $ \frac{\partial\mathcal{L}}{\partial c_1} $ → This is the one I don't know how to do
(3) $ \frac{\partial\mathcal{L}}{\partial s} = \mu + \lambda \tilde{r} = 0$ → I also not sure about this one too
I am getting stuck at (2) and (3). Especially number (2) since I do know how to take derivative of a function with random variable in this case. What should we do in this case?
Answer: I think we can transform random variable into a deterministic model as follow:
Obj: Max = $u[c_0] + β \mathbb{E}[u_1] = u[c_0] + β \sum  p^i  u[c_1^i] $
Constrains:
$c_0 = y_0 - s$
$c_1^i = y_1 + s \cdot r^i$
Then we can apply Lagrange:
$\mathcal{L} = u[c_o] + \sum  β p^i u[c_1^i] - \mu \left( c_0 - y_0 + s \right) - \sum \lambda^i \left( c_1^i - y_1 - s r^i \right)$
$\frac{\partial \mathcal{L}}{\partial c_0} = u'[c_0] - \mu = 0$
$\frac{\partial \mathcal{L}}{\partial c_1^i} = β p^i u'[c_1^i] - \lambda^i = 0$
$\frac{\partial \mathcal{L}}{\partial s} = -\mu + \sum \lambda^i r^i = 0$
Then we might need some more condition to solve this system of equation
Any comment?


How to find the the guitar notes in the staff notation if it starts from other than key of C

Question: I can identify the open string in the key of C, but i don't know how to identify the open string if it starts from other than key of C,Can someone help me, I have shown below the music sheets
Where is the guitar notes in which fret it falls if it starts from key of Bb?
Open string in the key of C, is shown below

How to indentify open strings in the staff notation?
Comment: This question isn't easy to follow. Could you please ask it another way? The open strings are the same note names whatever key a piece is in. Those open notes are not really 'in C', they just happen to all be notes from the key of C. Or key G, or key D or key A
Comment: What do you mean "the open string"? Can you explain what and where "the open string" is in C?
Comment: I have shown in my post for open string in C @piiperi
Comment: I am sorry for my way of asking question, this makes you all confussion. I have modified my question @Tim
Answer: You look at the notes in the scale. Whichever of them happens to be E, A, D, G or B, (or an enharmonic equivalent) can potentially be played with an open string, at least from the one octave. And assuming that your guitar is tuned to regular E A D G B E tuning.
The C major scale doesn't have any sharps or flats, and its notes are:

C, D, E, F, G, A, B

From this set of notes, D, E, G, A and B can be played using an open string (in the one octave where the open strings are). For C and F, no open string can be found.
Bb major has two flats: Bb and Eb. The notes of the scale are:

Bb, C, D, Eb, F, G, A, Bb

From this set of notes, D, G and A can be played using an open string, at least in one octave. For Bb, C, Eb and F, no open string can be found.

The E, B and E strings are nowhere on the Bb major scale, so that's why there's no arrows for them.
The higher A note on the staff doesn't have an arrow pointing to any string. That's because it's in a higher octave than the open A string.
(Enharmonic equivalent means, for example Fb - if you ever encounter such a thing - can be played with the E string. Cb can be played with the B string.)
Comment: This could end up sounding rather strange. OP is obviously a beginner, and could well play those open strings in another octave to the dots! Could be fun!
Comment: @Tim Well yeah, I noticed that when making the picture ... maybe it isn't obious without specifically pointing out that the higher A doesn't have an arrow even though there's an A string. :)


Linear Mixed Models assistance

Question: I'll try and keep this as concise as possible. I have a dataset, and I am looking at how Fat mass (y) depends upon physical activity. I have data at years 8, 9,12, and 15, and I am looking to fit a linear mixed model to my data.
Firstly, I performed a box cox transformation so that my data meets the assumptions of a linear model, and the Q-Q plots and the histogram both agree with the assumptions. My first question is do I fit 4 different linear models? ( I have 1,000) participants, or is there a way to combine all of them?
My second, is that I know that the physical activity is my fixed effect, however what are my random effects? I should add that there are other explanatory variables, such as affluence which is a factor (1-5).
I should note that I know how to program to a high standard in r, I'm just struggling to get my head around this. I think I am on the right lines when I understand that each participant provides one data point for each four years, so I guess that my random effect must be 1| participant?
Thanks for this, I'll give it a shot and see what I can come up with
That seems fairly straightforward, I'll post my results once I have them.
Cheers

So the data was transformed once the plot of the fitted values was shown to be random,
As for this, I therefore fit a model with 1|participant ? And is it four models that I plot or just the one?
Hello again,
I have managed to sort out my data, and have fitted the following model;
newmodel<-lmer((y8_FMI)^0.38~y8_MVPAper + (y8_MVPAper|Child_ID)

FMI is Fat Mass Index, and I have transformed it to the power according to the Box Cox transformation. MVPAper is the physical activity percentage, and of course is for the year 8 participants. The child ID is for the 1,029 that are participating in this study.
I get the following error: Error: number of levels of each grouping factor must be < number of observations
When I run my code, any ideas why?
Answer: The distributional assumptions are concerning the residuals, not the variables themselves. There is no condition/assumption that the variables be normally distributed, so Box-Cox transformations are not needed (at least, not for that reason). The residuals should be tested for normality
As to your experiment, you have repeated measures taken on individuals, so you have clustering, therefore the random effect will be the individual. This is known as random intercepts (because each individual has their own intercept). You could also have random effects for other covariates, so that each individual has their own slope too (random slopes).
In R using lme4 this could look something like:
m <- lmer( y ~ t + (t|individual), mydata)

Here you treat time, t as a continuous variable. If the association is linear, this is great, if it is non-linear you can consider either introducing a quadratic term into the model, or coding the variable separately and have a piecewise-linear model. You don't need to run 4 models, one for each time point.

Linear Mixed Models assistance


How to attach ImageView above BottomSheet?

Question: I found this article
https://medium.com/android-bits/android-anchoring-views-to-bottom-sheet-9c9069caf7d4
Here explain the case what I need except one thing that I need to change. I changed FloatingActionButton to ImageView because I need to attach ImageView above BottomSheet.
So, my xml
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

.........

    <include layout="@layout/other_bottom_sheet_layout"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:src="@drawable/my_image"
        app:layout_anchor="@+id/ll_other_bottom_sheet"
        app:layout_anchorGravity="top|end" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

And here screenshot

As you can see half of the image above the BottomSheet and half of image below...
I need a whole image to be exactly above
I tried to change a margin like this
<ImageView
    android:layout_width="170dp"
    android:layout_height="50dp"
    android:layout_marginBottom="60dp"
    android:src="@drawable/my_image"
    app:layout_anchor="@+id/ll_other_bottom_sheet"
    app:layout_anchorGravity="top|end" />

And in initial state it looks ok

But when I expand BottomSheet I get such result

So, question is: how to attach ImageView above of BottomSheet and it should be above all the time, doesn't matter expand or collapsed bottomsheet...
Comment: Keep the image `` as a part of bottom sheet layout itself.. Then it would be always above the bottom sheet.
Answer: Don't use margin for this. Use layout_dodgeInsetEdges="bottom" attribute for the ImageView. Check this for more info.
<ImageView
   android:layout_width="170dp"
   android:layout_height="50dp"
   android:src="@drawable/my_image"
   app:layout_anchor="@+id/ll_other_bottom_sheet"
   app:layout_anchorGravity="top|end"
   app:layout_dodgeInsetEdges="bottom" />

Comment: Worked for me when I added layout_insetEdge to the bottom sheet.
Comment: I love you, just had to figure out that you have to put app:layout_insetEdge="bottom" on the bottom sheet also (here: ll_other_bottom_sheet)


Exponents in Binomial Distribution

Question: Why do we have exponentials in equation of binomial distribution? What's the intuition of it?
Comment: Because when events are independent their joint probability is calculated by multiplying the individual event probabilities, and all those are either `p` or `q`.  Q.E.D., the products are powers of `p` and `q`.
Answer: By definition, a (discrete) probability mass function $f_X$ of a random variable $X$ satisfies $$\sum_{k \in X} f_X(k) = 1,$$ where $f_X(k)$ is the probability that the random variable $X$ is equal to the given value $k.$ (I have admittedly for simplicity abused notation by writing $X$ as both a random variable and its set of outputs.)
By the Binomial Theorem, we can compute the powers of a sum of numbers by $$(p + q)^n = \sum_{k = 0}^n \binom n k p^k q^{n - k},$$ where the coefficient $\binom n k$ gives the number of ways to choose (without regard for order) $k$ elements from among a collection of $n$ elements. Labelling the probability mass function $f_X(k) = \binom n k p^k q^{n - k}$ and the random variable $X$ with outputs $\{0, 1, \dots, n \}$ and assuming that $q = 1 - p,$ indeed, we obtain a discrete probability distribution. We can check that $f_X(k)$ is a probability mass function sums to $1$: $$\sum_{k \in X} f_X(k) = \sum_{k = 0}^n f_X(k) = \sum_{k = 0}^n \binom n k p^k q^{n - k} = (p + q)^n = 1^n = 1.$$

On the matter of intuition, one can be convinced by a simple counting argument: if we observe $n$ independent events such that either $P$ happens with probability $p$ or $Q$ happens with probability $q$ (i.e., not $P$ happens with probability $q = 1 - p$), then by the Multiplication Principle, the probability of witnessing $P$ a total of $k$ times is given by $p^k q^{n - k},$ as we must therefore witness $Q$ a total of $n - k$ times. Of course, you will notice immediately that this is an undercount: for $n = 2,$ we could have witnessed $P$ a total of $k = 1$ times in any of the $2 = \binom 2 1$ ways $PQ$ or $QP.$ Generally, in order to get the correct count, we must multiply by a factor of $\binom n k,$ i.e., the number of ways to choose (or observe) the event $P$ a total of $k$ times in $n$ trials.

Exponents in Binomial Distribution


Does marching cubes in vtk require specific input?

Question: The marching cubes class does not generate anything after I passed a ImageData to it.
I am trying to generate surface from a 3D numpy array. I used evtk to export the array to a .vti (Structured Points) file and stored as pointData. Then, read the file by vtkXMLImageDataReader. After passing the output of the Reader (vtkImageData) to vtkMarchingCubes class, the class returns a vtkPolyData with no point or cell, vtkImageData were used in vtk wiki's example, the MarchingCubes class should have no problem to handle it.
I tried to export the ImageData generated by the Reader, the output can be visualized by ParaView without any problem.
I just wondering are there any special requirement for the marching cubes in vtk to generate the surface? It will be even better if the 3D numpy can directly transform to data type that suitable for the MarchingCubes.
Here is my code to read the .vki file and pass it to marching cubes:
dataReader = vtk.vtkXMLImageDataReader()
dataReader.SetFileName('model_name.vti')
dataReader.Update()
contacts = vtk.vtkDiscreteMarchingCubes()
contacts.SetInputConnection(dataReader.GetOutputPort())
contacts.SetValue(0,1)
contacts.ComputeNormalsOn()
contacts.Update()
result = contacts.GetOutput()

Comment: what does result give you?
Comment: result gives me an empty vtkPolyData.
Comment: Is it possible that the values you specify do not make sense for the input data?
Comment: The data are ranged [1,7]. I compared the input with examples from vtk. Seems the XMLImageDataReader does not convert the PointDataArray to the scalar and MarchingCubes won't work without scalar...
Comment: Sounds like you answered your own question
Answer: If you wish to do this in the python/numpy world, then I would suggest using the marching cubes implementation from here (or look at isosurface routines in pyqtgraph, mayavi ...). Although there are many tutorials on moving data to and from VTK, if you are already programming with numpy, I wouldn't mix the two methodologies.
Answer: Although this question is quite old already, I ran into a very similar problem.
As YC.Chui pointed out in his comment, we need to set the scalar values in the read data. For me this helped:
reader = vtk.vtkXMLImageDataReader()
reader.SetFileName('model_name.vti')
reader.Update()

readData = reader.GetOutput()
readData.GetPointData().SetScalars(readData.GetPointData().GetArray(0))

I am quite new to VTK and have absolutely no idea why this solves the problem and whether there is a better way. Feel free to comment and correct me!


How to get only numbers from the row?

Question: I'd like to ask if there is any efficient way to get the values from the row instead of writing:
as.numeric(data[2,]

Example:
data <- data.frame(Names=c('Train','Car', 'Plane', 'Bicycle'),
                   Day1 = c(15,22,36,22),
                   Day2 = c(33,57,2,41),
                   Day3 = c(87,32,15,25),
                   Day4 = c(14,2,6,33),
                   Day5 = c(41,76,11,44),
                   Day6 = c(11,12,35,25),
                   Day7 = c(52,2,42,55))

Not correct:
> data[2,]
  Names Day1 Day2 Day3 Day4 Day5 Day6 Day7
2   Car   22   57   32    2   76   12    2

Correct output:
> as.numeric(data[2,])
[1]  2 22 57 32  2 76 12  2

Any "nicer" way to get such output ?
Comment: `DayX` are the columns in `data`, so `data$Day2` works.
Comment: Do you want the output to be a vector or a data.frame?
Comment: `as.numeric` transform `car` in `2`, is that what you want?
Comment: I don't think "data" is the data frame you expected. Where did the 2 come from in your as.numeric ? edit : welp @Victorp got me
Comment: output as a vector. You are right this 2 in the begining shouldn't be there. I just want to get values from each day for a specific row. Is it possible to call a row same as a column with data$xxx ?
Comment: but what about rows ? like data$Car ? such fast access to the rows is possible ?
Comment: No, use `t()` for transpose your data, `Car` will be a column and `data$Car` will work
Answer: I think you should convert your data.frame into a matrix:
mat <- as.matrix(data[, -1])
rownames(mat) <- data[, 1]
mat[2,]

How to get only numbers from the row?


Issues with make execution order

Question: This is a make file I have wrote with the current goal of turning all c++ files in a directory into object files. My issue is that it always executes g++ first then any other commands second.
CC=g++
CPPFLAGS=`pkg-config --cflags --libs gtkmm-3.0`
SRCS=$(wildcard classes/source/*.cc)
PROGS=$(patsubst %.cc,%,$(SRCS))
CLASS_HEADERS=classes/
all: $(PROGS)
    mkdir -p build/release/objs
%: %.cc
    $(CC) -I$(CLASS_HEADERS) -c $< -o build/release/objs/$(@F).o $(CPPFLAGS)

results in:
g++ -Iclasses/ -c classes/source/helloworld.cc -o build/release/objs/helloworld.o `pkg-config --cflags --libs gtkmm-3.0`
mkdir -p build/release/objs

Thanks
Comment: Provide another dependency target for `build/release/objs`, and place that before `$(PROGS)`.
Comment: The rules for `all` say: make sure all the `$(PROGS)` are up to date; when that's done, running the `mkdir` will finish the job of creating `all`.  You need something more like `all: objdir $(PROGS)` where the rule for `objdir` might be: `objdir: ; mkdir -p build/release/objs` (where you'd use a newline and tab in place of the semicolon, though the notation shown does actually work on a single line).  This is just a longer version of what @πάνταῥεῖ said.
Comment: Jonathan Leffler you have solved my problem, and provided a great explanation to help me better understand how make works.
Answer: The simplest fix is to always issue mkdir before using the target directory.  If using the -p option, there's really no harm in executing this command redundantly.
%: %.cc
   mkdir -p build/release/objs
   $(CC) -I$(CLASS_HEADERS) -c $< -o build/release/objs/$(@F).o $(CPPFLAGS)

As noted in my answer to GNU make's -j option: Although it may sometimes work, it is not correct to simply add a mkdir target as an initial dependency in your existing all dependency list.  In principle, these could be executed in any order (so mkdir wouldn't necessarily be first).  If you want to take the dependency-based approach, you have to start doing more complicated stuff.  For example using make recursively:
all: do_mkdir
   $(MAKE) $(progs)

do_mkdir:
   mkdir -p build/release/objs

Answer: Possible solution could be creating a target for the object directory and place it before the build target:
......
OBJ_DIR := build/release/objs

all: create_obj_dir $(PROGS)

create_obj_dir:
    mkdir -p  $(OBJ_DIR)

%: %.cc
    $(CC) -I$(CLASS_HEADERS) -c $< -o $(OBJ_DIR)/$(@F).o $(CPPFLAGS)

Comment: Won't necessarily work if you use -j to run multiple make threads
Answer: Recipes for a target are executed after any prerequisites, so when you make all make will go through $(PROGS) before running mkdir.
build/release/objs should be a prerequisite of the object files, because they can't be made unless the directory exists. The directory should also be an order-only prerequisite because you don't want to remake every single object file if the dir timestamp changes.
Your pattern rule is broken(see 2) as well, and you're using the wrong flag variables. You can also recycle the built-in implicit rule for compiling.
CXXFLAGS := $(shell pkg-config --cflags --libs gtkmm-3.0)
CPPFLAGS := -Iclasses/
SRCS     := $(wildcard classes/source/*.cc)
OBJS     := $(SRCS:classes/source/%.cc=build/release/objs/%.o)

.PHONY: all
all: $(OBJS)

$(OBJS): build/release/objs/%.o: classes/source/%.cc | build/release/objs
    $(COMPILE.cc) $(OUTPUT_OPTION) $<

build/release/objs: ; mkdir -p $@

Side note: since you're only compiling -libs gtkmm-3.0 probably shouldn't be in the flags, you provide libs when linking.
Comment: @Russeree Please accept the answer if it answered your question.

Issues with make execution order


Modelling with Conditional Missing Numeric Variable in R

Question: First time poster and I apologize if the question seems basic but I have searched far and wide seeking the right answer but haven't come across it. Could even be a very basic yes/no. I am currently building a model for a project to predict churn in a f2p mobile game, meaning players can choose to spend money if they wish to advance quicker. My concern is I have a number of numeric variables that apply only to spending customers. For example "time until spend", "time since last spend", "standard deviation of purchases" and similar. Other spending variables such as "total spend", "average price" etc can be imputed as 0, but the former values cannot be.
The problem of course is that they aren't missing in the traditional way, rather they simply don't exist and are not possible for these customers. I do not wish to factor the variables as I believe they are more valuable in their numeric form. Is there a way to model this in R without having to build independent models for spenders and non spenders? I will most likely be using logistic regression and decision trees for my problem. Thanks for your help!
Answer: In regression, the standard way to handle this is to add a categorical predictor to the model that indicates, in your example, whether each customer chose to spend. That's nicely explained in at least two Cross Validated pages, here and here. For non-spenders in a regression analysis you can set the values of spending-related variables to 0 and get useful results, as the coefficients for spending-related variables will then only add to the model for customers who chose to spend.
I haven't thought through whether this approach will work for tree-based models. My first guess is that for trees you might need also to set up a separate set of categories to distinguish the non-spenders from the spenders who have a value of 0 for each spending-related predictor.


Maximizing $\frac{\sum w_i a_i}{\sqrt{\sum w_i^2 b_i}}$ with $\sum w_i = 1$

Question: Intuitively, $\frac{\sum w_i a_i}{\sqrt{\sum w_i^2 b_i}}$ looks like a ratio between a $L_1$ norm and $L_2$ norm.
To provide motivation, we consider i.i.d. $(x_i, y_i)$ and $\text{Var}[x_i] = \sigma_{i}^2, \text{Var}[x_i] = \tau_{i}^2, \text{Cov}[x_i, y_i] = r_{i}$. We wish to find the linear combination of $\sum w_i x_i$ such that $\text{Cor}[\sum w_i x_i, \sum y_i]$ is maximized.
$$
\text{Cor}\left[\sum w_i x_i, \sum y_i \right]
\propto
\frac{\sum w_i \text{Cov}[x_i, y_i]}{\sqrt{\sum w_i^2 \text{Var}[x_i]}} = \frac{\sum w_i r_i}{\sqrt{\sum w_i^2 \sigma_i^2}}
$$
Answer: Define two vectors:
$\mathbf{x} = (w_1 \sqrt{b_1}, w_2 \sqrt{b_2}, \ldots, w_n \sqrt{b_n})$,
$\mathbf{y} = (\frac{a_1}{\sqrt{b_1}}, \frac{a_2}{\sqrt{b_2}}, \ldots, \frac{a_n}{\sqrt{b_n}})$
$\mathbf{x} \cdot \mathbf{y} = \sum w_i a_i$
and
$\mathbf{x} \cdot \mathbf{x} = \sum w_i^2 b_i, $
$\mathbf{y} \cdot \mathbf{y} = \sum \frac{a_i^2}{b_i}$
Applying the Cauchy-Schwarz inequality:
$(\sum w_i a_i)^2 \leq (\sum w_i^2 b_i)(\sum \frac{a_i^2}{b_i})$
Therefore, the maximum value of the original expression is:
$\frac{\sum w_i a_i}{\sqrt{\sum w_i^2 b_i}} \leq \sqrt{\sum \frac{a_i^2}{b_i}}$
The equality holds when $\mathbf{x}$ and $\mathbf{y}$ are linearly dependent, which means:
$w_i \sqrt{b_i} = c \cdot \frac{a_i}{\sqrt{b_i}}$
for some constant $c$.
For our motivating example,
$$
\text{Cor}\left[\sum w_i x_i, \sum y_i \right]
\propto
\frac{\sum w_i \text{Cov}[x_i, y_i]}{\sqrt{\sum w_i^2 \text{Var}[x_i]}} = \frac{\sum w_i r_i}{\sqrt{\sum w_i^2 \sigma_i^2}}
$$
$w_i$ should satisfy $w_i \propto \frac{r_i}{\sigma_i^2}$, which is the regression slope of regressing $y_i \sim x_i$.


Which will consume more memory?

Question: Suppose there are two arrays, one with n number of Class elements and one with n number of Struct elements. Which will consume more memory? Also what happens when we start manipulating the arrays. Below is what I tried.
class C {
    var a = ""
}

struct S {
    var a = ""
}

func sizeOf<T>(_ value: T) {
    print(MemoryLayout.size(ofValue: value))
}

let classVar = C()
let structVar = S()

let arrayClass = [classVar, classVar, classVar]
var arrayStruct = [structVar, structVar, structVar]

sizeOf(classVar)
sizeOf(structVar)
sizeOf(arrayClass)
sizeOf(arrayStruct)

var newStruct = structVar
newStruct.a = "Value changed"

sizeOf(structVar)
arrayStruct.append(newStruct)
sizeOf(arrayStruct)

Output
8
16
8
8
16
8

Why is struct size greater than class and also why are both the array of the same size? Is it because array itself is a struct? Can someone provide with detailed explanation?
Comment: Swift structs are generally dependent on the size of their components, in this case `String` has a size of 16 bytes so that is what you're seeing. Classes are generally pointer-sized as they are a reference to memory allocated somewhere. `Array`s themselves are laid out in such a way that they are fixed size no matter what they contain (I would assume that they are a pointer to some underlying implementation).
Comment: @Jonathan If you've got data it's worth adding as an answer.
Comment: Why not allocate a million of each and find out?
Comment: @tadman I tried appending `10000` elements and the result was same. But it takes a lot of time in the playground to calculate that, allocating a million of each will take even longer.
Comment: That's at least one data point. It shouldn't take too long to allocate if compiled, mind you, so if that's an option try that. Does that list the total memory footprint of the object?
Comment: I ran each ```10_000_000``` times and the result was the same
Comment: I ran 10 million iterations with ```enum``` as well with the same result
Comment: Then maybe you have your answer, but it's worth checking the overall process memory footprint to see what differences there are. I'd expect `struct` and `class` to be effectively the same unless you have subclasses involved.
Comment: @tadman I just tested down 10 subclass layers, all with the same result.
Comment: @tadman I noticed that recursive objects give larger numbers
Comment: Is it ok to say that both the arrays will have same impact on the memory as arrays are structs and elements will be stored in their own memory space and array element will be a pointer to that particular memory?
Answer: I think this is related too: Struct Memory Size vs. Class Memory Size
I ran the struct 10,000,000 times
I ran the class 10,000,000 times
I ran suclasses down to 10 layers 10,000,000 times each
I ran enum cases, and initialized enums 10,000,000 times
They all printed 8 (I will provide images)

I did find that doing this prints 16
struct S { struct SS { var a = "" } }
SS() // 10,000,000 times

For both the struct and recursive struct, the actual memory size given by XCode was actually larger around 170MB, I just wanted to point that out.
all structs: ~170 MB (even recursive ones)
all classes: ~97 MB (even subsclassed ones)
all enums: ~20 MB

Which will consume more memory?


Maior e menor valor

Question:
Faça um programa que receba um conjunto de valores inteiros e positivos e que calcule e mostre o maior e o menor valor do conjunto. (Para encerrar a entrada, deve ser digitado o valor zero).

Eu sei que a parte do menor valor está errada, pois a dúvida é exatamente conseguir o menor valor, o maior valor está correto.
package exercices2;

import java.util.Scanner;

public class MaiorEMenor {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double valor = 1;
        double valorMaior = 0;
        double valorMenor = 0;
        for(;valor!=0;) {
            System.out.println("Digite os valores (0 para parar):");
            valor = input.nextDouble();
            if(valor > valorMaior && valor!=0) {
                valorMaior = valor;

            }
            if(valor < valorMenor && valor!=0) {
                valorMenor = valor;
            }
        }
        System.out.println("Maior valor: "+ valorMaior);
        System.out.println("Menor valor: "+ valorMenor);

    }

}

Answer: O que aconteceu no seu código de errado? Você definiu valores iniciais ruins. Note que no enunciado não foi definido que os números serão estritamente positivos. A seguinte entrada daria o resultado errada para o valorMaior porém certo para valorMenor:

-1.0 -10.0 -7.0 0

Por que vai dar valor errado? Porque você está pegando um valor de fora da lista e usando como base.
Sem usar Java 8, a solução seria pegar o primeiro número como algo especial. Eu vejo três alternativas:

a leitura do primeiro número ser considerada especial
usar alguma marcação para indicar que é a primeira leitura e fazer o tratamento especial, como usar o wrapper Double para guardar os valores máximo e mínimo da lista inicializado com null
armazenar numa lista e então tratar como se estivesse no caso 1

Primeira leitura especial
Scanner input = new Scanner(System.in);
double valorLido = input.nextDouble();
double menor, maior;
menor = maior = valorLido; // inicializo ambos os valores maior e menor com o primeiro valor lido
while (valorLido != 0) {
  valorLido = input.nextDouble();
  if (valorLido != 0) {
    if (valorLido > maior) {
      maior = valorLido;
    } else if (valorLido < menor) {
       menor = valorLido;
    }
  }
}

System.out.println("maior " + maior);
System.out.println("menor " + menor);

Tratei a primeira leitura de modo especial. Inicializei os valores básicos e só então fui atrás de saber como atualizar para novos valores.
Note que devido ao princípio da boa ordenação, se a > b e b > c, então tenho que a > c. Logo não tem como eu atualizar o valor maior e, ao mesmo tempo, atualizar o valor menor. Então, devido a isso, eu coloquei um else if no lugar de sempre verificar se se deve atualizar o menor valor oi não.

Marcação da primeira leitura via wrapper
Scanner input = new Scanner(System.in);
Double menor, maior;
menor = maior = null;

while (true) {
  double valorLido = input.nextDouble();
  if (valorLido == 0) {
    break;
  }
  if (maior == null) {
    // marcação da primeira leitura, devo atualizar ambos os valores de maior e menor
    maior = menor = valorLido;
  } else if (valorLido > maior) {
    // já caiu no caso else, então não é primeira leitura
    maior = valorLido;
  } else if (valorLido < menor) {
    menor = valorLido;
  }
}
System.out.println("maior " + maior);
System.out.println("menor " + menor);

Marcação da primeira leitura usando um booleano
É absurdamente semelhante. Só que aqui eu devo inicializar com valores arbitrários (escolhi NaN) os valores de maior e menor.
Scanner input = new Scanner(System.in);
double menor, maior;
menor = maior = Double.NaN;
boolean primeiraLeitura = true;

while (true) {
  double valorLido = input.nextDouble();
  if (valorLido == 0) {
    break;
  }
  if (primeiraLeitura) {
    // marcação da primeira leitura, devo atualizar ambos os valores de maior e menor
    maior = menor = valorLido;
    primeiraLeitura = false; // marcando para não ler novamente
  } else if (valorLido > maior) {
    // já caiu no caso else, então não é primeira leitura
    maior = valorLido;
  } else if (valorLido < menor) {
    menor = valorLido;
  }
}
System.out.println("maior " + maior);
System.out.println("menor " + menor);

Armazenar em uma lista
Essa alternativa é trivial. Jogue a leitura para um ArrayList e depois trate os valores dentro dele.
Scanner input = new Scanner(System.in);
ArrayList<Double> lista = new ArrayList<>();

double valorLido;
while (true) {
  valorLido = input.nextDouble();
  if (valorLido == 0) {
    break;
  }
  lista.add(valorLido);
}
double maior, menor;
maior = menor = lista.get(0);
for (Double valor: lista) {
  if (valor > maior) {
    maior = valor;
  } else if (valor < menor) {
    menor = valor;
  }
}
System.out.println("maior " + maior);
System.out.println("menor " + menor);

Usando stream API do Java 8
Eu poderia usar diversas alternativas, mas a mais rápida é usando a DoubleSummaryStatistics. Para isso vou usar um coletor especial: Collectors.summarizingDouble. Estou adaptando essa resposta do Stack Overflow internacional.
Scanner input = new Scanner(System.in);
ArrayList<Double> lista = new ArrayList<>();

double valorLido;
while (true) {
  valorLido = input.nextDouble();
  if (valorLido == 0) {
    break;
  }
  lista.add(valorLido);
}
DoubleSummaryStatistics summary = lista.stream().collect(Collectors.summarizingDouble(Double::doubleValue));
double maior = summary.getMax();
double menor = summary.getMin();
System.out.println("maior " + maior);
System.out.println("menor " + menor);

Maior e menor valor


Given $e^{i\theta}=x+iy$, how do you prove that $e^{-i\theta}=x-iy$ without using Euler's formula?

Question: Suppose we use the following definition of complex exponentiation:
$$
e^z=\lim_{n \to\infty}\left(1+\frac{z}{n}\right)^n \, .
$$
For a given real value of $\theta$, let $e^{i\theta}=x+iy$. I want to show that $e^{-i\theta}=x-iy$ without using Euler's formula.
I think that this is true because the algebraic properties of $i$ and $-i$ are identical. This means, roughly, that if we have a valid equation involving $i$, and replace every occurence of $i$ with $-i$, then we still get a valid equation. Here is my attempt to turn it into a rigorous argument. Is it correct?

Since the mapping $a+bi\mapsto a-bi$ is a field automorphism of $\Bbb{C}$, we know that for all $n\in\Bbb{N}$,
$$
\left(\overline{1+\frac{i\theta}{n}}\right)^n=\overline{\left(1+\frac{i\theta}{n}\right)^n} \, .
$$
Hence,
\begin{align}
e^{-i\theta} &= \lim_{n \to \infty}\left(1-\frac{i\theta}{n}\right)^n \\[5pt]
&= \lim_{n\to\infty}\left(\overline{1+\frac{i\theta}{n}}\right)^n \\[5pt]
&= \lim_{n\to\infty}\overline{\left(1+\frac{i\theta}{n}\right)^n}  \\[5pt]
&= \overline{\lim_{n\to\infty}\left(1+\frac{i\theta}{n}\right)^n} \tag{*}\label{*}\\[5pt]
&= \overline{e^{i\theta}} \\[5pt]
&= x-iy \, .
\end{align}
The line $\eqref{*}$ is justified because the function $a+bi\mapsto a-bi$ is continuous.
Comment: Yes, your reasoning is fine. If $a(n) \to A$ as $n \to \infty$, then $\overline{a(n))} \to \overline{A}$ as $n \to \infty$. You can just read this off the definitions.
Comment: Very elegant proof.
Comment: Possible duplicate - https://math.stackexchange.com/q/3997692/443030 ? Otherwise your proof seems correct.
Comment: From your definition you can directly prove that $e^{\phi+i\theta} =e^{\phi} (\cos\theta+i\sin \theta) $. And then your identity follows by using $\phi=0$.
Answer: It seems fine, maybe we can also proceed as follows, since
$$e^{i\theta}\cdot e^{-i\theta}=e^0=1$$
we have
$$(x+iy)(a+ib)=ax-by+i(ay+bx)=1$$
and

$ay+bx=0 \implies a=-b\frac x y$
$ax-by=-b\frac {x^2}y-by=-\frac b y=1 \implies b=-y \quad a=x$
Comment: Hi @Sebastiano, we can conclude that $e^{-i\theta}=x-iy$.
Comment: I have my mind....fused with the high temperatures in Sicily.
Comment: Hi user. Welcome back to MSE. Thanks for posting this answer. I have one query: I'm not sure how your argument works if $x=0$ or $y=0$, since we have no way of knowing *a priori* that $e^{-i\theta}$ can still be written in the form $ax+iby$ for some constants $a$ and $b$.
Comment: Hi @Joe thanks for your welcome! For $x=0$ we obtain $-by^2=1$ as condition which implies $b=-1$ and similarly for $y=0$ it seems to work. Maybe, is there some other properties we are using in this algebraic argument which you are nor assuming among the hypoteses?
Comment: What I mean is: suppose that $e^{i\theta}=0+1i$, for instance. Then, if the real part of $e^{-i\theta}$ is non-zero, then there is no way of writing $e^{-i\theta}$ in the form $ax+iby$. Of course, the real part of $e^{-i\theta}$ *is* zero, but we don't know that yet.
Comment: @Joe Ah ok now I get your point. I think you are right. I try to edit and use a more general approach.
Comment: @Joe Now it should be fixed for the issue you pointed out!
Comment: @user Hi, what happen if  $b=-y, \text{ and } a=x$? :-)


Firebase storage rule check if custom claims list contains value (hasAll, hasAny, etc)

Question: -- UPDATE --
It turns out the below should have worked, but didn't due to a bug, so this was recommended by Firebase Support as a workaround, and it worked for me:
request.auth.token.permissions.toSet().hasAll([0])
-----------------------------
I have an array/list of with a single integer on my custom claims like this:
someList: [0]

I want to check if a number exists in the list in my storage security rules.  I can't seem to get the hasAll rule to work that is mentioned in the documentation: https://firebase.google.com/docs/reference/security/storage#hasall
Some rules work when referencing the list so I know for sure that the property is there in my custom claims, but I can't seem to get it to work with hasAll or using tricks with strings.
Example -
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /clients/{clientId}/{allPaths=**} {
      // These work -
      allow read, write: if request.auth.token.someList.join('').size() > 0
      allow read, write: if request.auth.token.someList != null

      // These do not -
      allow read, write: if request.auth.token.someList.hasAll([0])
      allow read, write: if request.auth.token.someList.join('').matches('0')
    }
  }
}

♂  ?
Comment: The rules that work dont actually check for the integer. Are you sure your custom claims is properly set?
Comment: Can your try: `allow read, write: if request.auth.token.someList is list`
Comment: And `allow read, write: if request.auth.token.someList.size() == 1`
Comment: @l1b3rty I tried both `is list` and `size() == 1` and those work fine, but still not `hasAll([0])` ☹
Comment: and I did `admin().auth().getUser()` in a firebase function and I for sure have `someList: [0]` in my customClaims.
Comment: Have you write any data to `someList` before you write `0` to it?
Comment: Could you try re login your app?
Comment: @flutroid I have tried re-setting the custom claims and also re-logging in and no luck.
Comment: Try `allow read, write: if 0 in request.auth.token.someList`
Comment: @chevin99 Ok, But beware that when you update custom claims it won't also update data in rules immdetely see this [Firebase custom claims are not update in rules](https://stackoverflow.com/questions/58280361/firebase-custom-claims-are-not-update-in-rules)
Comment: @l1b3rty `allow read, write: if 0 in request.auth.token.someList` didn't work either
Comment: @flutroid I had made sure to force the token refresh client-side, but still no luck ☹.
Comment: In your client, can you try calling `getidtoken` with `forceRefresh` set to true before making the call? And still in the client, log the token you get back https://firebase.google.com/docs/reference/js/v8/firebase.User#getidtoken
Comment: @l1b3rty thanks, yes, I've done that and the values are there on the claims.  I should also point out I've been using that same `someList: [0]` for firestore rules for quite some time and they work, but haven't gotten it to work with storage rules yet.
Answer: Some things you can do:

Confirm custom claims are properly set and refreshed: in your client, try calling getidtoken with forceRefresh set to true before making the call, log the data you get back.

Check if the following rules pass:
allow read, write: if request.auth.token.someList is list;
allow read, write: if request.auth.token.someList.size() == 1;
allow read, write: if 0 in request.auth.token.someList;

Comment: It was indeed a bug.  See workaround in original question.
Comment: I'm probably going to open a ticket with firebase since I would assume one of those work but I had no luck with any.  Thanks.


Excel não consigo ler a celula no código VB

Question: x.Range("A1:z1").Select
x.Goto Reference:="R2C4"
verificaCel =     x.ActiveCell.FormulaR1C1

if verificacel then

Não estou conseguindo ler a célula especifica na planilha do excel, no qual vou enviar os dados para um banco de dados.
Comment: Para verificar algo vc deve colocar o que precisa ser verificado, exemplo `verificacel = "teste"`... dê um `debug.print verificacel` para verificar o valor que tem nessa variável.
Comment: Este valor que aparece na verificacel "=SUMIF(Matriz!C[6],RC[-4],MATRIZ!C[5])" ...e o que deveria aparecer é um valor "314,82"
Comment: Então ao invés de usar o `x.ActiveCell.FormulaR1C1` use `x.ActiveCell.Value`
Comment: Deu certo \o/\o/\o/\o/
Comment: Muito obrigado!!!
Comment: Como o problema foi resolvido, postei a resposta para marcar como resposta e deixar como referência para outros colegas.
Answer: Para ler o valor da célula deve-se usar .value ou .text se aplicável. Usando .FormulaR1C1 o resultado será a fórmula da célula. Segue correção abaixo:
verificaCel = x.ActiveCell.Value

Comment: Marcos marque como resposta clicando no "V" cinza ao lado da resposta para ficar verde. Isso significa que a resposta resolveu seu problema. Valeu
Comment: Obrigado Evert. Como dizem.. "simples assim" rs


Ssh use private key over 2 servers

Question: Machine1 has private and public pair.
Machine2 and machine3 have public key.
I go to machine2 from machine1 over ssh and then I want to go to machine3 from machine2.
It's like that machine1--ssh-->machine2--ssh-->machine3
How can I do that?
Answer: Lets clarify this: the user you are establishing these connections as has a public/private key pair. In ~/.ssh/authorized_keys on machine2 and machine3 there is a copy of the connecting users public key?
If this is the case then on machine1 you should be running an ssh-agent to cache the credentials for your key. Here's what it looks like when I set up an ssh agent and cache my key:
<tbielawa>@(expressomaker)[~] 04:15:33
$ eval `ssh-agent`
Agent pid 21725
<tbielawa>@(expressomaker)[~] 04:15:20
$ ssh-add
Enter passphrase for /Users/tbielawa/.ssh/id_rsa:
Identity added: /Users/tbielawa/.ssh/id_rsa (/Users/tbielawa/.ssh/id_rsa)
<tbielawa>@(expressomaker)[~] 04:15:31
$ ssh-add -l
2048 5f:e0:9b:92:e4:80:7e:5e:c8:29:00:29:ae:ca:bd:58 /Users/tbielawa/.ssh/id_rsa (RSA)

(You only need to run eval ssh-agent if one isn't running already)
Then when I make SSH connections I run the ssh command with the ForwardAgent option like this:
<tbielawa>@(expressomaker)[~] 04:17:58
$ ssh fridge -o "ForwardAgent yes"
Last login: Wed Nov  3 14:32:28 2010 from expressomaker

Except I don't do that all the time, my ~/.ssh/config file is configured to forward my agent automatically:
<tbielawa>@(fridge)[~] 04:18:03
$ cat .ssh/config
Host *
     ForwardAgent yes

If you run your ssh commands like this then your agent will be with you on server. You can then ssh to server3 using your public/private key combination.
Comment: A shorter form of -o "ForwardAgent yes" is simply to use -A.
Comment: @andol, I'm surprised I forgot to mention that. Nice addition! I guess I was thinking about the concept of agent forwarding in general.
Answer: Use ssh-agent on machine1 and agent forwarding.  There is a great guide from Steve Friedl which illustrates this in detail.
You might want to check out keychain, too.
Answer: If you want to login from one machine to another without a password, you need to use ssh-keygen tool to generate the key pair and then copy it to the destination machine.
The way to do it to create the key pair and then copy the public key to each server you want to have access. The file should be copied to /home/user/.ssh/authorized_keys. Of course, you have to replace /home/user with the real user home directory.
Here are required the steps:
1- Create key pair on machine1.
2- Copy the generated public key to machine2.
3- Create another key pair on machine2.
4- Copy the generated public key to machine3.
Comment: this isn't necessary, you can use ssh agent forwarding as mentioned in other answers.

Ssh use private key over 2 servers


Styling file upload button for simple_form_for with Bootstrap in Rails 3

Question: Using simple_form_for, Bootstrap and Rails 3. In a form:
<%= f.input :upload, label: 'PDF file:' , input_html: {accept: ('application/pdf') } %>
I don't know how I'd style this so that the "choose file" button can have a different class ('btn btn-primary').
Additionally, when using with Bootstrap at least, its severely misaligned by default. See attached image.
Finally, how do I redefine the text from "No file chosen" to "Chose file" when there isn't one added yet, and show the file name when there is one.
Answer: This is how I do it:

In the view add your form file field and hide it
Add a styled additional field just to display the file name
Add a button to trigger the file browse dialog

    <%= f.input :real_file, input_html: { hidden: true }, label: 'Upload Attachment' %>

      <input id="file-display" class="input-large uneditable-input" type="text">
       Browse

In your JS (Coffee w/ jQuery shown), pass the click from the display button onto the real file input and when they select a file drop the file name in the display text field (I drop the path so that I don't see C:\FakePath....)
$(document).ready ->

  # ------------------------------------------------------
  # pretty-fy the upload field
  # ------------------------------------------------------
  $realInputField = $('#real_file')

  # drop just the filename in the display field
  $realInputField.change ->
    $('#file-display').val $(@).val().replace(/^.*[\\\/]/, '')

  # trigger the real input field click to bring up the file selection dialog
  $('#upload-btn').click ->
    $realInputField.click()

Comment: My file input does not seem to get hidden, does anyone have any idea why?
Comment: Great answer. I have only one question: what does the 'uneditable-input' class does?
Comment: uneditable-input it's a bootstrap class that just disables the input field so that the only way to change the contents is via the file picker
Comment: @Wraithseeker, The hidden: true is overwritten by display: block of input[type="file"] when I did the inspect
input[type="file"] {
1. display: block;
}
[hidden], template {
2. display: none;
}
You can add a class inside input_html: {class: 'hidden-input'} and a more specific rule in you scss:
products.scss
// Product new/edit (ie. _form)
input[type="file"].hidden-input {
    display: none;
}
Comment: I had to do this (HAML): = f.input :pic, as: :hidden, input_html: { hidden: true, type: "file", class: "file required" } to hide the original file upload box. Also, to get it readonly the "uneditable-field" class didn't work for me so I added readonly: true. Works great!
Answer: This worked great for me and only requires HTML

  Add a file!

    <%= f.file_field :image, required: true, multiple: true, name: 'picture' %>

Answer: I ran across and am using Jasny's extension to Bootstrap 3.  It seems to work well so far.
Answer: No JS required, just plain css
scss
.fileinput-button {
  position: relative;
  overflow: hidden;
  float: left;
  margin-right: 4px;
  width: 110px;
  height: 32px;
  input{
    opacity: 0;
    filter: alpha(opacity=0);
    transform: translate(-300px, 0) scale(4);
    direction: ltr;
    cursor: pointer;
  }
}

html / slim
span class="btn btn-success fileinput-button"
  i.fa.fa-pencil
  span
   |  Select File
  = f.file_field :cover_ar

I recommend using compass for cross browser compatibility
Comment: You have two small typos in your CSS (one missing semicolon, one missing }), and for Safari support, it is helpful to add `-webkit-transform: translate(-300px, 0)scale(4);`. Other than that, great solution, thanks a lot!
Answer: As @rafaelfranca said you can't style file input but you can add your own button which will be clicking your hidden original button. See example here http://jsfiddle.net/rUdf2/6/
Answer: Every Browser has a different type of file input field button and this makes it a pain. You can play a little with css. This has given me a basic styling with JS without the annoying "No file chosen" text in chrome and Safary:
$(document).ready(function() {
  $(".your_button").css("width", "80px");
});

Otherwise the best solution is to hide it and show a fake one that intercepts the click:
http://duckranger.com/2012/06/pretty-file-input-field-in-bootstrap/
With respect to the question of how to show that a file has been uploaded, a basic solution with jquery file upload is to detect the upload complete event and replace some of your text with a success message (The exact file name I believe it is not possible to obtain with modern browsers):
$(".your_button").fileupload({
    dataType: "json",
    done: function(e, data) {
        $(".place_for_your_text").text("File uploaded.");
    }
});

In summary, a basic solution is to use javascript in your assets to:

Hide the annoying "No file chosen text" with css.
Place your "Chose file" text next to the button and give it a class you can reference.
Replace the text with "File uploaded"
Answer: No fancy shiz required:
HTML:
<form method="post" action="/api/admin/image" enctype="multipart/form-data">
    <input type="hidden" name="url" value="<%= boxes[i].url %>" />
    <input class="image-file-chosen" type="text" />

    <input class="btn image-file-button" value="Choose Image" />
    <input class="image-file hide" type="file" name="image"/>

    <input class="btn" type="submit" name="image" value="Upload" />

</form>

JS:
$('.image-file-button').each(function() {
      $(this).off('click').on('click', function() {
           $(this).siblings('.image-file').trigger('click');
      });
});
$('.image-file').each(function() {
      $(this).change(function () {
           $(this).siblings('.image-file-chosen').val(this.files[0].name);
      });
});

CAUTION: The three form elements in question MUST be siblings of each other (.image-file-chosen, .image-file-button, .image-file)
Answer: If you are using Bootstrap, Simple Forms, Jasny, and ReFile, this post may be of interest to you refile simple_form undefined method attachment_field
Answer: <label class="btn btn-primary"
<%= f.input :upload, label: 'PDF file:',
class:"hidden", input_html: {accept: ('application/pdf') } %>

Works for me
Using class="hidden"
PS: I'm using Tailwind CSS


How are the Imperial March intro harmonies best interpreted on piano?

Question: I'm wondering how the below is most effectively performed on piano. I've solved the 'rhythm problem' (i.e., the very rapid triplet repetitions) by using both hands via piano 'split' feature — each playing identical pitches but on different keys — but am not sure how to handle the 5 pitch classes shown in the 2nd measure. There are 4 classes crammed into the span of a major 3rd! My novice reasoning says the G in the bass is essential, and that - because 3 of the 4 remaining classes are also present at the end of measure 1 - the A should also probably be played to further distinguish the two measures. And... that's as far as I get. This is my first attempt at such a task on the piano, and I'd be grateful for any guidance...
(I've 'condensed' the below with copyright issues in mind; only the last beat of the 2nd measure is shown. I'm aware that some dotted notes appear undotted, but I'm all out of dots...)
Edit: because of the machinegun-esque rapidity of the triplets, I need both hands playing identical notes (have 'split' my piano so that e.g. the viola's 1st note may be played on 2 widely-separated keys)
Comment: I'm not seeing "4 classes crammed into the span of a major 3rd" - I'm seeing A, B flat, E flat, and F sharp, all in the span of a major 6th instead. The G's are far enough from the A's that the impression is that they're not as cramped.
Comment: @Dekkadeci always happy to be put right about this stuff; I saw (very possibly erroneously) classes F#, G, A, and Bb all lying 'between' classes F# and Bb (incl.), and understood classes ignored difference-by-octave...
Answer: Given the goal of playing the same pitches in each hand (with one portion of the keyboard octave-transposed to match the other), I would voice the chord as shown below. The bass G is, as surmised, necessary, as is the F# on top, being the highest — and therefore most aurally prominent — pitch. The Eb I would also keep, because it carries over from the previous chord in the parallel position.
The A I would leave out, because it adds the least to the sound, muddies the G, and is physically the most awkward to include.
The Bb (in red) is in parentheses. Play it if possible, but it can be dropped if one or both hands has difficulty reaching it. Note that in the case of one hand playing it comfortably, the chord repetition is so quick, that its absence in the other hand would probably not be noticed.

FWIW: given the option of playing the complete chord ...
I would play the chord as notated below: A G octave in the left hand, and the chord A-Bb-Eb-F# in the right.

The contrabass sounds an octave lower than written, so the bass and cello Gs are an octave apart, which accounts for the left hand.
The viola and violin I are playing the A-F# sixth in unison, with the violin II playing the Bb-Eb fourth.
It's true that if one reorders the pitches, the F#, G, A, and Bb all fit within a (enharmonic) major third, but that isn't a concern in this case. The notes are actually spread out in a way fairly convenient for translation to the piano.
Comment: thanks for your answer - I have edited the question to note a restriction that I failed to spell out explicitly | edit: I'm sorry you expended this effort only to make me realize the incompleteness of my Q!
Comment: @darthritis Could you say more about why you need both hands playing the same notes. Do you mean exactly the same — same octave? Do you alternate hands to get the rapid-fire effect?
Comment: The need for both hands to play exactly the same notes (albeit using different keys) exists because at the indicated tempo (not depicted) of 'crotchet = 112', the triplets need to be sounded at a rate of I think ~11/second - physically impossible for me (see username!). I have set up my digital piano so that the pitches of the keys in its left ~half are identical to the pitches of the keys in its right ~half; e.g. pressing middle C causes C1 to sound. I use the right hand for the measures' 3rd, 5th, 8th, 10th, and 12th notes, and the LH for the remainder...
Comment: @darthritis Okay, just to make sure I'm clear, you're effectively alternating hands on exactly the same pitches, but by virtue of your keyboard's split, those identical pitches occur on two different set of keys. Yes?
Comment: Absolutely correct. I've found 0 piano performances of the March that fully reproduce this rhythm, and am hoping to come up with something more faithful (at least until the well-known melody begins in measure 5)...
Comment: @darthritis I think I've got it. Much appreciated if you'd try it out and let me know how it goes.
Comment: Very grateful, @Aaron. Can I ask why you deem the F# necessary? Am guessing it's due to a (possibly less-strict) counterpart of the "no inversions!" rule governing treatment of the score's bass note. I'm also still hung up on the A (muddy and awkward as it is) and whether the contrast it creates with the harmony established in the 1st 7 beats (and the tension it imparts 0.5s before melody begins) can be done away with. Maybe this contrast is simply a factor that must be laid aside in this case given the restriction to one hand, but that could potentially be accommodated in other contexts(?)
Comment: @darthritis I've updated my answer to explain the F#. The issue with the A is that it's the least hearable pitch at that moment in the piece. If you listen to an orchestral recording, you'll hear the G, the F#, and probably the Eb, but you'd be challenged to pick out the A. If you really want the A present, then leave out the Bb and play the left-hand chord 5-4-2-1 and the right-hand chord 1-1-3(or 4)-5. (1-1 means the thumb plays both G and A.)
Comment: Perfect - thank you so much. Glad I'm not alone when it comes to hearing the A (which I am now finally comfortable omitting). I'll also have to add another Eb atop the triad I play in bar 1 after your update...


Interpretation of (scale of) AIC, AICc and BIC when comparing different models

Question: I'm trying to fit a model to a time series, but I am pretty confused as to which is the best.
I'm looking at an arima model, and ets model and an stlf model, which each performed best within their own family of models. When comparing rolling forecasting errors for 6 month forecasts, they perform exactly equally well, each model has the smallest errors exactly one third of the time.
I then try to look at other criteria such as AIC, AICc and BIC, and get the following results (my problem is really the scale of the information criteria - it's about a factor hundred smaller for the stlf model, is it really that much better or is something else at play here?):
<code>#The arima model:
Series: myts
ARIMA(0,1,0)(1,0,0)[12]
Coefficients:
        sar1
      0.8394
s.e.  0.0704
sigma^2 estimated as 19456:  log likelihood=-229.81
AIC=463.61   AICc=463.99   BIC=466.72

#The ets model:
ETS(M,N,M)
Call:
 ets(y = myts)
  Smoothing parameters:
    alpha = 0.5505
    gamma = 1e-04
  Initial states:
    l = 500.5273
    s=0.5977 0.3134 0.298 0.5218 1.6367 2.0899
           2.1506 2.2123 0.8724 0.5279 0.4086 0.3708

  sigma:  0.1507

     AIC     AICc      BIC
438.9330 458.9330 461.1023

#The stlf model:
 ETS(A,N,N)
Call:
 ets(y = x, model = etsmodel)
  Smoothing parameters:
    alpha = 0.483
  Initial states:
    l = 6.0707
  sigma:  0.1587
      AIC      AICc       BIC
0.4533825 0.8170189 3.6204204
</code>
Can they be compared at all? I do think I remember something about only being able to compare these criteria between different models under certain conditions.
Comment: Sometimes average AIC (AICc, BIC) is reported: the usual AIC is divided by the number of observation. Check out if multiplying the AIC (AICc, BIC) from `stlf` by the number of observation will not bring it somewhere close to 400-500 as for the other two methods. Anyhow, I would side with @StephanKolassa in that you should be careful comparing AIC from different algorithms. But sometimes you can track the way AIC is calculated for different algorithms, then there is a chance you could adjust it (if necessary) and compare.
Answer: You can't compare information criteria between different fitting methods. AIC and friends involve a constant that different fitting algorithms set to different values. You can compare AICs for different models fitted by the same method. So no help there.
Looking at rolling out-of-sample forecasts was already exactly the right thing to do. Now you know that each model is best one third of the time. You could now also look at the magnitude of the errors (MAD or MSE) - perhaps one model sometimes yields very low, sometimes very high forecasts.
Failing that, it may well be that all three methods are equally good.
One smart trick to improve forecast accuracy is: calculate forecasts from all three methods and average them within each future time bucket. Averaging forecasts, in particular from "very different" methods, almost always improves accuracy and also reduces error variance.
Comment: If your separate methods use normals for their predictive distributions, then "pooling" them amounts to creating a *mixture* of normals (google that - mixtures are implemented in R and elsewhere), with the point forecast = the expectation of the mixture = the average of the expectations of the components. If your component methods use different predictive distributions (e.g., a normal and a negbin), then your mixture may not be tractable analytically, and you may need to resort to simulation to obtain prediction intervals.
Comment: (Incidentally, note the difference between a *prediction* interval, which refers to an interval that contains a future *observable* realization or not, and a *confidence* interval, which contains an *unobservable* parameter or not.)
Comment: Thanks for clarifying that! How do you go about calculating the variance and confidence intervals of the average of several methods?
Comment: I think your answer is imprecise. It says you cannot compare AICs across different fitting methods, the reason being that different methods / software functions calculate AIC values differently (some calculate a modified AIC value). A precise statement should be: for comparability across methods, it is sufficient to use the same definition of AIC. Did I get you right?
Comment: @RichardHardy: you are right that my answer is imprecise. One aspect I did not mention here is that different R functions or different software package optimize different objective functions in fitting. For instance, `Arima()` can optimize the likelihood (`method="ML"`), minimize the constrained sum of squares (`"CSS"`) or a combination (`"CSS-ML"`). If different criteria are used, it makes little sense to compare likelihoods and likelihood-based information criteria.
Comment: Well, then could you edit your answer to make it more precise? The answer has been referenced and misinterpreted in another thread, this is why I came back here to clarify.
Comment: Regarding models that are estimated by optimizing something else than likelihood and whether it makes sense to use likelihood-based criteria there, I believe that under suitable regularity conditions the AIC still estimates the out-of-sample likelihood. Hence, it can be used for model comparison as long as we are interested in finding a model with the highest out-of-sample likelihood. Regardless of how the model is built and estimated, we are allowed to evaluate its performance. In fact, we should do that whenever we need an indication of how well the model is going to perform on a given task.