Rationale for taking second derivative in least squares estimation?

Question: I am reading DeGroot and he talks about how to derive the b0 and b1 coefficients using LS estimation.  I understand everything except the last part where he talks about taking the second derivative of the cost function.  What is the rationale for doing this?  Is this to check if we are at minimum or maximum or is there more to it than that?  I thought the cost function is convex.
Comment: Or a saddle point...just any other point that has zero gradient but isn't the minimum
Comment: @confused: There's a theorem that says that, if you find a point , $\beta^{*}$, of a function where the gradient of the function equals zero, then,  if the hessian of the function is positive definite at that point $\beta^{*}$,  then $\beta^{*}$ is a global minimum. In your case, the  "hessian" is just the second derivatives ( and the partials ) of $\beta_{0}$ and $\beta_1$
Comment: It's to prove that the spot you've found is indeed a minimum.
Comment: As opposed to...it being a maximum?
Comment: Wow, thanks did not know that!  Did not know there's a way to find a global minimum just by looking at the hessian.  I'm guessing that's a pro of finding analytical results as opposed to doing something numerical like gradient descent cuz I recall there's a lot of times when we are unable to find a global minimum.
Comment: In general it doesn't guarantee that you find a global minimum, but here there is only one solution to the normal equations and it's a minimum, so it does have to be *the* minimum.
Answer: Recall from single-variable calculus that th derivative of a function being zero does not guarantee that point to be a minimum. Indeed, the point could be a maximum (e.g., $f(x)=-x^2$) or neither a maximum nor a minimum (e.g., $f(x)=x^3$). The second derivative shows us if the point is a minimum or not.
There is similar logic in multivariable calculus related to the second and mixed partial derivatives in the Hessian matrix.
Therefore, just because we found a point where the loss function has a derivative of zero does not mean that the point must be a minimum. Checking the higher-order derivatives in the Hessian matrix confirms that the point is indeed the desired minimum.
Answer: There are first and second order conditions of minimum of the function at a point $x$:
$$F.O.C.: f'(x)=0\\S.O.C.: f''(x)>0$$
It's in basic calculus curriculum in high schools.
Comment: I believe the situation is subtler than this: the case of deficient-rank design matrices is one where the second derivative is only positive *semi* definite--and that does not assure a minimum has been reached.


How to iterate a column to get the sub list in sqlalchemy?

Question: I would like to acquire the sub list from a table like this using sqlalchemy:
Column1    Column2
   a          1
   a          2
   b          1
   b          2

and firstly get
Column1    Column2
   a          1
   a          2

and secondly get
Column1    Column2
   b          1
   b          2

Can I realize it by executing the query language once?
Answer: You can do it with a single query, but you need to perform the grouping in Python. To do that you could use itertools.groupby to process the query results which you would order by (column1, column2). Here is an example:
from itertools import groupby
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()
class Stuff(Base):
    __tablename__ = 'stuff'
    id = Column(Integer, primary_key=True)
    column1 = Column(String(10))
    column2 = Column(String(10))

    def __repr__(self):
        return '({}, {})'.format(self.column1, self.column2)

db_url = 'sqlite:////tmp/test.db'
engine = create_engine(db_url)
Base.metadata.bind = engine
session = sessionmaker(bind=engine)()

for k, g in groupby(session.query(Stuff).order_by(Stuff.column1, Stuff.column2),
                    key=lambda stuff: stuff.column1):
    print('{}: {}'.format(k, ','.join(stuff.column2 for stuff in g)))

If your table contains this data:

Column1    Column2
   a          1
   a          2
   b          1
   b          2
   a          3

the output would be:

a: 1,2,3
b: 1,2

To process the data replace the print() in the for loop with your processing code.


How to make the images in the GalleryView come to initial position after completion of Scroll?

Question: I will consider a simple GalleryView which consists of few images.I will scroll from left to right or vice versa and then leave. As of now (as shown in the 2 images below) the gallery stops in the middle of the view, leaving some spaces (marked red colour in the image) from the initial position or from the final position whenever the scrolling is complete. My concern is, I want my gallery to acquire the initial position or final position (scroll dependent) after scrolling. How would I do that?
Images:
When left to right scrolling is completed:

When right to left scrolling is completed:

My GalleryView:
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mAudio =(Button)findViewById(R.id.button1);
    mVideo =(Button)findViewById(R.id.button2);
    mImages=(Button)findViewById(R.id.button3);

    mAudio.setOnClickListener(this);
    mVideo.setOnClickListener(this);
    mImages.setOnClickListener(this);

    Gallery ga = (Gallery) findViewById(R.id.Gallery01);
    galleryAdapter = new ImageAdapter(this);
    galleryAdapter.setResourseArray(image_pics);
    ga.setAdapter(galleryAdapter);

    imageView = (ImageView) findViewById(R.id.ImageView01);
    ga.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
            Toast.makeText(getBaseContext(), "You have selected picture " + (pos + 1) + " of Gallery", Toast.LENGTH_SHORT)
            .show();
            if(activeGallery==AUDIO){
                imageView.setImageResource(audio_pics[pos]);
            }else if(activeGallery==VIDEO){
                imageView.setImageResource(video_pics[pos]);
            }else if(activeGallery==IMAGE){
                imageView.setImageResource(image_pics[pos]);
            }
        }
    });
}

@Override
public void onClick(View view) {
    if (view == mAudio) {
        if (galleryAdapter != null) {
            galleryAdapter.setResourseArray(audio_pics);
            galleryAdapter.notifyDataSetChanged();
            activeGallery=AUDIO;
        }
    } else if (view == mVideo) {
        if (galleryAdapter != null) {
            activeGallery=VIDEO;
            galleryAdapter.setResourseArray(video_pics);
            galleryAdapter.notifyDataSetChanged();
        }
    } else if (view == mImages) {
        if (galleryAdapter != null) {
            activeGallery=IMAGE;
            galleryAdapter.setResourseArray(image_pics);
            galleryAdapter.notifyDataSetChanged();
        }
    }
}

public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;
    private int[] resourseArray = null;

    public ImageAdapter(Context c) {
        ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
        imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
        ta.recycle();
    }

    @Override
    public int getCount() {
        return resourseArray.length;
    }

    public int[] getResourseArray() {
        return resourseArray;
    }

    public void setResourseArray(int[] resourseArray) {
        this.resourseArray = resourseArray;
    }

    @Override
    public Object getItem(int arg0) {

        return arg0;
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        ImageView iv = new ImageView(ctx);
        iv.setImageResource(resourseArray[position]);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
        iv.setBackgroundResource(imageBackground);
        return iv;
    }

}

To be simple, I want the elastic effect to these gallery views.
Comment: see this http://stackoverflow.com/questions/3615140/android-gallery-image-position-problem also http://stackoverflow.com/questions/4341158/android-align-first-item-in-gallery-to-the-left and http://groups.google.com/group/android-developers/browse_thread/thread/1d69e05d6238d030?pli=1
Comment: you want to remove those spaces? or you want the Gallery to show the first image after you stop scrolling?
Comment: Thanks Frankenstein for your valuable comment.I want to remove those spaces.
Comment: I am trying to do with one method which is given there,do you know how to use setSelection()?
Comment: not much...but as gallery show selecte in Middel...you need to set selected image means 2nd 3rd so the first will be at extreme left..setSelection is for that..
Answer: I know only how to remove space from left side and image will start from left.
A structure describing general information about a display, such as its size, density, and font scaling.To access the DisplayMetrics members, initialize an object like this
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    Gallery g = (Gallery) findViewById(R.id.gallery);

    // set gallery to left side
    MarginLayoutParams mlp = (MarginLayoutParams) g.getLayoutParams();
    mlp.setMargins(-(metrics.widthPixels / 2 + (imageWidth/2)), mlp.topMargin,
                mlp.rightMargin, mlp.bottomMargin);


Setting dynamic inventory hostnames from Ansible

Question: I'm setting up a bunch of up to 150 temporary EC2 servers for a hands-on tutorial session.
I'm successfully creating EC2 inventory dynamically and running roles against the instances that are created to configure everything, but I need to set a straightforward hostname for each one. To that end I have a file that contains a simple list of names that I want to use for the hostnames. This is in my playbook:
---
- hosts: localhost
  connection: local
  gather_facts: false

  tasks:
    - name: Provision a set of instances
      ec2:
        key_name: ubuntu
        instance_type: t2.micro
        image: "{{ ami_id }}"
        wait: true
        exact_count: {{ server_count }}
        count_tag:
          Name: Tutorial
        instance_tags:
          Name: Tutorial
        groups: ['SSH', 'Web']
      register: ec2

    - name: Add all instance public IPs to host group
      add_host: hostname={{ item.public_ip }} groups=ec2hosts
      loop: "{{ ec2.instances }}"

    - name: Set a host name for each instance in DNS
      route53:
        zone: {{ tutorial_domain }}
        record: "name.{{ tutorial_domain }}"
        state: present
        type: A
        ttl: 120
        value: {{ item.public_ip }}
        wait: yes
      loop: "{{ ec2.instances }}"

It really comes down to that record: "name.{{ tutorial_domain }}" line - how can I look up a name in my list of names and use it as a hostname, turning name into {{ some_dynamic_name }}?
I have seen the lookup plugins, but they all seem to be focused on looping over the entire contents of some external file - but I'm already looping over the list of servers, and that list may be shorter than the list of names (e.g. I may have only 10 servers). Ideally I want to read the list of names into an array once, then use the index from the server loop to pick the name (i.e. so the 3rd server would get the 3rd name). How do I do that in ansible? Or is there a better approach?
Answer: You can use the zip filter to combine your list of instances with a list of names, like this:
---
- hosts: localhost
  gather_facts: false
  vars:
    tutorial_domain: example.com
    ec2:
      instances:
        - public_ip: 184.108.40.206
        - public_ip: 126.96.36.199

    names:
      - blue-duck
      - red-panda

  tasks:
    - debug:
        msg:
          route53:
            zone: "{{ tutorial_domain }}"
            record: "{{ item.1 }}.{{tutorial_domain}}"
            state: present
            type: A
            ttl: 120
            value: "{{ item.0.public_ip }}"
            wait: yes
      loop: "{{ ec2.instances|zip(names)|list }}"

In older versions of Ansible, you would accomplish the same thing with a with_together loop.
Comment: Thank you. This works nicely. A hitch I've since run into is that if I want to add more instances later, the host names will restart from the beginning, so I guess I would need to calculate an offset into the names array to skip existing hosts.
Answer: Another approach would be to use loop index to loop over multiple lists. This way, it will be easier to add offset in the list.
- set_fact:
    ips: ['184.108.40.206', '184.108.40.206']
    hostnames: ['host1', 'host2']
- debug:
    msg: "ip={{ ips[index] }}, hostname={{ hostnames[index] }}"
  loop: "{{ ips }}"
  loop_control:
    index_var: index


Incorrect syntax near SET in SQL Server 2000 stored procedure

Question: I generated a SQL script from SQL Server 2000 because I can't just use the .bak file to restore into version 2014. When I tried to execute the generated SQL script on the new SQL Server 2014, I get an error

Incorrect syntax near the keyword 'SET'

Checking the blamed line:
IF ISNULL(@pi_strMChargeNo,'') = '' BEGIN
    INSERT INTO TPURCHASEGROUP(MCHARGENO, CASHPRICE, COUPONPRICE, EVENTPRICE, TOTALPRICE, PITEMCNT, REGDATE, UPDDATE)
    VALUES(@v_strMChargeNo, 0, 0, 0, 0, 1, GETDATE(), GETDATE())

    IF @@ERROR <> 0
    BEGIN
        SET @po_strErrMsg = 'TPURCHASEGROUP INSERT FAIL'
        SET @po_intRetVal = 2266
        SET RETURN -- blamed
    END
END

Might be changes to the syntax from 2000 to 2014, but what? Or possibly just delete the line?
Answer: Without further context, it looks like you should replace the line SET RETURN by RETURN.
Regarding your problem with restoring a backup. You might want to try restoring on SQL Server 2008, increase the compatibility level of the database to 100, make a new backup and restore that under SQL Server 2014.
Comment: Nice tip :) Will try. Though I still would like to have a SQL script. I'll try to replace `SET RETURN` with `RETURN`.
Comment: yup, replacing those with just `RETURN` fixed the problem. Though a few others come up. But not related to this topic anymore. Accepted.


What could be causing 30+ Apache processes consuming all memory?

Question: I have a VPS which hosts a site (http://www.tanguay.info/web) which gets about 1800 unique visitors per day.
The site ran fine getting 3-second response times for weeks.
All of a sudden on Friday at 4PM the site became inaccessible with response times at 30+ seconds and has been down ever since. Here is the pingdom graph:
alt text http://tanguay.info/web/external/pingdomdown.png
Even after rebooting it numerous times, I cannot get response times back below 30 seconds. With the "top" command I see that there are 30+ Apache processes running which seem to be taking up over 90% of the memory.
What could be causing this?  Why would there be so many Apache processes open, especially since the site is basically inaccessible now anyway? Could it be that these processes are simply not getting closed somehow?
alt text http://tanguay.info/web/external/tooManyApacheProcesses.png
Answer: You're on a Virtuozzo/OpenVZ-based virtual machine (based on the top screenshot you posted).  Something else is probably hammering the machine that you're on, since that top output doesn't really show anything abnormal (17MB RES for an Apache process is quite reasonable).  I'd talk to your hosting provider about not overloading their VM servers quite so much, or find a better VM provider.
Comment: No swap, no buffers, no cache.  I'm not aware of any other virtualisation system that makes that much of a hash of the VMM stats.
Comment: huh?  How did you determine that they are on a Virtuozzo/OpenVZ-based virtual machine, based off of top? (just curious.)
Answer: Static content do not suffer, the problem is probably in database. Inspect MySQL slow querires log at /var/log/mysql/mysql-slow.log. Apache mod_status also maybe useful, it will show status page like this.
Answer: Seeing all the processes marked with a D, I'm thing that the disk sub system may have been overloaded.


Canvas doesn't follow actual position of mouse

Question: i followed Draw on HTML5 Canvas using a mouse to  draw free share in convas, provided code snippet is not working properly, but when i try to use, it it not working properly.  I mean position of cursor is different than in canvas.
Screenshot

and here is code snippets

function init() {
  canvas = document.getElementById("whiteboard-canvas");
  ctx = canvas.getContext("2d");
  w = canvas.width;
  h = canvas.height;

  canvas.addEventListener(
    "mousemove",
    function (e) {
      findxy("move", e);
      console.log("on mouse move", e);
    },
    false
  );
#othere mouse event handler goes here..
}

function draw() {
  ctx.beginPath();
  ctx.moveTo(prevX, prevY);
  ctx.lineTo(currX, currY);
  ctx.strokeStyle = x;
  ctx.lineWidth = y;
  ctx.stroke();
  ctx.closePath();
}

function findxy(res, e) {
  if (res == "down") {
    prevX = currX;
    prevY = currY;
    currX = e.clientX - canvas.offsetLeft;;
    currY = e.clientY - canvas.offsetTop;;

    flag = true;
    dot_flag = true;
    if (dot_flag) {
      ctx.beginPath();
      ctx.fillStyle = x;
      ctx.fillRect(currX, currY, 2, 2);
      ctx.closePath();
      dot_flag = false;
    }
  }
  if (res == "up" || res == "out") {
    flag = false;
  }
  if (res == "move") {
    if (flag) {
      prevX = currX;
      prevY = currY;
      currX = e.clientX - canvas.offsetLeft;;
      currY = e.clientY - canvas.offsetTop;;
      draw();
    }
  }
}

My code snippets are almost identical as in the link.
Comment: First of, JS comments don't start with `#` - thats invalid and you should notice that immediately when you start up your console in the browser. Secondly, from you snippet you I can't see anything inhereently wrong, except that `ctx.fillStyle = x;` makes no sense, `x` is usually a coordinate, so using it as a fill makes no sense but since I don't know where `x` is coming from, I can't say if that's causing thee issue. In short, add your variable definitions to this post as well (including `canvas`, `ctx`, etc...), as well as turning this into a SO snippet (its the `<>` document icon).
Comment: You must scale the mouse coordinates (which is in CSS pixels)  to match the canvas resolution (canvas pixels). The example does not contain the code to do that and there are many caveats. Most basic is `canvasPixelX = currX * (canvas.width / canvas.getBoundingClientRect().width * devicePixelRatio)` and same for height. see https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect for correct use` getBoundingClientRect`and https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio for devicePixelRatio
Comment: @Blindman67 Thats not true, `offsetX` provides the correct value, no need for scaling.
Comment: @somethinghere `offsetX` and Y gives the top left corner, it does not provide any information on the size of canvas pixels compared to CSS pixels. Canvas resolution is independent of the canvas display size. In default state canvas coordinates are in canvas pixels not CSS pixels. If OP has a retina (or one of many HD devices ) display `devicePixelRatio` must also be used to scale correctly
Comment: You are getting off in the woods here man. The issue is not that. There is no indication this has anything to do with DPI. You're making the problem unnecessarily complicated. With no indicators of retina, all of this matches up. And even then, you can solve that with a simple context.scale() and forget anout it altogether. This guy is just starting out, defo no retine problem.
Comment: @somethinghere sorry of some issue, actually I'm python developer, that why, By mistakenly i added # for comment only in this question. Actually my problem was scaling, My actual canvas size is `width:300 Height: 150`, and also this is the actual reason behind facing problem. I've used css to increase height and width of canvas to match div size.  Is there any way to fix this problem.
Comment: @somethinghere value of x is coming from another function, which i 've included here, due to limitation of stackoverflow. I hope you understand.
Comment: Set the width and height of the canvas directly with camvas.width and csnvas.height. Though again, since you use offset it isn't necessarily to do with scaling. Your csnvas might not fill the screen but it should still work.
Answer: CSS to canvas pixel coords
Canvas resolution, measured in pixels, and canvas page size, measured in CSS pixels, are independent.
When using the 2D API rendering is done in canvas pixels (not CSS pixels)
The mouse event holds coordinates as CSS pixels and as such will not always directly translate to canvas pixel coordinates.
One can get the page size of the canvas using getBoundingClientRect which can be used to scale from CSS pixels to canvas pixels.
However getBoundingClientRect includes the element's padding and border which must also be considered when scaling the CSS pixels to canvas pixels.
Note in my comments I talked about the device pixel ratio. I was wrong in my assessment of it's use, it is not required in this situation
Example
The example uses a default canvas (300 by 150px) and then uses CSS rules to scale it to the page, adding padding and a border in units other than CSS pixels. The canvas is also scaled such that it can be scrolled up and down.
Switch between the full page and snippet to see how scaling effects the example.
The function getInnerRect uses getBoundingClientRect and getComputedStyle to get the bounds of the canvas on the page in CSS pixels. From the top left of the top left pixel on the canvas to the bottom right of the bottom right most pixel.
The draw function then uses the inner rect (named bounds in example) to calculate the scales (x, y). Then offsets the mouse coordinates and scales them to draw to the canvas.

Note That the function mouseEvent captures the mouse coordinates from event.pageX and event.pageY

Note The function getBoundingClientRect is relative to the page scroll position. Thus when using it's return one must adjust the coordinates to take in acount the page scroll position. The example uses scrollX and scrollY to do this.

Note that the scale and offset can also be encapsulated as a 2D transformation. As the coordinates in the example will only change when the page is resized the transform can be calculated once on resize and applied to the canvas. Thus the draw function can use the CSS pixels to render directly to the canvas. This make the code in the example a little simpler. (See second snippet);

Note That because the transform will transform all rendering sizes it is necessary to compute the inverse scale of the transform. This is used to scale the line width in the second example. If this is not done then the line width rendered will change as the page size changes.

const ctx = canvas.getContext("2d");
var bounds;
const mouse = {x:0,y:0,b:0,px:0,py:0}; // p for previouse b for button
function mouseEvent(event) {
    mouse.px = mouse.x;
    mouse.py = mouse.y;
    mouse.x = event.pageX;
    mouse.y = event.pageY;
    if (event.type === "mousedown") {
        mouse.b = true;
        canvas.classList.add("drawing");
    } else if (event.type === "mouseup" || event.type === "mouseout") {
        mouse.b = false;
        canvas.classList.remove("drawing");
    }
    draw();
}
addEventListener("mousemove", mouseEvent);
addEventListener("mousedown", mouseEvent);
addEventListener("mouseup", mouseEvent);
addEventListener("mouseout", mouseEvent);

const CSSPx2Number = cssPx => Number(cssPx.replace("px",""));
function getInnerRect(element) {
    var top, left, right, bottom;
    const bounds = element.getBoundingClientRect();
    const canStyle = getComputedStyle(element);

    left  = CSSPx2Number(canStyle.paddingLeft);
    left += CSSPx2Number(canStyle.borderLeftWidth);

    top  = CSSPx2Number(canStyle.paddingTop);
    top += CSSPx2Number(canStyle.borderTopWidth);

    right  = CSSPx2Number(canStyle.paddingRight);
    right += CSSPx2Number(canStyle.borderRightWidth);

    bot  = CSSPx2Number(canStyle.paddingBottom);
    bot += CSSPx2Number(canStyle.borderBottomWidth);

    return {
        top: bounds.top + top  + scrollY,
        left: bounds.left + left  + scrollX,
        bottom: bounds.bottom + bot + scrollY,
        right: bounds.right + right + scrollX,
        width: bounds.width - left - right,
        height: bounds.height - top - bot,
    };

}
function resizeEvent() {
    bounds = getInnerRect(canvas);
    widthText.textContent = "Canvas page size: " + bounds.width.toFixed(1) + " by " + bounds.height.toFixed(1) + "px Canvas width: " + canvas.width + " by " + canvas.height + "px";
    ctx.lineWidth = 3;
    ctx.strokeStyle = "black";
    ctx.lineCap = ctx.lineJoin = "round";
}
addEventListener("resize",resizeEvent);
resizeEvent();

function draw() {
    if (mouse.b) {
        const xScale = canvas.width / bounds.width;
        const yScale = canvas.height / bounds.height;

        const x = (mouse.x - bounds.left) * xScale;
        const y = (mouse.y - bounds.top) * yScale;
        const px = (mouse.px - bounds.left) * xScale;
        const py = (mouse.py - bounds.top) * yScale;

        ctx.beginPath();
        ctx.lineTo(px, py);
        ctx.lineTo(x, y);
        ctx.stroke();
    }
}
canvas {
    position: absolute;
    top: 5%;
    left: 10%;
    width: 80%;
    height: 170%;
    border: 1pc solid blue;
    padding: 1%;
    cursor: crosshair;
}
.drawing { cursor: none; }
.info {
   position: absolute;
   font-family: arial;
   font-size: x-small;
   pointer-events: none;
   text-align: center;
   background: white;
   border: 1px solid black;

}
#widthText {
  top: 2%;
  left: 20%;
  width: 60%;
}

<canvas id="canvas"></canvas>

Using transform

const ctx = canvas.getContext("2d");
const matrix = [1,0,0,1,0,0];
const mouse = {x:0,y:0,b:0,px:0,py:0}; // p for previouse b for button
function mouseEvent(event) {
    mouse.px = mouse.x;
    mouse.py = mouse.y;
    mouse.x = event.pageX;
    mouse.y = event.pageY;
    if (event.type === "mousedown") {
        mouse.b = true;
        canvas.classList.add("drawing");
    } else if (event.type === "mouseup" || event.type === "mouseout") {
        mouse.b = false;
        canvas.classList.remove("drawing");
    }
    draw();
}
addEventListener("mousemove", mouseEvent);
addEventListener("mousedown", mouseEvent);
addEventListener("mouseup", mouseEvent);
addEventListener("mouseout", mouseEvent);

function draw() {
    if (mouse.b) {
        ctx.beginPath();  // using CSS pixels
        ctx.lineTo(mouse.px, mouse.py);
        ctx.lineTo(mouse.x, mouse.y);
        ctx.stroke();
    }
}

const CSSPx2Number = cssPx => Number(cssPx.replace("px",""));
function getInnerRect(element) {

    var top, left, right, bottom;
    const bounds = element.getBoundingClientRect();
    const canStyle = getComputedStyle(element);

    left  = CSSPx2Number(canStyle.paddingLeft);
    left += CSSPx2Number(canStyle.borderLeftWidth);

    top  = CSSPx2Number(canStyle.paddingTop);
    top += CSSPx2Number(canStyle.borderTopWidth);

    right  = CSSPx2Number(canStyle.paddingRight);
    right += CSSPx2Number(canStyle.borderRightWidth);

    bot  = CSSPx2Number(canStyle.paddingBottom);
    bot += CSSPx2Number(canStyle.borderBottomWidth);

    const canvasInner = {
        top: bounds.top + top + scrollY,
        left: bounds.left + left  + scrollX,
        bottom: bounds.bottom + bot + scrollY,
        right: bounds.right + right + scrollX,
        width: bounds.width - left - right,
        height: bounds.height - top - bot,

    };
    const xScale = canvas.width / canvasInner.width;
    const yScale = canvas.height / canvasInner.height;

    // use mean of x,y scale to get inverse scale
    canvasInner.invScale = 1 / ((xScale + yScale) / 2);

    // set scale
    matrix[0] = xScale;
    matrix[3] = yScale;

    // set offset in canvas pixels
    matrix[4] = -canvasInner.left * xScale;
    matrix[5] = -canvasInner.top * yScale;

    ctx.setTransform(...matrix);
    return canvasInner;

}
function resizeEvent() {
    const bounds = getInnerRect(canvas);
    widthText.textContent = "Canvas page size: " + bounds.width.toFixed(1) + " by " + bounds.height.toFixed(1) + "px Canvas width: " + canvas.width + " by " + canvas.height + "px";
    ctx.lineWidth = 3 * bounds.invScale;  // correcting line width for render
    ctx.strokeStyle = "black";
    ctx.lineCap = ctx.lineJoin = "round";
}
addEventListener("resize",resizeEvent);
resizeEvent();
canvas {
    position: absolute;
    top: 5%;
    left: 10%;
    width: 80%;
    height: 170%;
    border: 1pc solid green;
    padding: 1%;
    cursor: crosshair;
}
.drawing { cursor: none; }
.info {
   position: absolute;
   font-family: arial;
   font-size: x-small;
   pointer-events: none;
   text-align: center;
   background: white;
   border: 1px solid black;
}
#widthText {
  top: 2%;
  left: 20%;
  width: 60%;
}

<canvas id="canvas"></canvas>

Comment: Awesome explanation, Thank you


How do I get the total number of iOS devices by generation from Microsoft InTune?

Question: I'm new to Microsoft InTune and I want to be able to determine how many iOS devices I have enrolled by generation (4th, 5th, 6th, etc). I've looked and it doesnt seem straightforward.
Comment: Can't you do that with the standard reporting features ? https://learn.microsoft.com/en-us/mem/intune/fundamentals/reports#all-devices-report-operational  - Choose Columns to provide more device details for the report. This report provides filtering, searching, paging, and sorting capabilities
Comment: I appreciate the answer but i was looking for something that summarizes this information. Like...

iPad 5th Gen: 50
iPad 6th Gen: 12
iPad 7th Gen: 7
etc
Answer: I discovered that I could export my entire device inventory to a csv using the Export button. Once there I will aggregate the data.


Fourier transform of f(x)/x

Question: Is there a relation to the Fourier transform of the following statement?
$$\tau(\frac{f(x)}{x})=??$$  and $$\tau(\frac{f(x)}{x^{2}})=??$$
Is this correct?
$$\tau(\frac{f(x)}{x})=\tau(f(x))\tau(\frac{1}{x})$$
I want to know if there is a relationship like this?
$$\tau(x^{n} f(x))=(-i)^{n}\frac{d^{n}}{dk^{n}}F(k)$$
Comment: There's more than one way to transform $xf(x)/x$
Comment: The Fourier transform of a product is not the product of the transforms. Instead it's the convolution of the transforms (perhaps times a factor, depending of what definition of the transform is used).
Answer: Let $\mathcal F_{x\to\xi}\{f(x)\} = \int_{-\infty}^{\infty} f(x) \, e^{-i\xi x} \, dx.$ Then we have $\mathcal F_{x\to\xi}\{x \, f(x)\} = i \frac{d}{d\xi} \mathcal F_{x\to\xi}\{f(x)\}.$ This implies that
$$\mathcal F_{x\to\xi}\{f(x)\} = \mathcal F_{x\to\xi}\left\{ x \, \frac{f(x)}{x} \right\} = i \frac{d}{d\xi} \mathcal F_{x\to\xi}\left\{ \frac{f(x)}{x} \right\}$$
Therefore
$$ \mathcal F_{x\to\xi}\left\{ \frac{f(x)}{x} \right\} = -i \int \mathcal F_{x\to\xi}\{f(x)\} \, d\xi + C.$$
Comment: what is Fourier transform of  $$\delta(x)f(x)&&?
Comment: Since $\delta(x) \, f(x) = f(0) \, \delta(x)$ and $\mathcal F_{x\to\xi}\{\delta(x)\} = 1$ we get $\mathcal F_{x\to\xi}\{f(x) \, \delta(x) \} = f(0).$
Comment: thank you. what is inverse Laplace of $$\sum_{n=0}^\infty\frac{Γ(b+as^{\alpha})}{Γ(b+as^{\alpha}-n)}$$??
Comment: I'm sorry, but that one is too complicated for me.
Comment: is Fourier transform of  $$\delta(x)f(x)&& true??Why did not you use convolution theory?
Comment: $$\tau(\delta(x)f(x))=\tau(f(0)\delta(x))=??$$
How to use convolution theorem to solve it?
Comment: $$\mathcal F_{x\to\xi}\{f(x) \, \delta(x) \} = \int f(x) \, \delta(x) \, e^{-i\xi x} \, dx = f(0) \, e^{-i\xi \cdot 0} = f(0)$$
Comment: Using convolution theorem:
$$\begin{align}
\mathcal F_{x\to\xi}\{f(x) \, \delta(x) \}
& = \frac{1}{2\pi} \mathcal F_{x\to\xi}\{f(x)\} * \mathcal F_{x\to\xi}\{\delta(x)\} \\
& = \frac{1}{2\pi} \int \mathcal F_{x\to\xi'}\{f(x)\} \, \mathbf 1(\xi-\xi') \, d\xi' \\
& = \frac{1}{2\pi} \int \mathcal F_{x\to\xi'}\{f(x)\} \, d\xi' \\
& = \frac{1}{2\pi} \int \left( \int f(x) \, e^{-i\xi' x} \, dx \right) \, d\xi' \\
& = \frac{1}{2\pi} \int f(x) \, \left( \int e^{-i\xi' x} \, d\xi' \right) \, dx \\
& = \frac{1}{2\pi} \int f(x) \, 2\pi \, \delta(x) \, dx \\
& = f(0)
\end{align}$$

Fourier transform of f(x)/x


Question about users vs cck

Question: I need to add a field to represent a person's role on a board. I have a CCK for individuals that includes a field for their position.  I now need to make some changes so that I can keep track of the current position as well as other positions they may have had in the organization.
Originally, I did not set this up as a "User" entity but as a content type.  Now I"m wondering if this is the best way to structure my desired functionality?
Any Advice?
Answer: Do these people need to login to the website? Do they have to change those profile settings themselves? do you plan that those entities will interact on that page? If any of these is yes, than you probably want users.
In all other cases this is probably a no and you are good to go with content types/ custom entities.
for keeping a record of past positions, you might want to try out field collections, you could create a field collection with start, end, position for instance and allow n entries of it.
Comment: No, they don't need to log in, and that you for the idea with field collections.  I think that will head me in the correct direction

Question about users vs cck


Use OCR on a file located in Google Cloud Storage

Question: I performed a few tests with OCR using image upload but would like to use OCR with remote images. These images are located in Google storage and yet I have the following error:
<code>image-annotator::error(10): Can not open file: gs://my_sample_api.appspot.com/my_sample_image.jpg
</code>
Here is the documentation related: https://cloud.google.com/vision/docs/detecting-fulltext
Any idea? Thanks
Answer: Related to Firebase free plan limitation, difficult to find however since no log was provided...


Show that if $m$ in $\mathbb{Z}$ has greatest common divisor $1$ with $21$, then $m^{6}-1$ is divisible by $63$.

Question: Show that if $m$ in $\mathbb{Z}$ has greatest common divisor $1$ with $21$, then $m^{6}-1$ is divisible by $63$. Also, I have to work in $\mathbb{Z}/63 \mathbb{Z}^{*}$, thus the group $63$ modulo $\mathbb{Z}$ under multiplication.
So in other words, I have to prove $m^{6} \equiv 1 (\mod 63)$.
I have no idea how to tie the fact that $m$ has $1$ as gcd with $21$ together with working in the $63$ modulo $\mathbb{63}$ group. I would really appreciate some hints or suggestions guide me in the right direction.
Answer: $(m,7)=1\Rightarrow m^{\phi(7)}=m^{6}\equiv 1 \pmod{7}$
and
$\begin{cases}
m\equiv 1\pmod{3}\Rightarrow 9|(m-1)(m^2+m+1)(m^3+1)=m^6-1\\
m\equiv 2 \pmod{3} \Rightarrow 9|(m^3+1)|(m^6-1)
\end{cases}\Rightarrow m^6\equiv1\pmod{9}$
Answer: Hint: The fact that $(m,21)=1$ implies, clearly, that $(m,63)=1$. Hence we can use Euler's Theorem, which says that $x^{\phi(n)}=1$ mod $n$ for all $n$, and $x$ coprime to $n$. Here, $\phi$ is Euler's totient function. Can you proceed further?
Comment: Using Eulers totient function, I would say $\varphi(63)=\varphi(3^{2} \cdot 7) = \varphi( ( 3^{2}-3^{1}) \cdot 7) = \varphi(42)$. Substracting $42$ from $63$ gives $21$, thus I could work in $m^{6} \equiv 1 (\mod 21)$, correct? But I don't see how this makes the problem easier.
Comment: @Mathbeginner there's no need to work mod $21$, Euler's theorem gives you mod $63$, which is exactly what you want. (i.e. use $n=63$ and $x=m$ immediately)
Comment: But that would be just stating that since we know $m \equiv 1 (\mod 21)$, this implies $m \equiv 1 (\mod 63)$ and that would be the end of it. Is that what you are saying?
Answer: The key point is that $21$ and $63$ have the same prime divisors.
Indeed, since $21 = 3 \cdot 7$ and $63 = 3^2 \cdot 7$, we have
$$
\gcd(m,21)=1 \iff \gcd(m,3)=1=\gcd(m,7) \iff \gcd(m,63)=1
$$
Then Euler's theorem gives
$$m^6\equiv 1 \bmod 7, \quad m^6\equiv 1 \bmod 9$$
Therefore, $m^6\equiv 1 \bmod 63$.
Answer: Carmichael Function $\lambda(63)=[\lambda(9),\lambda(7)]=6$
$(m,21)=1\implies (m,7)=(m,3)=1$
Comment: Downvoting this seems a bit harsh. Though it may well be beyond the OP's knowledge level, it may help to introduce readers to this handy generalization.
Comment: @BillDubuque, Down-voting  like up-voting is not controlled here in MSE.  Just now received one: https://math.stackexchange.com/questions/771320/evaluate-int-pi-2-pi-2-frac-cosx1ex-dx/771389#771389 .Sometimes I try to learn the flaw: https://math.stackexchange.com/questions/3157905/prove-if-c28-equiv-0-mod-p-then-c3-7c2-8c-is-a-quadratic-residue-mod/3157915#3157915


solve A=7A+2B+1C, B=2A+6B+2C, C=1A+2B+7C, A+B+C=1

Question: The answer is A=B=C= 1/3 I can't seem to finish the logic below and I wonder if there is a faster way that anyone can suggest please.  This is related to steady state probabilities and I know I could use "Doubly Stochastic", but I really need to be able to solve these without shortcuts.
I used;
A=7A+2B+1C
B=2A+6B+2C
C=1A+2B+7C
A+B+C=10A+10B+10C
A=B+C
Then
A=7A+2B+1C
-(C=1A+2B+7C)  =
A-C=6A+0B-6C
-5A=-6C
A=C
Now I have these A+B+C=1 and A=B+C and A=C, but keep going around in circles not able to actually arrive at the solution to the answer "A=B=C= 1/3".
Comment: The answer you quote doesn't satisfy the equations as written - I think you need to explain rather more about what is going on here.
Comment: In your very first approach where you add the three equations together, you get $A+B+C=10A+10B+10C$ which gives you $0=A+B+C$ immediately and says that $A+B+C$ cannot have any other value.
Comment: [Welcome to Math.SE!](https://math.meta.stackexchange.com/a/4928/290189) Please use MathJax.
For some basic information about writing math at this site see e.g. [basic help on mathjax notation](/help/notation),
[mathjax tutorial and quick reference](//math.meta.stackexchange.com/q/5020),
[main meta site math tutorial](//meta.stackexchange.com/a/70559)
and [equation editing how-to](//math.meta.stackexchange.com/q/1773).
Comment: I appreciate OP's effort in getting the details done right.  I'm expanding my answer in the spirit of answering OP's real question hidden in the context of this question since he has put ([tag:stochastic-processes]) and mentioned that it originates from a "doubly stochastic matrix".
Answer: Rewrite
\begin{cases}
A = 7A + 2B + C \\
B = 2A + 6B + 2C \\
C = A + 2B + 7C \\
1 = A + B + C
\end{cases}
as
\begin{cases}
0 = 6A + 2B + C \\
0 = 2A + 5B + 2C \\
0 = A + 2B + 6C \\
1 = A + B + C \tag{1}\label{1}
\end{cases}
Subtract the first from the third equation to get $5C-5A=0$, which gives $A = C$.
\begin{cases}
0 = 7A + 2B \\
0 = 4A + 5B \\
1 = 2A + B \tag2\label2
\end{cases}
From the first two equations in \eqref{2}, it's clear that $A = B = 0$, which doesn't satisfy \eqref{2}, so your system is inconsistent.

Remarks:
We're getting something wrong not because of the steps in the question body.  The true reason is that OP has asked the wrong question.  More precisely, he has set up a wrong linear system of simultaneous equations.
The stochastic matrix is in fact $S=\begin{pmatrix}7/10&2/10&1/10\\2/10&6/10&2/10\\1/10&2/10&7/10\end{pmatrix}$, and he's trying to find an invariant measure $p=(p_1,p_2,p_3)$ so that $pS = p$.  (i.e. a left-eigenvector $p$ of $S$ corresponding to the eigenvalue $1$.)  The "shortcut" is to add up the rows of $S$ and observe that it becomes $(1,1,1)$. (i.e. $(1,1,1)S = (1,1,1)$) This gives $p_1 = p_2 = p_3 = 1/3$, which OP wants.
I appreciate OP's effort to translate the problem of "solving for an invariant measure from a doubly stochastic matrix" into "solving for $p$ from $pS = p$ with a given $S$".  The reason is that not everyone is familiar with the jargons in stochastic-processes.  From the actually numeric value of $p$, we can see that OP's mistake is a minor one: when he's stripping off the denominator $10$ from the doubly stochastic matrix $S$, he'd forgotten to multiply another side by $10$.  It should be $p(10S)=10p$ instead of $p(10S) = p$.
i.e. We should have
\begin{cases}
10A = 7A + 2B + C \\
10B = 2A + 6B + 2C \\
10C = A + 2B + 7C \\
1 = A + B + C
\end{cases}
instead.  This should give $(A,B,C) = (1/3,1/3,1/3)$.
Making careless mistakes can help understanding things.
Comment: Thank you.  Your work seems to confirm that I can't arrive at the answer I was given of A=b=c=1/3...I will go back to my instructor..maybe there is a mistake here.
Comment: Thank you again, this makes sense now at least.  I will go practice it.  Thank you very much.


probability of collision from hashing

Question: So i have a hash table that can hold 100 elements. It currently stores 30 elements. What is the probability that the next 2 inserts will result in at least one collision?
What i did was figure out the sample space to be 100*100=10000, representing all the possible number of different insertions for the 2 insertions (for example: first insertion being in 5th index and second insertion being in 74th index).
Then I tried to get elements of the sample space, which is in the form of (index of first insertion, index of second insertion) that have collisions. First, I counted 30 collisions from the first insertion and multiplied that by 100 since there are 100 possibilities for the second insertion for each of the first insertion. This would get the count of collisions that are guaranteed from first insertion. Then I multiplied this by 2 since there are also 30 collisions from the second insertion and I had to multiply that by 100 since there are 100 possibilities for the first insertion for each of the second insertion. This would get the count of collisions that are guaranteed from second insertion. Since this is overcounting the number of collisions that are the same(For example: if there was a collision at 5th index, first and second insertion being at the 5th index), I subtracted 30 since thats the count of occupied indexes.
Then I added 70 in the case of the first insert not causing collision but the second one causing collision by being inserted in the same place as the first.(For example both insertions being in 7th index when originally index 7 wasnt occupied before the first insertion)
I got .604 as my final answer: (2*30*100-30+70)/10000
Is this the correct answer and approach?
Comment: The answer would be somewhat similar to the "Birthday Match" problem in some sense. This may provide a good read:http://preshing.com/20110504/hash-collision-probabilities/
Comment: @NoChance I know I can solve it by using "1- P(no collision)". I just want to know if my way is correct.
Comment: Your answer should be less than 1. For example the answer given below is 0.517
Comment: @NoChance  my answer is less than 1: ".604"
Comment: fine, I don't get what do you mean by "I found all the combinations of first and second inserts that would result in collisions:
2*30*100=6000. 30*100 is for counting the number of collisions from first insert and second insert being anything."
Comment: @NoChance the sample space is 100*100, which is all the combination of the first insert and second insert. There are 30 spaces occupied out of the 100 in your first insert, so you count each of these 30 spaces with any space being inserted in the second insert: 30*100. This is counting the guaranteed collisions in the first insert.
Comment: I still don't get your approach.
Comment: @NoChance I just made it clearer.
Comment: Your approach is very strange to me, I don't get it at all. Maybe some one else could answer.
Answer: $$P(\text{Colllision}) = 1 - P(\text{no collision}) =
1 - {70\over100}\cdot{69\over 100}.$$
Comment: Can you please explain what i did wrong?

probability of collision from hashing


Making the content of a resizable div responsive

Question: I have created a 'virtual desktop' for a web app I'm developing, and into this desktop I can open 'virtual windows'. Each is an absolutely positioned div which can be moved, resized and so on.
I'd like to make the content of (some of) these 'windows' responsive, scaling from 4 columns @ 25% each, down to 1 column @ 100%.
I'm not using any frameworks, as this is a one off edge case that I'm hoping to solve with a custom solution.
I've tried @media, but this has no effect at all as I'm not altering the size of the actual browser window, only the dimensions of the virtual window's content area.
Is there a CSS3 solution that I'm missing here? Essentially I need to alter the width (as a percentage) of a class, based on the dimensions of its parent, where those dimensions are completely independent of the browser window. Essentially, pointing @media to the immediate parent of the class definition I want to alter, rather than the containing viewport.
Is this possible?
Comment: Any reason that you dont use frameworks? jQuery UI is perfect for this kind of stuff. With functions as `draggable()` and `resizable()` it works like a dream.
Comment: I don't use jQuery as it would just be for this specific issue. That's a butt load of code to stretch and shrink a handful of form elements.

The whole thing is a bit of an apprenticeship piece, so I've hand coded everything from scratch, just for the LOLZ ;)
Comment: Ok! I take it you get paid by the hour ;-). Then its a great reason to not use frameworks!
Answer: Yes - it is possible... But:
You can't do it with a DIV.
After a goodly amount of coffee, it dawned on me that @media is taking it's dimensions from document.window, so the simplest workaround for this is to embed the 'responsive' elements in an iframe. This gives@media its own, localised document bounds within the main content of the page (or desktop in my case). If my pseudo window content is a separate document, with its own set of bounding dimensions, @media works like a dream and I can have responsive layouts inside resizable divs...
Easy when you think about it.
ASIDE:
I tried using flexbox, and while this achieved some of what I was after (ie stretching content to fill available space) it failed when it came to shrinking the content down below its min-width attribute, and the wrapping mechanism was, to put it bluntly, pretty pants. There are a lot of settings to play with though, and it might well be possible to leverage it for specific use cases. It would definitely be a neater solution.


Region of Interest in nighttime vehicle detection

Question: I am developing a project of detecting vehicles' headlights in night scene. I am working on a demo on MATLAB. My problem is that I need to find region of interest (ROI) to get low computing requirement. I have researched in many papers and they just use a fixed ROI like this one, the upper part is ignored and the bottom is used to analysed later.

However, if the camera is not stable, I think this approach is inappropriate. I want to find a more flexible one, which alternates in each frame. My experiments images are shown here:

If anyone has any idea, plz give me some suggestions.
Comment: What if you look at the V component of the HSV space? I am guessing that would give you a starting point.
Comment: u mean I should based on the brightness? I have tried but dont know how to distinguish between 2 regions.
Comment: It might be useful if you updated your question with an image where you highlight the regions you want to segment in e.g red manually.
Comment: basically I want to discard the upper darker part and only process the  lower brighter part in the image
Comment: You haven't shown what regions you are after. Add two extra images and show the regions you want to identify. It's not that clear what regions you are after and that means you will have less chances of getting a useful answer.
Answer: I would turn the problem around and say that we are looking for headlights
ABOVE a certain line rather than saying that the headlights are below a certain line i.e. the horizon,
Your images have a very high reflection onto the tarmac and we can use that to our advantage. We know that the maximum amount of light in the image is somewhere around the reflection and headlights. We therefore look for the row with the maximum light and use that as our floor. Then look for headlights above this floor.
The idea here is that we look at the profile of the intensities on a row-by-row basis and finding the row with the maximum value.
This will only work with dark images (i.e. night) and where the reflection of the headlights onto the tarmac is large.
It will NOT work with images taking in daylight.
I have written this in Python and OpenCV but I'm sure you can translate it to a language of your choice.
import matplotlib.pylab as pl
import cv2

# Load the image
im = cv2.imread('headlights_at_night2.jpg')

# Convert to grey.
grey_image = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

Smooth the image heavily to mask out any local peaks or valleys
We are trying to smooth the headlights and the reflection so that there will be a nice peak. Ideally, the headlights and the reflection would merge into one area
grey_image = cv2.blur(grey_image, (15,15))

Sum the intensities row-by-row
intensity_profile = []
for r in range(0, grey_image.shape[0]):
    intensity_profile.append(pl.sum(grey_image[r,:]))

Smooth the profile and convert it to a numpy array for easy handling of the data
window = 10
weights = pl.repeat(1.0, window)/window
profile = pl.convolve(pl.asarray(intensity_profile), weights, 'same')

Find the maximum value of the profile. That represents the y coordinate of the headlights and the reflection area. The heat map on the left show you the distribution. The right graph shows you the total intensity value per row.
We can clearly see that the sum of the intensities has a peak.The y-coordinate is 371 and indicated by a red dot in the heat map and a red dashed line in the graph.
max_value = profile.max()
max_value_location = pl.where(profile==max_value)[0]
horizon =  max_value_location

The blue curve in the right-most figure represents the variable profile
The row where we find the maximum value is our floor. We then know that the headlights are above that line. We also know that most of the upper part of the image will be that of the sky and therefore dark.
I display the result below.
I know that the line in both images are on almost the same coordinates but I think that is just a coincidence.
Comment: @Benoit_11 Thanks. I used this technique on microscopy images for finding the surface of a dish on which cells resided. It's nice to get acknowledgement from someone with similar background and interests (including ice hockey :-) ).
Comment: @Vendetta You could start with finding the row with the highest intensity. Once you know that, then you know which area of the image the lights are and will give you a starting point. So you could loop through each row of the image, from top to the horizontal line and take e.g. the average of each row. The highest average will tell you which row the headlights may be on. You would have to experiment with the max, mean, median etc to see if any would work.
Comment: thank you for your thorough answer. It helps me alot. I have 1 more question: How do you calculate the intensity of each row? Did you just add all pixel value of a row?
Comment: Yes. You take each row and sum the intensity for each row. you will then get vector with 1x640 elements (if you have 640 rows). One value for each row.
Comment: @kkuilla, Could you please explain how did you calculated heatmap ? profile and intensity_profile variables are vectors but the heatmap should come from a 2D matrix. How max_value and horizon values were used to obtain resultant image?
Comment: @Vendetta The heatmap is just the the `grey_image` displayed with a different colourmap. I think I used the jet colourmap. I can't find exactly how I did it but it is very similar to [this](https://stackoverflow.com/a/32427366)..
Comment: @kkuilla, Thank you, I got it. Could you also please suggest, how should I detect headlights in the above image after setting horizontal line as the ROI?
Comment: @kkuilla, Thank you that is helpful. But I wonder do I need to go through each row of each the image? is there a way to use above technique where you calculated the max. intensity along row, so that I can set a threshold for the rows to be inspected for highest intensity?
Comment: @Vendetta Apologies but I'm not sure I understand. I don't understand ` each row of each the image?`
Comment: Finding the horizontal line is just a pre-processing step, I guess. Another approach would be to calculate the image gradient. The higher the intensity, the higher the gradient. Then you can use the second to get the location of those maximum intensities.
Comment: @kkuilla, that is a typo, I meant the image not each image. Thank you for useful suggestions. By the way, you said "Then you can use the second to get " what is second here?
Comment: @Vendetta `Second` is referring to the second derivative of the image. The first derivative (first order gradient) will give you the peaks. The second (second order gradient) will give you the location of them. It is the same principle as the derivatives studied in school if you studied maths.
Answer: You may try downsampling the image.
Comment: hope the following example helps if what i'm suggesting is not clear to you.sometime back i had to localize a check in an image of size ~3MP. i downsampled the image and ran my localization algorithm on that, extracted the ROI, projected it to the original ~3MP image and extracted the check. then i did the required processing (like binarization) on the region i extracted from ~3MP image.
Comment: what do you mean? I dont understand.
Comment: from your question i understand that you already know how to detect what you want, and your main concern is speed. if the image frames are too large, you may scale them down and process the scaled down version. however, you have to pick the scale such that it does not adversely affect the accuracy
Comment: and, downsampling is not the same as resizing. though i cannot say for sure, it could be faster than interpolation (matlab resizing use interpolation)
Comment: yes you are right. I am concentrating on speed, but my goal is to process in HD image so I need to keep the same size of the image
Comment: you can use the downsampled image frames to detect the regions of interest (ROIs).once you know the ROIs in the scaled down version, you can easily project those to the HD image and extract those regions and process them


MVC Partial View in other view

Question: I have two views for creating an Owner or Service Company, each one having their own table.
When a user registers using the standard View->Account->Register view I have added a radio button to ask if they are an Owner or Service Company. What I am trying to achieve is based on which one they select a partial view of the create view for the option they select to appear with a single submit button that both registers the user and creates the Owner or Service Company record.
Look at various tutorials I can only find MVC 4 guides to creating partial views where the Add View dialog looks different. I have read the partial views section in Professional ASP.NET MVC 5 and it suggests you just call a view with a retuns PartialView() method, but I am not sure how to do this outside of a controller and based on a user action (i.e. selecting the radio button).
Any advice gratefully received!
Comment: Is it necessary to do this in one view? Could the `Register()` post method just redirect to a new view to complete one or other of the required forms?
Comment: Are you looking for something like this? http://www.binaryintellect.net/articles/218ca630-ba50-48fe-af6e-6f754b5894aa.aspx
Comment: I have also thought of that, my concern was not registering the user until Owner or Service Company data was also entered. I have just tried @Html.Partial("~/Views/Owners/Create.cshtml") which rendered the Owners Create view but it rendered the Submit button from that page as well so I have Submit and Register at the bottom of the page.
Comment: @Spionred. Your will need a view model containing the models for Register, Owner and ServiceCompany, plus a property for the radio buttons. The initial view has a form with the Register model and radio buttons. Then based on the selected radio button, use ajax to add the relevant partial partial view inside the form tags and reparse the validator. Then post back the view model (the Register and either the Owner or ServiceCompany models will be populated.
Comment: @Stephen, that sounds like a good plan and I think I understand what you are suggesting. I am new so learning as I go. If there are any examples of this please let me know. I will post back how I get on.
Comment: @Spionred, I post an answer (give me a hour) with an outline of how to go about this, however what happens if a user who is registering is (say) a service company, but another user from the same company has already registered - you could get multiple entries representing the same service company?
Comment: @Stephen, The email address is part of the AspNetUsers record and must be unique. I suppose there is nothing stopping someone from registering twice with different email addresses but not sure what we could do about that.
Comment: No, I meant a company usually has more than one employee. If more than one user registers from the same company, they will be all be entering details for the company, despite that fact its already been added by the first employee of that company.
Comment: I am planning to have another user type called contractor. These users will be created by the primary Service Company user in separate view.
Comment: Can't be sure, but I suspect the correct approach would be (1) show login page if not authorized which include a link to register (2) the register page includes a dropdown of registered companies to select the users company, and also includes a link to register a new company if its not in the list (3) the register company page includes options for company type. (4) submitting the register company form redirects back to the register user page where the new company can now be selected.
Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73396/discussion-between-spionred-and-stephen-muecke).
Answer: I would recommend posting to the Register method and based on the value of the selected option, redirect to the relevant view to complete the details. The users table could have a (say) IsRegistrationComplete field which is marked as true only when the 2nd form has been saved.
To do this in one view, you will need a view model with properties for the Register model, Owner model, ServiceCompany model and selected option.
View model
public class RegistrationVM
{
  public RegisterVM Register { get; set; }
  public OwnerVM Owner { get; set; }
  public ServiceCompanyVM ServiceCompany { get; set; }
  [Required]
  public string CompanyType { get; set; }
}

Controller
public ActionResult Register()
{
  RegistrationVM model = new RegistrationVM();
  model.Register = new RegisterVM();
  return View(model);
}

public ActionResult Register(RegistrationVM model)
{
  // Either Owner or ServiceCompany properties will be populated
}

public PartialViewResult CreateOwner()
{
  var model = new RegistrationVM();
  model.Owner = new OwnerVM();
  return PartialView(model);
}

public PartialViewResult CreateServiceCompany()
{
  var model = new RegistrationVM();
  model.ServiceCompany = new ServiceCompany();
  return PartialView(model);
}

Notes: The partial views should not contain a <form> element (it will be inserted inside the <form> tag of the main view). The model in the partial views needs to be RegistrationVM so the controls are correctly prefixed and can be bound on post back (e.g. <input name="Owner.SomeProperty" ../> etc)
View
@model RegistrationVM
@using(Html.BeginForm())
{

    // render controls for the register model

    @Html.RadioButtonFor(m => m.CompanyType, "owner",new { id = "owner" })
    Owner
    @Html.RadioButtonFor(m => m.CompanyType, "service",new { id = "service" })
    Service Company
    @Html.ValidationMessageFor(m => m.CompanyType)
    <button id="next" type="button">Next</button>

  <input id="submit" type="submit" /> // style this as hidden
}

Then add a script to handle the Next button which validates the existing controls, gets the value of the selected service, uses ajax to load the relevant form, re-parses the validator and displays the submit button
var form = $('form');
$('#next').click(function() {
  // Validate what been entered so far
  form.validate();
  if (!form.valid()) {
    return; // correct errors before proceeding
  }
  // Get selected type and update DOM
  var type = $('input[name="CompanyType"]:checked').val();
  if (type == "owner") {
    $('#step2').load('@Url.Action("CreateOwner")');
  } else {
    $('#step2').load('@Url.Action("CreateServiceCompany")');
  }
  // Re-parse validator so client side validation can be applied to the new controls
  form.data('validator', null);
  $.validator.unobtrusive.parse(form);
  // Show submit button
  $('#submit').show();
});

MVC Partial View in other view


VisualG - Prog para limites

Question: Estou fazendo o programa do seguinte exercício:

Crie um algoritmo que leia os limites inferior e superior de um
  intervalo e imprimir todos os números ímpares no intervalo aberto e
  seu somatório (UTILIZE O PARA). Suponha que os dados digitados são
  para um intervalo decrescente.

Minha dúvida é, como faço para ele não exibir os números digitados? Porque devo imprimir o intervalo aberto, e neste, ele não deve exibir os números digitados.
algoritmo "APS08"
var
   inf, sup, soma:inteiro
   i:inteiro
inicio

      escreva("Digite o limite inferior: ")
      leia(inf)
      escreva("Digite o limite superior: ")
      leia(sup)

      para i de inf ate sup faca passo -1
           se (i%2 = 1) entao
              escreva(i)

              soma <- soma + i
           fimse
      fimpara

      escreval("")
      escreval("Soma: ",soma)

fimalgoritmo

Alguém pode me explicar como faço?
[RESOLUÇÃO]
algoritmo "APS08"
var
   inf, sup, soma:inteiro
   i:inteiro
inicio

      escreva("Digite o limite inferior: ")
      leia(inf)
      escreva("Digite o limite superior: ")
      leia(sup)

      para i de sup-1 ate inf-1 passo -1 faca
           se (i%2 = 1) entao
              escreva(i)

              soma <- soma + i
           fimse
      fimpara

      escreval("")
      escreval("Soma: ",soma)

fimalgoritmo

Comment: como assim *"não exibir os números digitados"*? quer esconder o comando `leia`?  Como vai saber o que está sendo digitado? Se quiser tirar da tela depois, só usar o comando `limpatela`
Comment: você pode postar a solução como resposta que é o mais correto
Comment: Entendi, você pode tratar isso no `para`, não comece a partir de `inf` mas `inf+1` e a mesma ideia no `ate`, e se quiser inverter a ordem inveta também onde começa e onde termina o `para`
Comment: Se você inverter a ordem, precisa também inverter o passo: `para i de  sup-1  ate inf+1 passo 1`
Comment: Não amigo. Suponha que o limite inferior é 5 e o superior é 10. Ele vai exibir todos os ímpares de 5 até 10 (5,7,9,11,13,15,17,19,21,23,25). O 5 e o 25 não devem aparecer. Entende? E ainda deveria estar mostrando em ordem decrescente.
Comment: Fiz a alteração, ficou `para i de inf+1 ate sup-1 passo -1` e resolveu o problema. Quanto a ordem decrescente, nada. Quando eu inverti a ordem, ele parou de exibir.
Comment: Já achei o problema. Aquele bom e velho "erro entre a cadeira e o monitor". Agradeço sua ajuda, e vou editar o post com a resolução.
Comment: Ok. Nos próximos posts, farei isso. Obrigado :)
Answer: O limite inferir está incorreto. Veja um teste:

Como é um intervalo aberto, só deveriam ser considerados os valores entre 2 e 9 (inclusos). O 1 não deveria entrar.
Para corrigir, apenas altere de "-" para "+" para o inferior. Exemplo:
De:

para i de sup-1 ate inf-1 passo -1 faca

Para:
para i de sup-1 ate inf+1 passo -1 faca

Segue outra forma:
algoritmo "APS08"
var
   inferior, superior, somatorio: inteiro
   ultimo, primeiro:  inteiro
   i:inteiro
inicio
   escreva("Digite o limite inferior: ")
   leia(inferior)
   escreva("Digite o limite superior: ")
   leia(superior)

   //encontrar o último ímpar da sequência
   se (superior % 2 = 0) entao //se par
      ultimo <- superior - 1
   senao
      ultimo <- superior - 2
   fimSe

   //encontrar o primeiro ímpar da sequência
   se (inferior % 2 = 0) entao //se par
      primeiro <- inferior + 1
   senao
      primeiro <- inferior + 2
   fimSe

   escreval()
   escreva ("Intervalo aberto:")
   para i de ultimo ate primeiro passo -2 faca //os ímpares alternam de 2 em 2
      escreva (i)
      somatorio <- somatorio + i
   fimpara

   escreval("")
   escreval("Somatório: ", somatorio:1)
fimAlgoritmo

VisualG - Prog para limites


Varchar ou int para campos "tipo"

Question: Tenho uma coluna tipo com 6 opções para salvar no banco, é melhor salvar a string como varchar ou como int e tratar isso no front? Por exemplo, quando for 1 no banco, exibir certa string na tela e assim por diante.
Sei que int teria mais desempenho, mas gostaria de saber se tem muita dor de cabeça ou risco de inconsistência. Pensei em salvar como varchar mesmo para não ter que ficar tratando isso.
Comment: Você pode usar o type como ENUM ou SET
Comment: Normalmente uso `tinyint()`.
Comment: @Coringa legal, não conhecia esses tipos, vou dar uma olhada, valeu. Daniel, valeu pela dica.
Answer: Não dá para afirmar sem entender o que deseja de fato, sem conhecer todo o contexto.
Eu diria que é melhor usar um int ou até um tipo numérico inteiro de menor tamanho já que são apenas 6 opções.
Não acho que seja desejável ter o espaço do texto ocupado na tabela. Mas o problema pior é se tiver que mudar o nome de um desses tipos. Pode ser que seja útil fazer isso.
Isso é considerado uma enumeração e alguns bancos de dados possuem forma própria de lidar com isso. Mas é comum ser mais vantajoso ter uma tabela normalizada com as descrições. Claro que a normalização gera algum custo extra, mas se tudo ocorrer bem estará no cache e o custo será bem pequeno. Mas pode ser exagero fazer isso.
O problema de tratar na aplicação é que se precisar mudar um nome, ou adicionar um tipo novo, terá que dar o devido tratamento nela, nem todo mundo sabe fazer isso da forma correta. Mas está longe de estar errado. Pode ser que seja a solução mais simples.
Riscos sempre existem em cada uma dessas soluções. Se fizer errado qualquer uma pode não funcionar direito. Mas nenhum tem riscos óbvios mesmo fazendo certo.
Dê uma olhada:

Qual atributo usar para campos da base de dados que trabalhem com números de 0 à 5?
Armazenar valores representantes do 'sexo' de um usuário em bancos de dados
Qual a vantagem em usar o tipo ENUM?
Como definir tipos pré definidos no MySQL?

Coloquei no GitHub para referência futura.
Comment: Não, ter muitas tabelas não prejudica. Há quem goste que criar uma só com todas descrição, aí teria 3 colunas, uma dizendo sobre que tipo de descrição é, outra que identifica a descrição e a terceira que é a descrição em si. Eu já fiz isso no passado porque havia restrições diversas, hoje não vejo necessidade de fazer isso mais. Não tem nada de ruim em fazer na aplicação, só tem que fazer certo, mas isso vale para qualquer solução. Essa de não usar booleano depende do caso. Só use se realmente os valores sem SIM e NÃO e nunca vá mudar, se for outra coisa, não use, caso do sexo.
Comment: Entendi, ficou bem claro agora. Então usarei uma tabela com a descrição, mas como vou usar isso mais vezes, muitas tabelas com descrições prejudicará o desempenho? Usar somente int e tratar na aplicação é considerado má prática? Aproveitando, no outro post você disse para não usar boolean mesmo se forem 2 valores apenas, em qual cenário deve-se usar boolean no bd? Obrigado.
Comment: Ok. Muito obrigado, @bigown, ajudou muito!

Varchar ou int para campos "tipo"


bool class template generic copy and move constructor

Question: I was trying to implement a copy constructor for a class template that allows for all instantiations to converted from one version of the class template to the other.
This lead to the following code
#include <memory>
#include <iostream>

template<bool binary>
class Bar {
private:
    friend class Bar<!binary>;
    std::unique_ptr<int> data;
public:
    Bar(int example) : data(std::make_unique<int>(example)) {

    }

    template<bool value>
    Bar(const Bar<value>& bar) : data(std::make_unique<int>(*bar.data)) {

    }

    //other methods that differ depending on binary value...
};

int main() {
    Bar<false> b1{ 1 };
    Bar<false> b2{ b1 }; //causes compile error
    Bar<true> b3{ b1 }; //works as expected
}

Constructing from a different type works but constructing from the same type issues a compile time error stating

Bar::Bar(const Bar &)': attempting to reference a
deleted function

Apparently the generic copy constructor isn't being called which led to me manually writing out the copy constructor, leading to the current code.
#include <memory>
#include <iostream>

template<bool binary>
class Bar {
private:
    friend class Bar<!binary>;
    std::unique_ptr<int> data;
public:
    Bar(int example) : data(std::make_unique<int>(example)) {

    }

    Bar(const Bar& bar) : data(std::make_unique<int>(*bar.data)) {

    }

    template<bool value>
    Bar(const Bar<value>& bar) : data(std::make_unique<int>(*bar.data)) {

    }

    //other methods that differ depending on binary value...
};

int main() {
    //all work as expected.
    Bar<false> b1{ 1 };
    Bar<false> b2{ b1 };
    Bar<true> b3{ b1 };
    Bar<true> b4{ b3 };
}

It's worth noting that this is also necessary for the move constructor, copy assignment operator, and move assignment operator.
Thus, I have two questions

Why doesn't the generic copy or move constructor overshadow the copy/move constructor?
Is there a better workaround to allow for copy construction of both the same type and alternate type? As of right now I have to essentially duplicate code for each copy/move constructor and assignment operator.
Answer: From https://en.cppreference.com/w/cpp/language/copy_constructor

A copy constructor of class T is a non-template constructor whose
first parameter is T&, const T&, volatile T&, or const volatile
T&, and either there are no other parameters, or the rest of the
parameters all have default values.

Hence you can't define new parameters for the copy ctor in that way.
Now, if you use Bar<false>(const Bar<true>&) or Bar<false>(const Bar<true>&), the compiler will use the ordinary cpy ctor which has been generated on instantiating the class and won't instantiate your corresponding templated ctor.
And If this is true, leave the job for the compiler to define the copy ctor but change the unique_ptr to shared_ptr(because with unique_ptr as a member var the class is not copyable), or define two ctors as you have in the second code.
For the following code, I used shared_ptr and there is no error has occurred
but notice that the templated cpy ctor  doesn't execute for Bar<false/true>(const Bar<false/true>&) while it executes for Bar<true/false>(const Bar<false/false>&)
#include <memory>
#include <iostream>

template<bool binary>
class Bar {
private:
    Bar& operator=(const Bar&) = delete ;
    friend class Bar<!binary>;
    std::shared_ptr<int> data;
public:

    template<bool value>
    Bar(const Bar<value>& bar) : data(std::make_shared<int>(*bar.data)) {
        std::cout << __PRETTY_FUNCTION__ <<"\n";

    }
    Bar(int example) : data(std::make_shared<int>(example)) {

    }

    //other methods that differ depending on binary value...
};

int main() {
    Bar<false> b1{ 1 };
    Bar<false> b2{ b1 };
    Bar<true> b3 {b1};
    Bar<true> b4 {b3};
    Bar<false> b5 {b4};

}

Live


Conditional expectation of independent random variables

Question: Suppose $X, Y, Z$ are random variables and $X, Y$ are independent to each other. Can  I write $\mathrm{E}[XY|Z]=\mathrm{E}[X|Z] * \mathrm{E}[Y|Z]$ ?
Comment: Assume you are at home and you are about to apply the rule $$E[XY]=E[X]E[Y]$$ for your independent random variables $X,Y$. And someone in the radio says $Z$ has been realized. So in order to take everything in account you write in your paper that everything from now on in the world happens conditionally on the realization of Z. Then will your rule seize to apply?
Answer: I don't think the given statement is true.  Let $X, Y$ be IID discrete uniform on $\{1,2,3,4,5,6\}$.  Let $Z$ be $1$ if $X-Y = 2$, and $0$ otherwise.  Then ${\rm E}[XY \mid Z = 1] \ne {\rm E}[X \mid Z = 1]{\rm E}[Y \mid Z = 1]$.  In general, it is not too hard to see that if $Z$ is a nontrivial function of $X$ and $Y$, then $X \mid Z$ and $Y \mid Z$ are not necessarily independent even if $X$ and $Y$ are independent.
Comment: ok, thank you. I understand it.
Answer: Toss a coin twice. Let $X=1$ if we get a toss on the first head, and $0$ otherwise. Let $Y=1$ if we get head on the second toss, and $0$ otherwise. Then $X$ and $Y$ are independent.
Let $Z=X+Y$. If $Z=1$, then $XY=0$ with probability $1$, so $E(XY|Z=1)=0$.
But $E(X|Z=1)=E(Y|Z=1)=\frac{1}{2}$.
Comment: It is quite possible I have written this before. Finding stuff on MSE is, for me, difficult.
Comment: This was the counterexample I had read on this site before (maybe even you were the one who wrote it!) but for some reason I could neither remember the details nor find where it had been previously posted...


Getting only TID by Entity Query

Question: I am using Drupal 8. By retrieving an entity by Entity Query I am getting an array. How can I fetch directly the TID?
Right now I am doing the following:
  public function getCommuneTidByPostalCode(int $postalCode) {
    $commune = \Drupal::entityQuery('taxonomy_term')
      ->condition('field_zipcode', $postalCode)
      ->condition('vid', 'commune')
      ->execute();

    if (!empty($commune)) {
      return intval(key($commune));
    }
  }

Can I drop somehow this part?:
if (!empty($commune)) {
  return intval(key($commune));
}

Answer: No. An entity query always returns a list, if you want just one record, you have to get that from the array.
The intval() isn't really necessary though, Drupal/PHP doesn't care if you use the tid as string or integer. Also, it always returns an array, and key() also works on an empty array, so you can really simplify that to just return key($communie);. And I'd actually recommend to use current() or reset() because the key is the revision ID and the value the ID and while terms currently do not yet use revisions, they will likely soon support that.
So...
$commune = \Drupal::entityQuery('taxonomy_term')
  ->condition('field_zipcode', $postalCode)
  ->condition('vid', 'commune')
  ->execute();

return current($commune);

Is all you need.

Getting only TID by Entity Query


Set environment variables from header for Ruby/Rails app hosted in Nginx/Passenger

Question: I installed a server for Redmine, running with Nginx/Passenger. The server also hosts Gitlab and it goes well.
I put some kind of SSO plugin for Redmine (which I found and installed) and it needs an environment variable to be filled with the user name : this is where it is getting tricky.
So far I can separately :

get the username from the X-Forwarded-User header
auto connect to Redmine passing the username as a constant value
auto connect to Redmine passing the right username as a constant value after testing the current forwarded value => that IS ugly and requires to do a test for each existing user

But I cannot directly set the username with the forwarded value, it looks like the $http_x_forwarded_user variable is not being evaluated...
Is there a neat solution ? My current Nginx/Passenger config :

upstream gitlab-workhorse {
  server unix:/var/opt/gitlab/gitlab-workhorse/socket fail_timeout=0;
}

server {
  listen 0.0.0.0:80;
  listen [::]:80 ipv6only=on default_server;
  server_name server.example.com
  server_tokens off; ## Don't show the nginx version number, a security best practice

  root /opt/gitlab/embedded/service/gitlab-rails/public;

  location / {
    proxy_read_timeout 300;
    proxy_connect_timeout 300;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://gitlab-workhorse;
    access_log /var/log/nginx/gitlab_access.log;
    error_log /var/log/nginx/gitlab_error.log;
  }

  location ~ ^/redmine(/.*|$) {
        alias /opt/redmine/public$1;
        passenger_base_uri /redmine;
        passenger_app_root /opt/redmine;
        passenger_document_root /opt/redmine/public;
        passenger_enabled on;
        access_log /var/log/nginx/redmine_access.log;
        error_log /var/log/nginx/redmine_error.log;

        # returns "dave", who is the connected user => great !
        return 200 $http_x_forwarded_user;

        # connects on Redmine as Dave => marvelous !
        passenger_env_var REMOTE_USER dave;

        # working but ugly solution
        if ($http_x_forwarded_user = "dave") {
            passenger_env_var REMOTE_USER dave;
        }
        if ($http_x_forwarded_user = "john") {
            passenger_env_var REMOTE_USER john;
        }
#       ... and so on for every single user having an account...

        # fails : "user does not exist in database" => $http_x_forwarded_user not being evaluated ??
        passenger_env_var REMOTE_USER $http_x_forwarded_user;
    }
}

Answer: According to this bug report, this is currently broken:

passenger_env_var VAR $nginx_var; sets VAR to $nginx_var, not its value.

Possible workaround:

Drop passenger (but keep Nginx)
Setup redmine via uwsgi, which

aims at developing a full stack for building hosting services.
Application servers (for various programming languages and protocols), proxies, process managers and monitors are all implemented using a common api and a common configuration style.
Thanks to its pluggable architecture it can be extended to support more platforms and languages.
Currently, you can write plugins in C, C++ and Objective-C.
The "WSGI" part in the name is a tribute to the namesake Python standard, as it has been the first developed plugin for the project.
Versatility, performance, low-resource usage and reliability are the strengths of the project (and the only rules followed).

Using this myself in multiple production environments, highly recommend!
Comment: Thanks for pointing out the bug !  It seems like it will not be fixed shortly as it is labelled as 'enhancement' and was opened something like 18 months ago...  This pushes me toward the workaround with uwsgi


Javascript select content which is inside the tag

Question: I have a problem selecting the content inside a tag.
Here is the example:

aaaa

bbbb

cccc

I want to select the aaaa bbbb cccc and store them in an array.
If I use document.getElementsByTagName, it will store the

tags as well. How can I just pick the content?
Answer: You can use map() and return just text of each li with textContent

var li = document.querySelectorAll('li');
var result = Array.from(li).map(function(e) {
  return e.textContent;
})

console.log(result)

aaaa

bbbb

cccc

Answer: You can iterate over the elements and get the text content
let textArray = Array.from(document.getElementsByTagName('li')).map(function(elem) {
    return elem.textContent;
});

console.log(textArray); // ["aaaa", "bbbb", "cccc"]

Using .textContent instead of .innerHTML grabs just the text without any tags that may exists with the li elements


How to handle functions after dom change

Question: I'm having alot of ajax calls where every one of them insert new html code into the DOM.In ajax on success I'm calling functions again so they have the option to work with the new DOM elements.
The problem is that whit every new call the functions on success gets called X times - 1,2,4... And for example if I have a function createNewButton and click it twice I'll end up with 3 buttons, because the second time it pass the ajax will call the function again.
function ajaxAddFormFeild(field_type, value, unique) {
    $.ajax({
        beforeSend: function (request)
        {
            request.setRequestHeader("X-CSRF-TOKEN", $('meta[name="csrf-token"]').attr('content'));
        },
        type: "GET",
        url: base_url + 'Form/' + field_type + '?value=' + value + '&unique=' + unique,
        success: function (response) {
            if (unique) {
                $('.input-options[data-unique="' + unique + '"').first().after(response);
            } else {
                $('.form_content').append(response);
                addNewOption();
                changeFormFieldPosition();
            }
            removeFormGroup();
            removeFormField();
        },
        dataType: 'html'
    });
}

Im building server side generated form builder.
What will be the perfect realization ?!
You click minuses without giving an answer...
Comment: Some code please. You're probably attaching a listener after every AJAX call.
Comment: you should provide codes you have tried
Comment: Yes Im attaching them,if I not they will be not working for the new elements.But when they are attached twice or more... gets called each one over another.
Comment: Show me the code where you're attaching listeners.
Answer: You should use jQuery method $().off() to unbind the event on that element which is invoking the ajaxCall function repeatedly.
Comment: That won't solve the problem but remove the consequences.


Unrolled Loops of Static Arrays

Question: If I call the function
/** Check if all Elements, possibly recursively, of $(D x) are zero. */
bool allZero(T)(in T x) @safe pure nothrow {
    import std.range: isIterable;
    static if (isIterable!T) {
        foreach (ref elt; x) {
            if (!elt.allZero) { return false; }
        }
        return true;
    } else {
        return x == 0;
    }
}

with a static array will D unroll the foreach for me automatically in release mode?
If not could
/** Static Iota. */
import std.typetuple: TypeTuple;
template siota(size_t from, size_t to) { alias siotaImpl!(to-1, from) siota; }
private template siotaImpl(size_t to, size_t now) {
    static if (now >= to) { alias TypeTuple!(now) siotaImpl; }
    else                  { alias TypeTuple!(now, siotaImpl!(to, now+1)) siotaImpl; }
}

be used to achieve unrolling instead of foreach?
Further is there a flag to DMD that generates assembly code so that I myself, in the future, can investigate the code generated by DMD?
Update: Here's my solution so far.
/** Check if all Elements, possibly recursively, of $(D x) are zero. */
bool allZero(T, bool useStatic = true)(in T x) @safe pure nothrow { // TODO: Extend to support struct's and classes's'
    static        if (useStatic && isStaticArray!T) {
        foreach (ix; siota!(0, x.length)) {
            if (!x[ix].allZero) { return false; } // make use of siota?
        }
        return true;
    } else static if (isIterable!T) {
        foreach (ref elt; x) {
            if (!elt.allZero) { return false; }
        }
        return true;
    } else {
        return x == 0;
    }
}

Does it look ok?
Comment: David Simcha gave an [interesting talk](http://youtu.be/yMNMV9JlkcQ?t=35m24s) that included a section on automated loop unrolling using D's metaprogramming features at DConf 2013.
Answer:
with a static array will D unroll the foreach for me automatically?

No, the language does not guarantee it. Some implementations (compilers) might unroll the loop as an optimization.

If not could my implementation of static iota (siota) be used to achieve this?

Yes, using foreach over a tuple generates code for every "iteration", effectively unrolling the loop.

Further is there a flag to DMD that generates assembly code so that I myself, in the future, can investigate the code generated by DMD?

No, DMD cannot emit assembly listings. You can use a disassembler (such as obj2asm or IDA), or a different compiler instead.
Comment: If I am not mistaken, all D compilers do unroll. Even DMD. But as you say, it is not guaranteed.

Unrolled Loops of Static Arrays


What is the essence of the expression $f(\bigcup_iA_i) = \bigcup_if(A_i)$?

Question: I'm new to set theory. While reading the book "Naive Set Theory" by Paul R. Halmos, I encountered the following statement:

It is true that if $\{A_i\}$ is a family of subsets of $X$, then $f(\bigcup_iA_i) = \bigcup_if(A_i)$, but the corresponding  equation for intersection is false in genearal, and the connection between images and complements is equally unsatisfactory.

Here, I am failing to understand the essence of the expression $f(\bigcup_iA_i) = \bigcup_if(A_i)$. I cannot figure out what this expression is actually trying to convey.
From my understanding of families, if $\{A_i\}$ is a family of subsets of $X$, then $\bigcup_iA_i$ is the range of the family $A$. Now, the left hand side term $f(\bigcup_iA_i)$ is confusing me as I do not understand how we are able to feed the range of a function $A$ into the function $f$.
For the right hand side, I understand $A_i$ being the image of $A$ at the index $i$ and $\bigcup_if(A_i)$ being the union of the images obtained for $f$ at the terms $A_i$.
I am not able to understand how these two things are related to each other.
P.S. For those who  have access to the book, the statement can be found at the beginning of Section 10.
Answer: Each $A_i$ is a set.
$\bigcup A_i$ is also a set.
When you write $f(\text{set})$ you are referring to the set that you get from pushing every input from that set through $f$.
So $f\left(\bigcup A_i\right)$ is the collection of all elements $f(x)$ where $x$ comes from $\bigcup A_i$.
And this turns out to be equal to the set that you get from unioning all of the $f(A_i)$, each of which is a subset of the codomain of $f$.
Answer: Well $\bigcup_{i} A_i$ is just a set like any other set, and in particular it is defined as
$$\bigcup_{i} A_i=\{x : x\in A_i \text{ for some } i\}.$$
Thus $f\left(\bigcup_{i} A_i\right)$ is just the image of the set $\bigcup_{i}A_i$ under $f$.
Now let's use this to prove the statement, and hopefully you'll see why it makes sense. I'll use $I$ to denote the index set of the indexed family $\{A_i\}_{i\in I}$.
We start by showing that $\bigcup_{i\in I}f(A_i)\subseteq f\left(\bigcup_{i\in I}A_i\right)$. Let $x\in\bigcup_{i\in I}f(A_i)$. Then, by definition, $x\in f(A_i)$ for some $i\in I$. In particular, this means that $x=f(y)$ for some $y\in A_i$. But if $y\in A_i$, then we also have that $y\in\bigcup_{i\in I}A_i$, and so $x\in f\left(\bigcup_{i\in I} A_i\right)$.
We now show the reverse inclusion, i.e. that $\bigcup_{i\in I}f(A_i)\supseteq f\left(\bigcup_{i\in I}A_i\right)$. Let $x\in f\left(\bigcup_{i\in I}A_i\right)$. Then $x=f(y)$ for some $y\in\bigcup_{i\in I}A_i$. Then, by definition, $y\in A_i$ for some $i\in I$. In particular, this means that $x\in f(A_i)$. But if $x\in f(A_i)$, then clearly we also have that $x\in\bigcup_{i\in I}f(A_i)$.
As you can see in the two inclusions above, we use the same ideas in both directions, and the key part of the proof is the fact that if $x$ is an element of some union of sets, then $x$ is in at least one of the sets that the union is of, and vice versa. This is what allows us to "move" the union outside and inside the function as we wish, as we can always go from considering the entire union to considering one of sets the union is of, and back.


Queens Puzzle Breadth first

Question: I've been told to make a program that solves the eight queens puzzle using breadth first search. This is what i've got so far:
<code>def n_queens(n, width):
if n == 0:
    return [[]
else:
    return add_queen(n-1, width, n_queens(n-1, width))

def add_queen(new_row, width, previous_solutions):
solutions = []
for sol in previous_solutions:
    for new_col in range(width):
        if safe_queen(new_row, new_col, sol):
            solutions.append(sol + [new_col])
return solutions

def safe_queen(new_row, new_col, sol):
for row in range(new_row):
    if (sol[row] == new_col or
        sol[row] + row == new_col + new_row or
        sol[row] - row == new_col - new_row):
            return 0
return 1

for sol in n_queens(8, 8):
print sol
</code>
Is there any way to improve this?
Comment: You iterate through the "adjacent" nodes/next steps and, if they haven't been seen before, add them to the end of the queue. When you're done checking all the adjacent nodes/next steps from the current step, remove the current step currently at the head/start of the queue, then BFS on the next step in the queue/new head.
Comment: One obvious improvement would be to make it work. This code doesn't work. The first obvious bug is that the `n_queens` recursion has no base case.
Comment: That seems to be the only bug, though. After putting in a base case `if not n: return [[]]`, the code produces expected results.
Comment: BFS isn't usually implemented recursively anyway, but iteratively. It just requires you to use a deque (double-ended queue), add children to the end of the queue and pop them off the front.
Comment: @Daniel Roseman I've never used deque before. Can you give me an example of it being used in this scenario?
Comment: @KyleSum Thank you. I'm just having a bit of difficulty implementing it in code; i've just started learning programming on my own.
Comment: This requires tricky recursion for someone at your level. It could very well be an interview problem for prospective interns in the tech industry.  Dont feel bad if you cant get it, but I would encourage you to tackle easier projects first, especially if my answer below doesnt make sense.
Comment: @kylesum Thank you for your advice. I'm going to try and get my head round it otherwise it'll be constantly on my mind. I've been searching for a solution on google just so I can grasp the code but i cant find any!
Comment: Dont just look up the answer no matter how tempting it may be. First read my answer below and get out a piece of paper. Sketch out the approach. What are you trying to solve? What does that entail? Think about how to place queens and how one decision affects the next. If youre still lost, try to read explanations in english and implement it on your own. Recursion is important, and the sooner you start coding recursively the better off youll be in the future.
Comment: Tip: Python is supposed to be more convenient to test because, for example, it doesn't need to recompile. You should use this to your advantage by testing parts often. Also, look up breadth first search animations and stuff about game graphs. You need an image in your head about the problem as a graph: BFS is a graph search algorithm.
Comment: Cheers guys. Gonna keep working at it ;)
Comment: Are you cheating on your assignment? It very much sounds like it.
Answer: I don't think BFS is quite the way I'd reason about this problem. Rather, focus on recursively generating the possible placements. For each queen placed, there are only a certain number of possible placements in next row down that can't be attacked. "Place" a queen and recurse on each of those locations, and terminate when you've placed your total number of queens. Hopefully you'll recognize that a for loop mixed in with some recursive calls seems to be like a decent idea. Also remember to "pick up" the queens you placed as your recursion returns.
Answer: Agreed with previous entry. The algorithm described above is 'depth-first' search instead of 'breadth-first' search and is much more efficient for this type of problems.

Queens Puzzle Breadth first


Get all 1st data of reference key in SQL

Question: I have two tables named User and Payment. Where in Payment there is a foreign key of the user table. and here are the records of the Payment Table.
SELECT id,User_id FROM payment;

id
User_id

1
1

2
1

3
2

4
3

5
2

But my expected output is this.

id
User_id

1
1

3
2

4
3

I want only 1st data of every User_id in SQL.
Comment: Which MySQL version are you using?
Answer: Try this...
SELECT p1.id, p1.user_id
FROM payment p1
WHERE p1.id IN (SELECT MIN(p2.id) FROM payment p2 GROUP BY p2.user_id)

Assuming, by 'first' you mean the minimum value of the id in payment table for that user, and you want some more details also out of payment table OR do something else with it as well...
If you just want the MIN payment id, then simply this would work:
SELECT MIN(id), user_id FROM payment GROUP BY user_id

Answer: You can use GROUP BY clause on the User_id column.


trying to read every line in file, output always 'cat $file'

Question: I am trying to display every line of a file in the terminal but the output is always:

cat $file

this is my code:
#!/bin/bash
file="users.csv"
IFS=''
echo "bobama, Barack Obama" > $file
echo "gbush, George Bush" >> $file
for line in `cat $file`;
do
        echo $line;
done

Comment: Even though this is not the way to loop through the content of a file, it does work to me. See [How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?](http://mywiki.wooledge.org/BashFAQ/001) for the approach recommended by 9 out of 10 dentists.
Comment: why dont you just write `cat $file` instead of looping it over it?
Answer: I solved it by replacing 'cat $file' with $(cat $file).
You can find a better explanation here:
Why can't you use cat to read a file line by line where each line has delimiters
Comment: Your question says `\`cat $file\`` Your answer says error was due to `'cat $file'`. Both are different.


Lagrange Multiplier, maximum value to show a equation

Question: I was doing a exercise and I don't understand a part of it.

Calculate the maximum value of $f (x, y, z) = \ln x + \ln y + 3 \ln z$ on the portion of the sphere $x^2+y^2+z^2=5r^2$ in the first octant. Use this result to show that $abc^3\leq 27\big((a+b+c)/5\big)^5$, for all $a,b,c >0$.

I got it do the first part of the problem, but i do not know what i should have to do in the second part, that ask to show that expression.
ty
I took a picture :)
Comment: Welcome to Math.SE! Try formatting your question with [MathJax](https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference) to make it more readable. Also, using an image to represent things that could be typed is inaccessible (_e.g._ to users with screen readers). Try taking a few minutes to type it out (you're more likely to get an answer that way!)
Answer: Alt. hint (without Lagrange multipliers): $\;f(x,y,z)=\ln(xyz^3)\,$, and since $\,\ln\,$ is strictly increasing, the maximum value of $\,f\,$ is attained at the point which maximizes $\,xyz^3\,$. But by AM-GM:
$$
\frac{1}{27}x^2 y^2 z^6 = x^2 y^2 \frac{z^2}{3} \frac{z^2}{3} \frac{z^2}{3} \le \left(\frac{x^2 + y^2 + \frac{z^2}{3}+\frac{z^2}{3}+\frac{z^2}{3}}{5}\right)^5 = \left(\frac{x^2+y^2+z^2}{5}\right)^5 = r^{10} \tag{1}
$$
Therefore $\,xyz^3 \le 3 \sqrt{3}\,r^5\,$ with equality iff $\,x=y=\dfrac{z}{\sqrt{3}}\,$.
Also, $\,(1)\,$ can be written as $\;abc^3 \,\le\, 27 \,\left(\dfrac{a+b+c}{5}\right)^5\,$ where $\,a=x^2, b=y^2, c=z^2\,$.
Comment: Thanks it was really helpfull :)
Answer: Denote: $x=\sqrt{a}, y=\sqrt{b}, z=\sqrt{c}$. Then:
$$\begin{align}f (x, y, z) = \ln x + \ln y + 3 \ln z&\le 5\ln r+3\ln \sqrt{3} \Rightarrow \\
\ln (xyz^3)&\le \ln (r^5\cdot \sqrt{27}) \Rightarrow \\
xyz^3&\le \sqrt{27}\cdot r^5\Rightarrow \\
\sqrt{abc^3}&\le \sqrt{27}\cdot \left(\sqrt{\frac{a+b+c}{5}}\right)^5 \Rightarrow \\
abc^3&\le 27\cdot \left(\frac{a+b+c}{5}\right)^5.\end{align}$$
Comment: Thanks for sharing your knowledge with me :)
Comment: you are welcome. good luck.


How do we show that Rado's function (for the Busy Beaver Algorithm) is non-computable?

Question: Rado's function $\Sigma (n)$ gives us the maximum number of 1's that a Turing machine may write on an initially blank tape. It is widely known that this function is non-computable by a Turing machine, but I am struggling to find a proof of this.
I have found one proof (here), but I'm not entirely sure that I understand the idea behind it, which states that

We can show that Rado's function $\Sigma (n)$ is non-computable by showing that if $f(n)$ is any computable function, then there exists $n_0$ such that $\Sigma (n) > f(n)$ for all $n \geq n_0$.

Why does showing this (which I assume is equivalent to showing that Rado's function grows more quickly than any other computable function) prove that Rado's function is non-computable?
Alternatively, how else might we prove the non-computability of Rado's function?
Answer: Rado's function grows more quickly than any computable function. This means Rado's function must be non-computable, since otherwise it would grow faster than itself.

In detail: we know that for all computable $f$, there is some $n$ such that $\Sigma(n)>f(n)$ (we actually know much more than this, but this is all I need here).
Now suppose $\Sigma$ were computable. Then we could take $f=\Sigma$ in the above (since the only restriction on $f$ is "$f$ is computable"), which would mean that there is some $n$ with $\Sigma(n)>f(n)=\Sigma(n)$. And this is clearly impossible.

I think the confusion creeps in when you write:

I assume is equivalent to showing that Rado's function grows more quickly than any other computable function

(emphasis mine). What we know is that $\Sigma$ grows faster than all computable functions, period, not just all computable functions except possibly $\Sigma$ itself. That's why we get a contradiction. Similarly, we know that the Ackermann function isn't primitive recursive since it grows faster than any primitive recursive function, and so on.

It's worth noting,by the way, that there is no fastest growing computable function: if $f$ is computable, so is e.g. $x\mapsto f(x)^2$.
Comment: But would showing that $\Sigma (n) > f(n)$ not just prove that $\Sigma (n)$ is the fastest growing computable function?
Comment: @JChapman No, because - if $\Sigma$ were computable - we would have to have $\Sigma(n)>\Sigma(n)$ (that is, we could take $\Sigma=f$). Alternatively, if you're not happy with that for some reason think about $f(x)=\Sigma(x)+1$ - this $f$ is computable if $\Sigma$ is, so if $\Sigma$ is computable we have $\Sigma(x)>f(x)$ ...
Comment: @JChapman I've edited my answer to be more explicit.
Answer: $\Sigma(n)$ gives the maximum number of ones a Turing  machine of length $n$ (with no arguments) may write before it halts.
If $\Sigma$ is computable then we can see it as a TM. Modify it to take the input as an integer encoded in base $2$, and to output the result $m$ by writing $m+1$ ones on the tape.
What do you get for $\Sigma(n)$ for $n > L+\log_2(n)$ where $L$ is the length of $\Sigma$'s source code ?