Search API: Custom processor not hitting addFieldValues

Question: I'm creating a custom module for adding a processor that adds relative entities. Here's my code for getPropertyDefinition.
public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
$properties = [];
if ($datasource) {
  $entity_type = \Drupal::service('entity_type.manager')->getStorage('group_type')->load(key($datasource->getBundles()));
  $entity_type_ids = [];
  $plugin_manager = \Drupal::service('plugin.manager.group_content_enabler')->getInstalled();
  foreach($plugin_manager->getInstanceIds() as $instance_id) {
    array_push($entity_type_ids, $plugin_manager->get($instance_id)->getPluginDefinition()['entity_type_id']);
  }
  foreach($entity_type_ids as $entity_type_id) {
    $definition = [
      'label' => $this->t(ucwords($entity_type_id)),
      'description' => $this->t('Entity reference of ' . $entity_type_id),
      'type' => 'entity:' . $entity_type_id,
      'processor_id' => $this->getPluginId(),
    ];
    $properties[$entity_type_id] = new EntityProcessorProperty($definition);
    $properties[$entity_type_id]->setEntityTypeId($entity_type_id);
  }
}
return $properties;
}

And, after adding relative fields to index, I think it should hit addFieldValues method but it doesn't. For test purpose, I only put die in the method.
public function addFieldValues(ItemInterface $item) {die;}

But, when I copied code from addURI's getPropertyDefinition into my getPropertyDefinition, it hits addFieldValues when indexing. Is there anything wrong in my getPropertyDefinition?
Comment: I'm voting to close this question as off-topic because it's a bug report for a module and needs to be handled in the issue queue
Answer: EDIT
I created an issue on the search API issue queue Custom Entity Reference processor does not trigger addFieldValues()* and the maintainer has pointed to an existing issue that it was duplicating. So there is an existing issue that is being worked on here - #2910638: Entity reference via processor sample code not working Jump on there and help test :)

I couldn't post the first link because of a lack of reputation and I figured the link to the issue where work is being carried out is more useful :D

I'm encountering the same behaviour and have an idea why this occurs but not an answer on how to fix it (I can't post a comment so needed to add this as an answer).
\Drupal\search_api\Item\Item::getFields is responsible for calling the instances' implementation of addFieldValues. Within that function there is this code block:
   foreach ($this->index->getFieldsByDatasource($datasource_id) as $field_id => $field) {
      // Don't overwrite fields that were previously set.
      if (empty($this->fields[$field_id])) {
        $this->fields[$field_id] = clone $field;

        $field_data_type = $this->fields[$field_id]->getType();
        // If the field data type is in the fallback mapping list, then use
        // the fallback type as field type.
        if (isset($data_type_fallback_mapping[$field_data_type])) {
          $this->fields[$field_id]->setType($data_type_fallback_mapping[$field_data_type]);
        }

        $property = $field->getDataDefinition();
        if ($property instanceof ProcessorPropertyInterface) {
          $processors_with_fields[$property->getProcessorId()] = TRUE;
        }
        elseif ($datasource_id) {
          $fields_by_property_path[$field->getPropertyPath()][] = $this->fields[$field_id];
        }
      }
    }
    try {
      if ($fields_by_property_path) {
        \Drupal::getContainer()
          ->get('search_api.fields_helper')
          ->extractFields($this->getOriginalObject(), $fields_by_property_path, $this->getLanguage());
      }
      if ($processors_with_fields) {
        $processors = $this->index->getProcessorsByStage(ProcessorInterface::STAGE_ADD_PROPERTIES);
        foreach ($processors as $processor_id => $processor) {
          if (isset($processors_with_fields[$processor_id])) {
            $processor->addFieldValues($this);
          }
        }
      }
    }

If the value of $property is not an instanceof ProcessorPropertyInterface then addFieldValues() is never called.
What I'm not sure of is what is the best path to take to begin solving this.

Should I add a new custom processor that only acts on this custom field and use addFieldValues() there? (I've yet to test this) OR
Is it an issue that addFieldValues() is never called and the Search API module needs to be updated so that it is?

I've tried implementing the other functions from the parent class (\Drupal\search_api\Processor\ProcessorPluginBase) ie alterIndexedItems but none of those are triggered.
I came across the same approach that you're using here on d.o - Adding an entity reference via a processor so it may be the docs need to be updated.
Comment: Great. The new patch hits the method now. Thank you very much!


Difference of a likelihood function for a vector and a single value

Question: $p(x\mid C)$ is defined as the probability density of a point $x$ given that it belongs to a class $C.$ But what of $p(\mathbf{x}\mid C)$ where $\mathbf{x}$ is a vector?
I'm finding hard to understand this concept as a vector is made up of elements (i.e. $\mathbf{x}=(x_1,\dots,x_n)$.
Answer: I presume your question is in the context of the classification/discriminant analysis where you want to classify observations into classes. The concept from a single value to the vector value is nothing but just an extension to a higher dimension.
In the single-valued scenario, think as if you had a single feature (say, only weight of the individuals) available with you for performing the classification (classifying individuals among certain classes) - while in the vector-valued scenario, think as though you have more than one features (say, weight, height, age of the individuals) available with you based on which your decision boundary will be decided in the classical problem of segmentation.
While working on classification problems, you need to make certain assumptions about the distribution of the features. Thus, you end up having the probability of the random vector taking a vector-value given that this point belongs to a certain class C [based on the joint density of any random feature vector conditional on class].
Comment: Lets say the joint probability density you just mentioned is p(**x**,C). How would **x** vary? Would this just depend on the data points that I have (i.e. **x** are discrete vector points)? What if I wanted to find p(x∣C) but my observations are in vector points?


TFS Merging keeps merging some files without any changes

Question: For some unknown reason, TFS seems to keep queuing certain files for merges, even though there are no changes. In the Pending Changes windows, the Change column only states Merge, and not the usual Merge, edit or Merge, branch.
Nobody touches these files and yet they keep reappearing under the pending changes merge queue.
I've seen this happen before with a folder that was deleted, but kept reappearing in the merges between branches. In that case, I used tf destroy in all branches for that folder's path and it solved the problem. However, in this environment, it's with files that the team wants to keep.
Has anyone else experienced and/or resolved this issue?
Answer: Try TFS 2010 SP1 and CU1. It fixed some merge bugs. Like the following:

Merge or discard in both directions cause unnecessary "empty" merges
  in future merge attempts.
Comment: uh, this looks very interesting and seems to hint in the right direction. We are seeing "empty merges" on a daily basis. I will backup/update our TFS to see if it works out.
Comment: @Simon did this work you for you? I have not been able to get out TFS server down to apply these updates.
Answer: If you have the Team Foundation Power Tools installed you can use tfpt uu to undo changes to any unchanged files (yes, it's well annoying when files are flagged as changed and you get told they are identical!).
Comment: This doesn't work on merges.  You can however undo the pending changes, then select "No to all" when it asks you if you want to undo files that actually changed.
Comment: The "No to all" will be applyed only to modified files. Newly added files will not be merged. As stated here: http://stackoverflow.com/questions/15619885/files-listed-as-pending-changes-when-doing-merges-that-didnt-change


adonis migration:run fails on Gitlab deployment

Question: I am trying to delpoy Adonis JS (REST) on Gitlab.
When the job hit this script line: adonis migration:run,  the pipeline fails with this error message:
{ Error: connect ECONNREFUSED 127.0.0.1:3306
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1083:14)
    --------------------
    at Protocol._enqueue (/builds/begueradjitest/node_modules/mysql/lib/protocol/Protocol.js:144:48)
    at Protocol.handshake (/builds/begueradjitest/node_modules/mysql/lib/protocol/Protocol.js:51:23)
    at Connection.connect (/builds/begueradjitest/node_modules/mysql/lib/Connection.js:118:18)
    at /builds/begueradjitest/node_modules/knex/lib/dialects/mysql/index.js:95:18
    at Promise._execute (/builds/begueradjitest/node_modules/bluebird/js/release/debuggability.js:313:9)
    at Promise._resolveFromExecutor (/builds/begueradjitest/node_modules/bluebird/js/release/promise.js:483:18)
    at new Promise (/builds/begueradjitest/node_modules/bluebird/js/release/promise.js:79:10)
    at Client_MySQL.acquireRawConnection (/builds/begueradjitest/node_modules/knex/lib/dialects/mysql/index.js:90:12)
    at create (/builds/begueradjitest/node_modules/knex/lib/client.js:280:23)
    at tryPromise (/builds/begueradjitest/node_modules/tarn/lib/Pool.js:366:22)
    at tryPromise (/builds/begueradjitest/node_modules/tarn/lib/utils.js:57:20)
    at Promise (/builds/begueradjitest/node_modules/tarn/lib/Pool.js:366:5)
    at new Promise (<anonymous>)
    at callbackOrPromise (/builds/begueradjitest/node_modules/tarn/lib/Pool.js:357:10)
    at Pool._create (/builds/begueradjitest/node_modules/tarn/lib/Pool.js:307:5)
    at Pool._doCreate (/builds/begueradjitest/node_modules/tarn/lib/Pool.js:275:32)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3306,
  fatal: true }

I am using MySQL to run the migrations.
What should I do ?
Comment: The error seems to come from GitLab. Did you add a mysql service on your GitLab pipeline file?
Comment: Yes I did add MySQL first
Comment: Can you show us the content of your gitlab pipeline file?
Answer: You need to use this command :
> node ace migration:run

Adonis ACE documentation


Slow query in Eloquent using accessor

Question: I have a Model with an accessor that gets the last element related (through another table/model) with it. It comes in handy for individual queries but now I need to make a summary and the query takes way too long.
The model is called Device and it has the following accessor:
public function getPreviousDeviceAttribute()
    {
        if ($this->pos_id > 0){
            $pos = Pos::find($this->pos_id);
            $device = $pos->devices()->newPivot()->where([
                    ['device_id', '<', $this->id],
                    ['pos_id','=', $this->pos_id]
                ])->get()->sortByDesc('id')->first();
            return $device;
        } else {
            return null;
        }

    }

From a Controller I need to query all the previous_device of a collection.
This is the query:
$c_pendientes = Device::whereNotNull('fecha_instalacion')
                ->whereNull('fecha_reversa')
                ->where('pos_id', '>', 0)
                ->where('customer_id', 1)
                ->get();

$pendientes = $this->seriesPendientes($c_pendientes);

public function seriesPendientes($pendientes)
    {
        $count = 0;

        foreach($pendientes as $device)
        {

            $prev = $device->previous_device;
            if($prev instanceof App\Device)
            {
                if(is_null($prev->fecha_reversa))
                {
                    $count++;

                }
            }
        }
        return $count;
    }

EDIT:
Attributes:
Device
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `serial` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
    `rotulo` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
    `code` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
    `cost_center` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
    `guia_recepcion` INT(11) NULL DEFAULT NULL,
    `guia_reversa` INT(11) NULL DEFAULT NULL,
    `pep` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
    `modified_by` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
    `modified_on` DATETIME NULL DEFAULT NULL,
    `fecha_recepcion` DATETIME NULL DEFAULT NULL,
    `fecha_instalacion` DATETIME NULL DEFAULT NULL,
    `fecha_reversa` DATETIME NULL DEFAULT NULL,
    `status_id` INT(10) UNSIGNED NOT NULL,
    `location_id` VARCHAR(255) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `customer_id` INT(10) UNSIGNED NOT NULL,
    `str_id` INT(10) UNSIGNED NOT NULL,
    `model_id` INT(10) UNSIGNED NOT NULL,
    `pos_id` INT(11) NOT NULL,
    `user_id` INT(10) UNSIGNED NOT NULL,
    `technician_id` INT(10) UNSIGNED NOT NULL,
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL
)

Pos
CREATE TABLE `pos` (
    `id` INT(11) NOT NULL,
    `vigente` BLOB NOT NULL,
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)

Relationship
CREATE TABLE `devices_pos` (
    `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    `device_id` INT(10) UNSIGNED NOT NULL,
    `pos_id` INT(10) UNSIGNED NOT NULL,
    `created_at` TIMESTAMP NULL DEFAULT NULL,
    `updated_at` TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)

Comment: Ok, just edited it.
Comment: Please, post your database model to help people helping you :) at least the attributes of the tables related to the question. :)
Comment: You just want to count the "piendientes" ?
Comment: Yes, that's what I want.
Answer: If you are calling this getter for each item in a large collection, the $pos = Pos::find($this->pos_id); line will do one request for each item. You could avoid it by defining the following relation if it does not exist yet
public function pos()
{
   return $this->belongsTo(Pos::class);
}

And then eager load this relation (one request for all the items) :
$c_pendientes = Device::with('pos')
                ->whereNotNull('fecha_instalacion')
                ->whereNull('fecha_reversa')
                ->where('pos_id', '>', 0)
                ->where('customer_id', 1)
                ->get();

You should probably eagder load pos.devices too like :
$c_pendientes = Device::with('pos')
                ->with('pos.devices')
                ->whereNotNull('fecha_instalacion')
                ->whereNull('fecha_reversa')
                ->where('pos_id', '>', 0)
                ->where('customer_id', 1)
                ->get();

Let me know if it helped.
Comment: Remember I need to get the device related to the original one. It'd be good to just make one query from the controller and avoid the accessor but I'm not sure how the answer addresses that part of the problem.
Comment: Also the relationship at the beginning exists but it's a belongsToMany

Slow query in Eloquent using accessor


How do you load a node inside of another node?

Question: A node has several text links or thumbnails loaded from a view. For the purpose of this question, lets suppose they're thumbnails and the area with the thumbnails lets call it the "head" of the node.
Those thumbnails link to other nodes. To be able to understand the scenario am trying place here. Lets call the nodes that the thumbnails link to "Subnodes". The Area under the thumbnails, is a area where you want to load a "subnode" from the thumbnail clicked. Lets call this area the "footer".
The goal here is keep the original node with the thumbnails or text links ("Header") at the top of the page AND load the subnode in the "footer". The ideal method would involve some AJAX to keep the page load away from the end user. Although this is not required because a solution with a page load is acceptable.
Extra consideration to anyone who purposes a solution using AJAX.
Comment: Down voting the question and not commenting on it to help me improve the question is not very useful to anyone...
Comment: It is difficult to understand what you are asking; try leading your question with a clear question: "How do I load a node on a page using AJAX?"
Comment: Well the solution doesn't HAVE to be AJAX. I'm more concerned about getting a solution that would explain how to load one node inside of another. I will modify my question and hopefully that will clarify the misunderstanding.
Comment: please explain how those thumbnails are generated in the first place?  Are they node reference fields that are populated in the original node or something else? also, what version of drupal are you running?
Answer: Take a look at the Peekaboo; Ajax field formatter module.

Provides the ability to load the contents of a field via Ajax.
Use cases

Field output that is heavy on resources
  Some fields, such as the *reference fields, can contain a tremendous amount of overhead when formatting the output for the field. This module allows the page output to load without calculating the formatted output of the field, and the field contents can be subsequently loaded into the page with Ajax.
Field output that takes up too much space
  Peekaboo can be used to initially hide certain fields that take up a lot of space, like maps or images, only to be revealed later at the user's request.
Fields that reveal more information
  Peekaboo can also be used purely for the Ajax functionality as a gimmick to reveal more information.
Comment: Cool module, never heard of that one


Audiation: dealing with key changes when you hear in terms of solfege

Question: So, I'm a musician who believes very much in the value of audiation: that is, not only hearing pitches in my head, but also hearing them in terms of both the current harmony and the current tonal center. I learned to do this by hearing in terms of moveable-do solfege syllables, so for example if I'm hearing a blues on the radio I'll hear the bass line moving from do, to fa, to do, to sol, then to fa, etc. This works really well for me when the tonal center in question is pretty stable, even in the presence of chromaticism, and I've practiced it to the extent that I can compose homophonic four-voice harmonic progressions as well as various species of counterpoint exercises (in two voices) in my head when I'm on the train or before I go to sleep.
My question concerns how to audiate melodies and harmonies when the tonal center changes rapidly, either because of a rapidly modulating tonal center (like you see in early Schoenberg) or because of a brief tonicization of a secondary harmony. In music school, I was a big fan of Schoenberg's "Structural Functions of Harmony," which puts forth a pretty expansive notion of how even remote key areas still relate to the governing tonic of the piece, and so it's tempting to try to understand such passages in terms of the "original" key, using chromatic solfege syllables (e.g. I'd hear V/V in C major as re-fi-la, with "fi" being the solfege syllable for #4), but this becomes very difficult when relatively remote chords such as the major chord on the seventh degree are tonicized. On the other hand, it feels naive to "switch keys" in my head to whatever chord is being tonicized in the moment, and is moreover a difficult thing to predict when attempting to sight-read/sight-audiate a harmonically complex piece. Given all that, I suspect there are three potential answers to my quandary:

Just get better at conceiving tonicizations and brief modulations in terms of the "home" key of the piece, using the relevant chromatic solfege syllables.
Use the solfege syllables relevant to the tonal center being tonicized, while reading ahead to predict what exactly that tonal center might be in cases where it's initially ambiguous.
Stop hearing solfege syllables internally as a means of understanding the relation of any given pitch to the chord or the tonal center in favor of some other approach.

I vaguely suspect that option three is the likely answer, since I assume that a lot of musicians that are effective audiators even in the presence of rapidly changing harmonic contexts and tonal centers (like post-bop jazz players) don't hear tones in terms of solfege but still understand them not just as a sensory phenomenon but also in terms of their relation to the harmony and the key. I'd love any insight into the matter.
Answer: I grapple with very similar issues. I've been big on ear training the last 2-3 years and have taught myself to use moveable-do solfege quite well in one key, but I do struggle with how to handle key changes, especially the quick or ambiguous tonicizations you describe.
There are definitely moveable-do fans who are willing to move the tonic a lot. Even for atonal pieces, they will just constantly move the tonic as often as every few notes.
I've tried all three of your approaches and they all work to some degree. I've found combining #1 and #2 can be fruitful: if you have a tricky passage that goes by really quick or could be heard with two possible tonics, first try to hear the whole passage in the same key and use your home key syllables. Then try the same passage but try to use the syllables of the new tonic right away when you hit the key change. In all cases, try and make sure the syllables you are using correspond to the tonic that you are currently hearing—no matter whether it's the syllables or your own ear that initially triggers the shift. If you don't do that, syllables tend to become decoupled from their meaning of "this note means this scale degree of the key that I am in," which kind of defeats the whole purpose of moveable-do.
If you try to stick with one way or the other throughout a passage, and it just won't "give," then you've got to check if it's your solfege skills, or it's just that the piece isn't so ambiguous after all. Worst case, you can resort to playing it on your instrument and trying different bass notes to see if you can convince yourself about the various possible keys. And of course, if you do it long enough it just becomes second nature and you can try out the possible keys in your head with good accuracy.
I realize that a lot of the above doesn't quite apply to sight reading/singing where you don't have the option to go through multiple times, but I don't really sight read or sight sing, so I can't give you much on that situation.
So that's my take on options 1 and 2. Flawed, but doable, and some combination is probably the best choice. Option #3 just feels like giving up, honestly. Solfege is a powerful learning tool at almost any level and this stuff isn't anywhere near the level of complexity where it could start to break down.
Lately I've been exploring a fourth option: just give in and use fixed-do, where appropriate. Then you can simply sing the syllables for the notes, and just use your ear to decide how to interpret the harmony.
I much prefer chromatic fixed-do. Although it's a lot to learn and less popular than the conventional fixed-do, I think it's badly underrated, and it has two extra advantages for people coming in from moveable-do:

If you start learning chromatic fixed-do with pieces in C Major (plus C Minor or A Minor, depending on how you handle minor keys), the process is literally identical to using moveable-do without key changes, i.e. your option 1. Then once you're comfortable in C, you can gradually you can move left and right in the circle of fifths: try modulating to F using "te" for the B♭ or to G using "fi" for the F♯, etc. I have just started to do this, but it's coming along.
You still retain all the information about chromatic intervals that we so cherish in moveable-do. The distance from a "re" to a "fa" is always a minor third no matter what key you are in, etc. Also especially helpful when you're trying to be really mindful of all of the harmonic details, which is the goal here.

Fixed-do is well attested as a way to get better at hearing and understanding complex harmony. Some recent research:
One study from 2012 with eighty-five subjects found that people trained in fixed-do are better at learning complex diatonic and chromatic harmony than those trained in moveable-do, with very large effect sizes.
Another study from earlier this year found that 14% of subjects were able to acquire a basic form of absolute pitch as adults with just 12-40 hours of training (with comparable accuracy to natural absolute pitch possessors). Obviously a certain amount of absolute pitch is a huge help when dealing with harmony, and chromatic fixed-do is essentially like constantly training yourself in absolute pitch!
Comment: The point about chromatic fixed-do being equivalent to moveable-do in highly chromatic circumstances is a great one, and I really appreciate the links to relevant studies! I'm going to work on using fixed-do syllables in my hearing. Thanks!
Answer: Solfege works well diatonically, and also with a little modulation. Try it with fixed Doh. It's very confusing initially, but may be a way round.
For me just using real notes would be the way to go. If you don't know the key, and therefore can't audiate in real terms, just choose a key (I'd start on C) and work from there. With movable Doh, you're half way there.


Google Sheets How to find the Top 3 closest columns to a given column

Question: I have a table in google sheets like this one,
-------------------
   | A | B | C | D |
-------------------
1  |C1 |C2 |C3 |C4 |
2  | 1 | 2 | 1 | 2 |
3  | 2 | 3 | 4 | 3 |
4  | 5 | 7 | 1 | 6 |
-------------------

My goal is to find which 2 columns C1,C2,C3 are closest to C4,
by calculate the average difference bewteen each column and column C4,
e,g Column C1 will have an averyage of abs( (  (1-2)+(2-3)+(5-6)  )  /3 )
which is , abs( (  (A2-D2)+(A3-D3)+(A4-D4) )/(number of rows) )
I'm using ARRYFORMULA to get the average differece for one column and then I drag it horizontally so As will increase to Bs and so on
=ArrayFormula({A1;abs(average(  (checks if there is empty cell)  ,$D2:$D-(A2:A) )))})
if I use it in cell Z1, Z1 will show 'C1', and Z2 will show the average difference for column C1
but i'm not sure how to use a single nested formula to do it for all columns A:C  at once, with out having to drag it
like I if I type =FORMULA(...) in Z1, and a table will show up
Thank you
Answer: Try the formula:
=QUERY(ARRAYFORMULA(ABS((ROW(A2:C)*COLUMN(A2:C))^0*D2:D26-A2:C26)),
"select avg(Col"&JOIN("), avg(Col",ArrayFormula(row(INDIRECT("A1:A"&COLUMNS(A2:C)))))&")")
Explanation

(ROW(A2:C)*COLUMN(A2:C))^0*D2:D26 -- copy C4 to compare it with others
"select avg(Col"&JOIN("), avg(Col"... -- compose query to get the average for each column.

Note: in your formula abs(average( must be replaced → average(abs( in order to complete abs function first.


Layer 4 vs Layer 7 Load Balancing

Question: I am trying to decide between using a layer 4 load balancing solution for my datacenter or a layer 7 solution.  Unfortunately (for my sanity, that is), my use case is simple enough that both solutions would work well, avoiding most of the weaknesses and not really utilizing the strengths on the other.  Whatever solution we end up using, it has to have high availablity and high throughput.  But we are only planning to use it to load balance over a cluster of web servers, none of which have any requirements for "sticky" session management (cookie or IP), complex rewrite rules - or, for that matter, any rewrite rules at all.
The load balancers will be connected to two switches, both with an independent connection up to the datacenter aggregation layer and merged together using Rapid Spanning Tree and whatever proprietary protocol that the switches use for virtualizing.  The load balancers will also be cross-linked to each other over a crossover cable.  All of the servers in the cluster are connected to both switches.  All that the load balancers have to do is point the traffic over them.
Since it's just HTTP, I can use a layer 7 load balancing solution like HAProxy or nginx.  But I could also use the LVS project with ldirectord or keepalived or whatever.
I've tried to break up the pros and cons as I see them, but it just ends up in a wash.  What would you recommend and why?  Am I missing something?
Answer: One useful benefit of "L7" like haproxy is being able to use cookies to keep the same browser hitting the same backend server.  This make debugging client hits much easier.
L4 balancing may bounce a single user around on several backend servers. (which in certain cases may be advantageous, but in a debugging/profiling sense, using "L7" is much more valuable.)
EDIT:
There's also a potential speed advantage of using HTTP balancing.  With keep-alives clients can establish a single TCP session to your balancer and then send many HITs without needing to re-establish new TCP sessions (3-way handshake).   Similarly many LBs maintain keep-alive sessions to back end systems removing the need to do the same handshake on the back end.
Strict TCP load balancing may not accomplish both of these as easily.
/*
FWIW: I wouldn't say "L7" or "L4", I would say HTTP or TCP. But I'm a stickler for avoiding using the OSI to describe things it doesn't match up with well.
*/
I think fundamentally if you're not sure what to deploy, go with what feels simple and natural to you.  Test it (use apache bench?) and make sure it works for your needs.  To me HTTP LB is more natural.
Comment: Stickiness, cookie based or IP based, is certainly an advantage of L7 switching.  But it's not one that our application would be able to particularly make use of.
Comment: Wouldn't one disadvantage of HTTP level load balancing be that you would have to have a TCP level load balancer in front of the HTTP balancers to enable the failover between the two?
Comment: @Scrivener - you shouldn't, no. round-robin DNS can take care of that I believe, unless I'm misunderstanding your question.
Comment: @mfinni: The global geographic DNS will be able to point to one IP per datacenter.  I need something to respond to that IP.
Comment: I see. Well, it depends on the capabilities of your device(s). You can probably find a L7-capable device that can work in pairs with a single cluster VIP that wouldn't require a hardware TCP/IP load-balancer. If IIS and MS Windows NLB can do it, I imagine most other commercial products can do it.
Comment: I can do it with LVS. But then I'm using a L4 load balancer in front of an L7 load balancer instead of just using the L4 load balancer in the first place....
Comment: As I said, there is clustering technology that doesn't require additional hardware. If you're going to use HAproxy and LVS, why not build them on the same hardware?
Comment: Thank you for your advice.  I'll go with what I know best.  I'm just glad I didn't lose sight of any major things.  I was going up the wall, sure that I was missing something.
Comment: With an advanced Layer 7 load balancer, you can also monitor the servers and automatically take out a server if for example a page returns a status 500. It can also cache resources, and offload encryption.
Comment: Don't L4 load balancers offer sticky sesions as well? In this case based on TCP attributes (e.g. IP address) but isn't it just as useful as relying on cookies at L7 level?
Answer: Given the lack of advantage to you from doing L7 balancing, I'd settle on L4 balancing instead.  I'm a big fan of keeping it as simple as possible, without sacrificing too much.
L7 requires the balancers to inspect the http headers in the packets that are going through them for appropriate routing, adding additional overhead and a marginal increase in latency for the end user.  It seems a pointless expense to me if you'll gain nothing by it.
Answer: Some DNS providers have simple failover functionality.  You've mentioned what your requirements are not and not what they are, but if all you need is round robin with failover if something's down, then you could use e.g. zoneedit.com's Failover. Depending on your HA needs that may be good enough and you get to skip a whole tier in your architecture.
Comment: I wish it was that simple - we need something more like round-robin with fail-over, plus geographic separation. All of that is not in the question, however, because it's being done by an outside company.
Comment: What do you mean - I guess I mean DNS being done by an outside company too, and some of them support both geo-load balancing and failover as a DNS service - or you mean there's some additional third party between you and the DNS provider, or that you just don't have direct say over the DNS?
Comment: The former - we're already doing DNS with an outside company that's doing failover and geographic load balancing to the different datacenters.  I just need to load balance it inside the datacenter.
Comment: You can use the same round robin for server by server on the front end, right?  DNS for round robin load balancing is often used for a single data center; multiple geolocations is a big boy requirement atop that.

Layer 4 vs Layer 7 Load Balancing


How to write $1 \times 3 \times 5 \times \cdots \times (2n-1)$ as a sequence?

Question: I am a second year IB Mathematics HL student and I am trying to figure out how to write the equation for the following sequence:
1×3×5×...×(2n-1)
I'm pretty sure it involves factorials, but (2n-1)! doesn't work, and neither does 2(n!)-1, obviously.
If you know how to do this or can suggest another approach, I would really appreciate your help!  Thank you!
Comment: $$1\times3\times\cdots\times(2n-1) = \dfrac{1\times2\times3\times\cdots\times(2n)}{2\times4\times\cdots\times(2n)} = \dfrac{(2n)!}{2^nn!}$$
Comment: See also: [Factorial#Double_Factorial](https://en.wikipedia.org/wiki/Factorial#Double_factorial).
Comment: @KennyLau:  That is an answer, not a comment.  It solves the problem nicely.
Comment: That makes sense, because we can write all the even numbers in the numerator as 2×X, when X goes from 1 all the way to n.  Then, we can get rid of all the X by dividing by n!, and get rid of all the 2s of the 2×X by dividing by 2^n.  Thank you so much!
Answer: You have a notation for the multifactorials, which takes into account only one factor on $k$.
For instance, the double factorial is defined recursively by the relation
$$\begin{cases}n!!= 1&\text{if }-2<n\le 0,\\ n\cdot (n-2)!!&\text{if } n>0.\end{cases}
$$
Thus,
$$(2n-1)!!=1\cdot 3 \cdot 5\cdots (2n-1),\qquad (2n)!!=2\cdot 4\cdot6\cdots 2n.$$
Also, note that $ \; n!=n!! \, (n-1)!!$.
Similarly, you have a triple factorial, defined by
$$n!!!=\begin{cases} 1&\text{if }-3<n\le 0,\\ n\cdot (n-3)!!!&\text{if } n>0.\end{cases}
$$
Answer: This is a good exercise
in using product notation.
$\begin{array}\\
f(n)
&=1\ 3\ ...\ (2n-1)\\
&=\prod_{k=1}^n (2k-1)
\qquad\text{This is the product of the first $n$ odd numbers}\\
&=\dfrac{\prod_{k=1}^n (2k-1)\prod_{k=1}^n (2k)}{\prod_{k=1}^n (2k)}
\qquad\text{join the even numbers to the odd ones}\\
&=\dfrac{\prod_{k=1}^{2n} k}{2^n\prod_{k=1}^n k}
\qquad\text{numerator is the product of the first $2n$ numbers}\\
&=\dfrac{(2n)!}{2^nn!}
\qquad\text{renaming product of first consecutive numbers with factorials}\\
\end{array}
$
Answer: You may use Π(2k-1) for k=1 to k=n. The upper-case letter Π is used as a symbol for: The product operator in mathematics, indicated with capital pi notation ∏ (in analogy to the use of the capital Sigma Σ as summation symbol).


ofstream open modes: ate vs app

Question: This question asks the difference between app and ate and both the answers and cppreference imply that the only difference is that app means that the write cursor is put at the end of the file before each write operation, whereas ate means that the write cursor is put at the end of the file only when opening.
What I'm actually seeing (in VS 2012) is that specifying ate discards the contents of an existing file, whereas app does not (it appends new content to the previously existing content). In other words, ate seems to imply trunc.
The following statement appends "Hello" to an existing file:
ofstream("trace.log", ios_base::out|ios_base::app) << "Hello\n";

But the following statement replaces the contents of the file with just "Hello":
ofstream("trace.log", ios_base::out|ios_base::ate) << "Hello\n";

The MSDN documentation for VS 6.0 implies that shouldn't happen (but this sentence seems to have been withdrawn in later versions of Visual Studio):

ios::trunc:   If the file already exists, its contents are discarded.
  This mode is implied if ios::out is specified, and ios::ate, ios::app,
  and ios:in are not specified.
Answer: You need to combine std::ios::in with std::ios::ate then seek the end of the file and append the text:
Consider I have a file "data.txt" which contains this line:
"Hello there how are you today? Ok fine thanx. you?"

Now I open it:
1: std::ios::app:
std::ofstream out("data.txt", std::ios::app);

out.seekp(10); // I want to move the write pointer to position 10

out << "This line will be appended to the end of the file";

out.close();

The result is not what I wanted: No moving the write pointer but only the text is always appended to the end.
2: std::ios::ate:
std::ofstream out2("data.txt", std::ios::ate);

out2 << "This line will be ate to the end of the file";

out2.close();

The result above is not what I wanted no text appended but the content is truncated!
To solve it combine ate with in:
std::ofstream out2("data.txt", std::ios::ate | std::ios::in);

out2 << "This line will be ate to the end of the file";

out2.close();

Now the text is appended to the end but what is the difference:
As I said app doesn't allow moving the write pointer but ate does.
std::ofstream out2("data.txt", std::ios::ate | std::ios::in);

out2.seekp(5, std::ios::end); // add the content after the end with 5 positions.

out2 << "This line will be ate to the end of the file";

out2.close();

Above we can move the write pointer to the position we want while with app we cannot.

Look at this example: https://www.youtube.com/watch?v=6cDcTp1gn4Q
Comment: Yes. But as you can see combining read flags `std::ios::in` with write flags for a write stream is such a stupid logic in fstream.

ofstream open modes: ate vs app


After page refresh using setTimeout, display tab that was being displayed before page refresh

Question: I am using jQuery tabs, and on one of my tabs there is function that refreshs the page and then displays that same tab after the refresh.
Here is what I have so far, but it does not seem to set the tab to active:
setTimeout(function(){
    location.reload();
    $("#tabs").tabs({ active: 1});
}, 1000);

My tabs:

Carrier Details

Contacts

    <%--This is Carrier Details Tab--%>

        <%@include file="carrierProfileDetails.jspf" %>

    <%--This is Contacts Tab--%>

        <%@include file="carrierProfileContacts.jspf" %>

I want the contacts tab to be displayed after the refresh.
Comment: My suggestion would make it so the page focuses on the tab every time it's loaded, whether it was refreshed due to that function or not. If you need the exact functionality you defined, you should look into `localStorage`. I don't know a lot about it, but it should be able to solve that issue (possibly with spotty browser support).
Comment: If I understand correctly, you should just put `$("#tabs").tabs({ active: 1});` at the top of your js. `.reload()` refreshes the webpage and wipes out your js state, so no lines below it will ever run.
Comment: `location.reload()` is the same as hitting F5.  As such, any logic that you try to perform after doing so is going to be erased when the page reloads.
Comment: @wassona so is it possible to get what I want then?
Answer: You can use sessionStorage for your case, you can set/get/update data and it will be accessible after refresh. Here  is a simple example, but it will not work in snippet, you can create HTML page locally and check it out (it works fine locally):

Active tab:

  <button onclick="changeTab('1')">Tab 1</button>
  <button onclick="changeTab('2')">Tab 2</button>
  <button onclick="changeTab('3')">Tab 3</button>

The main idea is to set current state of tabs to sessionStorage on every change and get the last state from it on page load. If item exists and has a value - then set the state based in this data.
Answer: location.reload();

This will reload the webpage and not a single line after this will be executed.
If you want to always activate the same tab on page load, write $("#tabs").tabs({ active: 1}); on body onload method.


How to estimate the cost of flooding events in a given year?

Question: I have a cost associated to three flooding events with different return period: 5 years, 25 years and 100 years. The probability that an event with return period 5 years happens is pr(5) = 1/0.5 = 0.2. Same for the others: pr(25)=1/25=0.04 and pr(100)=1/100=0.01.
To each event I have associated a damage cost: C(5) = 1000, C(25)=1500 and C(100)=2000.
I would like to estimate the annual cost  given the three events. Should I consider the three events independent and do something like the following?
C = pr(5) * C(5) + pr(25) * C(25) + pr(100) * C(100)
Comment: Even if the events are *not* independent, expected values are additive
Answer: Linearity of expectations ALWAYS holds. What you need to do additionally in calculating the expected costs is to take the present value of these expected costs. For example: if you end up spending $100$ units of currency $5$ years from now, then at an average rate of interest of $10\%$, that money is worth $\frac{100}{1.1^5}$ to you today.
So your cost formula needs to be updated as:
$$E(\text{Present Value of Cost}) = \frac{P(5) C(5)}{(1+r)^5} +\frac{P(10) C(10)}{(1+r)^{10}} + \frac{P(15) C(15)} {(1+r)^{15}}$$
Where $r$ is your assumed constant rate of interest over the years.
Comment: a question: in this case low is `r` higher is the cost. Am I right? should not it be `(1-r)^5`
Comment: No. That is not correct.
Comment: so I do not understand why for higher rate of interest the price decrease
Comment: If you had to draw a loan from the bank to pay, say $X$, for a future event,let's say $t$ years from now at an interest rate $r$, that results in the cost $X$, how much loan should you apply for? Let's say you apply for an amount $Y$ and we want $X$ after $t$ years. So what do you get as the value for $Y$?


Fetch Twitter Friend List after Authentification

Question: I've used the following method from MGTwitterEngine.m in order to fetch the friend list:
(NSString *)getFriendsIds:(NSString *)twitterID
{
    NSString *path = [NSString stringWithFormat:@"friends/ids.%@", API_FORMAT];

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:twitterID forKey:@"user_id"];

    return [self _sendRequestWithMethod:nil path:path queryParameters:params body:nil
                            requestType:MGTwitterUserInformationRequest
                           responseType:MGTwitterUsers];
}

And in my class I've made the following call:
NSLog(@"friends %@",[_engine getFriendsIds:@"user_id"]);

I couldn't retreive the friend list.
Comment: Translated, OP will just have to accept the changes, as I don't pocess full editing rights. @user1195279 J'ai traduit ta question en anglais, si tu veux bien accepter les changement, il sera beaucoup plus facile pour la communauté de t'aider
Comment: Sorry, you're going to have to re-ask in English. Posez la question à nouveau en anglais.
Comment: Perhaps I could translate it for him, doing it as we speak.
Answer: - (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier {

    //NSLog(@"User Info Received: %@", userInfo);

    followTweet = [[TweetFollow alloc]initWithStyle:UITableViewStylePlain];//Table

    TweetModel *tweetFollw = [[TweetModel alloc]initWithTweetFollwerArray:userInfo];//Model
    [followTweet.tweetFollow addObject:tweetFollw];

    NSLog(@"ARRYYYYYY: %@",followTweet.tweetFollow );
    followTweet.view.frame = CGRectMake(161, 0, 160, 420);
    [self.view addSubview:followTweet.view];
    [tweetFollw release];

}

#define TWITTER_DOMAIN @"api.twitter.com/1"  // Plz change this in to MGTwitterEngin.m

-(void)viewWillApper {

//_engine is instance of MGTwitterEngin class
[_engine getFollowersIncludingCurrentStatus:NO];

}

This is simple code by which you can get the twitter Friend list ;
Thank you !!


Find $\sum_{i=1}^n (n \mod i)$

Question: How can we find $\sum_{i=1}^n(n\mod i)$?
Comment: You are adding things in different groups here. I think what you want is to take a choice of integer representatives in each congruence class $\mod i$ (for example, take the nonnegative integer less than $i$), since it doesn't make sense to add elements $\mod i$ and $\mod j$ when $i\neq j$
Comment: @BrettFrankel It's possible that the OP intends to in the computer sciencey way, that is, the smallest nonnegative remainder after division (rather than an equivalence class).
Comment: @rschwieb That's the only reasonable conclusion, but I think it's instructive to have it on record that this is not well-defined as stated, and to have the correct formulation of the question appear in at least answers, if not an edit of the original question.
Comment: @BrettFrankel I agree completely with you, and I didn't mean anything by alerting you. I was just trying to feel like I wasn't talking to myself when I made my remark :)
Comment: Elmi - if you find answers to be helpful, you may upvote them (click on the "upwards" arrow to the right of an answer. If you find one answer to be particularly helpful, you can also *accept* one answer per question by clicking in the "greyed out" check-mark to the left of the answer.
Answer: This is at the OEIS.  Various identities and references are given there.
By calculating a few terms, e.g. the value of your expression when $1 \le n \le 7$ say, you get the sequence 0,0,1,1,4,3,8,8.  You can then go to the OEIS and search with those terms, you get the entry linked above.

Find $\sum_{i=1}^n (n \mod i)$


Universal quadratic forms

Question: A quadratic form is a polynomial $p(x_1,\dots,x_n)$ of the form $$ p(x_1,\dots,x_n)=\sum_{i \leq j}a_{ij}x_ix_j. $$
For example, $p_1(x,y,z,w)=x^2+y^2+z^2+w^2$ and $3x^2-5y^2$ are quadratic forms. I'm not sure this is relevant here, but for any such $p$ there is a symmetric matrix $A$ such that $p(\vec x)= (\vec x)^T A(\vec x)$, where we think of $\vec x$ as a column vector.
$p$ is positive definite iff $p(\vec x)>0$ whenever $\vec x\ne 0$.
The form is integer valued iff $p(\vec a)\in{\mathbb Z}$ whenever $\vec a\in {\mathbb Z}^n$.
For short, let's say "form" instead of "positive definite integer valued quadratic form".
A form is universal iff it represents every positive integer, i.e., for all $m>0$ there is $\vec a\in{\mathbb Z}^n$ with $p(\vec a)=m$. For example, $p_1$ above is universal.
I read that no form in 3 or fewer variables is universal, and I was hoping for a proof (or at least a reference if the proof is too long/convoluted for it to be written here). I was also wondering whether the requirement of positive-definiteness is relevant here, and whether this is only for forms $p$ where the matrix $A$ above has integer entries.
Comment: The positive-definiteness is certainly relevant: for example, $x^2 + y^2 - z^2$ is an indefinite universal form in three variables.
Comment: Positive definiteness is definitely relevant: the form $xy$ represents everything.
Comment: May I suggest that you change the title of the question into something a bit more descriptive? "Quadratic forms" is rather vague.
Comment: @Qiaochu, user6312: Thanks, I should have noticed this. @Hans: You are right, I edited the title.
Answer: John Conway gives a proof (that no form in 3 or fewer variables is universal) in 2 paragraphs on page 142 of his book, The Sensual Quadratic Form. But the two paragraphs depend on $p$-adic squareclasses and other bits of theory developed elsewhere in the book.
Comment: Thanks! I had heard of the book years ago, it is time I read it.

Universal quadratic forms


What is a standard order for learning/practicing scales when learning how to play the electric bass (esp. for jazz/blues music)?

Question: I'd like to bring my theoretical knowledge and practical skill for playing jazz/blues scales on the electric bass up to snuff. What is the standard order for learning/practicing jazz/blues scales on the bass?
Answer: The order might vary, but usually it is arranged so you get the ones that you will use the most first, but it doesn't really matter. You can google pages and pages that have fingerings for these.
One benefit we have with a stringed instrument is that there are patterns and shapes that each scale makes. The shape you learn for one major scale is the same for all major scales. We have it a lot easier than other instruments in that regard.

Major Scale - C D E F G A B C - This scale also gives you dominant, and two versions of minor, just keep the same notes, but begin in a different place.
A. Dominant - G A B C D E F G (same notes, just start on G)
B. Minor ( Dorian )- D E F G A B C D - start on D
C. Minor (Natural) - A B C D E F G A - start on A

Melodic Minor - C D Eb F G A B C - The next most common form of minor scale.
Pentatonic - C D E G A C or the minor version A C D E G A

Check out this free PDF for theory and various scales:
http://www.jazzbooks.com/mm5/merchant.mvc?Screen=FQBK&Store_Code=JAJAZZ


How to reuse the code if only the base class is different in C++?

Question: I have two classes, all the codes are the same, except for the base class. How could I refactor them and reuse the code? some_code_1 and some_code_2 are the same in two classes. I'm new to C++, thank you for giving me any suggestions.
<template class C>
class A : Base_1<C>
{
    private:
        using Base = Base_1<C>;
    public:
        using Base::Base;

        method(pa1, pa2, pa3)
        {
            // some_same_code_1
            Base::some_method(pa1, pa2, pa3);
            // some_same_code_2
        }
}

<template class C>
class A : Base_2<C>
{
    private:
        using Base = Base_2<C>;
    public:
        using Base::Base;

        method(pa1, pa2, pa3, pa4, pa5)
        {
            // some_same_code_1
            Base::some_method(pa1, pa2, pa3, pa4, pa5);
            // some_same_code_2
        }
}

Comment: Not nearly enough information. What are the base classes? What are these classes supposed to do? What are the child classes supposed to do? A refactor that you're talking about requires some actual analysis, not band-aid code.
Comment: Please don't make large changes to the question after getting an answer. The point of SO is to preserve knowledge for future readers, and the large changes will make existing answers meaningless for them.
Comment: Code of these two classes do not appear to be the same.
Comment: This feels a lot like you shouldn't have inheritance at all here but rather use composition. Without more info that's hard to tell though
Comment: sorry, i haven't written c++ before, i don't know how to ask clearly. The base classes are some basic operation, for example,  calculate matrix multiplication, and the child classes are some variant operation. The different between Base_1 and Base_2 is Base_1 is doing exact calculation and Base_2 realizes some approximate calculation. And I want to implement the approximate version of derived class B.
Answer: Use a template:
template <typename Base>
class Foo : Base
{
  public:
    // some_code_1
    Base::some_method(pa1, pa2, pa3);
    // some_code_2
};

using A = Foo<Base_1>;
using B = Foo<Base_2>;

Here, using A = Foo<Base_1>; makes A a shorthand for Foo<Base_1>. Alternatively you can write Foo<Base_1> every time you need your class. Same for B and Foo<Base_2>.
Comment: thank you, when calling some_method, the number of arguments are different in Base_1 and Base_2, how could i resolve it?
Comment: @KikiDu Oh, I didn't notice that the arguments were different. Then you can make `A` and `B` classes inheriting from `Foo` and `Foo` respectively (don't forget to inherit constructors). Then put the common code into `Foo`, and the parts that differ into `A` and `B`.
Comment: how to put the common code into Foo? make them (same_code_1, same_code_2) as methods?
Comment: @KikiDu Maybe, it depends on what you're doing.


A step in the proof of Erdos-Tetali Lemma.

Question: I got confused reading this expression. Would be great if someone could help elaborate.

$$\sum_{(i_1,...,i_s) \in [m]^s} \prod_{j=1}^s \mathbb{P}(E_{i_{j}}) = \left(\sum_{i \in [m]} \mathbb{P}(E_i)\right)^s$$
Answer: The left side is the sum of all products of $s$ not necessarily distinct $\mathbb P(E_i)$'s, where order matters. If you expand the right side, each such product is written out exactly once. Simply imagine "choosing" the term $P(E_{{i_j}})$ for the $j$th term in the product.
As an example, note that $xx+xy+yx+yy=(x+y)^2$. The equation in question is not too different.


$p$-adic metric

Question: This is a question from Robert Strichartz: the way of analysis, page 385.
He defines a $p$-adic metric on $\mathbb{Z}$ as follows. $p$ is a fixed prime. For any integer $z$, we have $z = \pm \sum_{j=0}^N a_j p^j$. $$|z|_p = p^{-k}$$
where $k$ is the smallest integer such that $a_k \neq 0$.
(a) Show that $d(x, y) = |x - y|_p$ is a metric.
(b) Show that $d(x, z) \leq \max\left(d(x, y), d(y, z) \right)$
My understanding:
First, I am assuming $x - y$ should be an integer. Second, from what he wrote, it does not follow that $|0|_p = 0$ so I am going to assume that.
Any hints about how to start proving the triangle inequality? The answer to part (b) implies the triangle inequality only if I can solve part (b) without using the fact that $d$ is a metric.
Comment: It's standard to prove part (b) by itself and then see that this implies the triangle inequality.
Comment: "Second, from what he wrote, it does not follow that |0|p=0 so I am going to assume that." A good observation.  $0 = \sum_{j=0}^N a_j p^j$ means $a_j=0$ for all $j$ and so $a_j= 0$ so there *is* no smallest $k$.  So yes.  $|0|_p = 0$ must be special case.
Comment: Reiterating Alex Mathers comment.  b) implies the Triangle Inequality.  SO prove b) *first* without assuming $d$ is a metric.
Comment: "I am assuming x−y should be an integer" since the space is $\mathbb Z$, $x,y \in \mathbb Z$.
Comment: Note:  Ultra metrics show that all triangles are isosceles!
Comment: What's a counterexample for c?
Answer: First observation is that $|z|_p = p^{-k}$ $\iff$ $k$ is the highest power of $p$ such that $p^k | z$.
Second observation is that if $p^k | z$ then $p^n | z$ for all $0 \leq n \leq k$.
Now consider $x, y, z \in \mathbb{Z}$. Let $|x - y|_p = p^{-k}$ and $|y - z|_p = p^{-j}$. Then, let $m = \min(k, j)$ so that $p^m | (x - y)$ and $p^m | (y - z)$ $\implies $ $p^m | (x - z)$ because $x - z = (x - y) + (y - z)$.
Hence, the highest power of $p$, call it $l$, such that $p^l | (x - z)$ satisfies $l \geq \min(k, j)$. Hence,
\begin{align*}
    |x - z|_p = p^{-l} &\leq p^{-\min(k, j)} \\
    &= \max(p^{-k}, p^{-j})\\
    &= \max(|x - y|_p, |y - z|_p)\\
    &\leq |x - y|_p + |y - z|_p
\end{align*}
Answer: Note.  If $z = \pm \sum_{j=0}^N a_j p^j$, then $k$ is the first $a_k\ne 0$ if and only if $k$ is the highest power of $p$ so that $p|z$.
Keeping that in mind.  If $k$ is the highest power that divides $x-y$ and $j$ is the highest power that divides $y-z$.  If $k \ne j$ and $\min(k,j) < m \le \max(k,j)$ then $p^{m}$ will divide one of $x-y$ or $y-z$ but not the other so $p^m|(x-y)+(y-z) = x-z$.
In that case the highest power that divides $x-z$ is $\min(k,j)$ and $|x-z| = p^{-\min(k,j)}= \max(p^{-k},p^{-j}) = \max(|x-y|_p, |y-z|_p)$.
If on the other hand if $k = j$ then the maximum power that divides $(x-y)+(y-z)=x-z$ is $\ge k = j$ and $|x-z| \le |x-y|_p = |y-z|_p$ so
So $d(x,z) \le \max(d(x,y), d(y,z))$ and thus
$d(x,z) \le \max(d(x,y),d(y,z)) + \min(d(x,y), d(y,z)) = d(x,y) + d(y,z)$.
So that proves the triangle inequality.
And it proves b).
(It's okay to use b) to prove a) but.....)
Comment: Yes.  $a|b$ means $a|b$.  But $a|b+c$ doesn't mean $(a|b)+c$ which doesn't make any sense ($a|b$ is not a number; its a sentence-- you can't add to it)  $a|b+c$ means $a|(b+c)$.   And $a|b+c=d$ means: $a|(b+c)$ and $b+c = d$ so $a|d$.
Answer: For the triangle inequality let $a,b,c \in \mathbb{Z}$. We need to show
$$
\frac{1}{e^{\nu_p(a-c)}} \le \frac{1}{e^{\nu_p(a-b)}} + \frac{1}{e^{\nu_p(b-c)}}$$
and note that we can write
$$a-c=(a-b)+(b-c).$$
Here $e^x$ denotes the $\text{exp}(x)$ function.
Comment: You should explain how this is connected.

$p$-adic metric


Remove string after last slash in Elixir

Question: I have a problem with removing everything after the last slash of URL in Elixir. For instance, I have URL:
http://localhost:4000/admins/new
I wanna change it to:
http://localhost:4000/admins
How can I do it?
Answer: The safest way would probably be to use URI.parse/1
uri = URI.parse("http://localhost:4000/admins/new")

%URI{uri |
  path:
    uri.path
    |> String.split("/")
    |> Enum.slice(0..-2)
    |> Enum.join("/")
}
|> URI.to_string()

#⇒ "http://localhost:4000/admins"

Answer:
You can do it with a regular expression:
Regex.replace(~r{/[^/]+$}, "http://localhost:4000/admins/new", "")

Output:
"http://localhost:4000/admins"


erro em consulta no mysqli

Question: Pessoa, como arrumo esse meu codigo, ele esta dando erro:
<?php
//IMPRIME TODOS OS VALORES DO BANCO CADASTRO

$servidor = "localhost";
$usuario  = "root";
$senha    = "admin";
$banco    = "cadastro";

//conecta-se ao banco de dados Mysql
$con = new mysqli($servidor, $usuario, $senha, $banco) or die("");

// Verifica se ocorreu algum erro
if (mysqli_connect_errno()) {
die('Não foi possível conectar-se ao banco de dados: ' . mysqli_connect_error());
exit();
}
if(isset($_POST['buscar'])){
$q = mysql_real_escape_string($_POST['consulta']);
$query_Busca = "SELECT * FROM 'equipamento' WHERE 'tombamento' LIKE '%$q%'";
$Busca = $con->query($query_Busca);
$row_Busca = mysqli_fetch_array($Busca);
$totalRows_Busca = $Busca->num_rows;

} else {
$query_Busca = "SELECT * FROM 'equipamento'";
$Busca = $con->query($query_Busca);
$row_Busca = mysqli_fetch_array($Busca);
if (!$check1_res) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
$totalRows_Busca = $Busca->num_rows;
}
?>

Os erros que recebo são: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given
E ESTE:
Undefined variable: check1_res in C:\wamp\www\cadastro\consulta.php on line 58
Comment: Olá. Pode editar a sua pergunta e acrescentar mais detalhes? Que erro é que dá? Aonde é que dá o erro? Tente explicar melhor o seu problema e não cole apenas o código.
Answer: Quando fizer uma consulta e tabela/campo tiver caracteres especiais use:
`

Aspas simples ' devem ser somente usadas em valores do tipo texto/character etc
troque as ocorrências:
$query_Busca = "SELECT * FROM 'equipamento'";

por:
$query_Busca = "SELECT * FROM `equipamento`";

ou ainda:
$query_Busca = "SELECT * FROM equipamento";

Ao executar uma consulta lembre de fazer ela exibir o erro do banco de dados assim:
$Busca = $con->query($query_Busca) or die($con->error);

Comment: Perdeu tu é o cara! Desculpe a falta de detalhes do problema, mas o pouco que você me passou deu pra resolver legal. Agora soh me responde uma coisa: como eu faço para o algoritmo de busca do php ficar em outra pagina eser chamado? Queria fazer isso para ficar mais seguro o Banco de dados. Sabe como faz? se souber edita na sua resposta e adiciona por favor. estou precisando disso também e obrigado por tudo!
Comment: @DarkJontex, pode criar um arquivo separado com funções e usar um [include](http://php.net/manual/pt_BR/function.include.php)/[require](http://php.net/manual/pt_BR/function.require.php) para chama-los em outra página.

erro em consulta no mysqli


Goal Line Technology in football

Question: Why isn't Goal Line Technology implemented? Are there any reasons why this technology is not being used in football?
Rugby has had a system to make the game fair for ages, but football hasn't yet implemented a similar system?
Comment: I once seen an interview with Platini where he explained why he didn't think it was a good idea to implement it in football because of the cost and logistics. I'll try find that interview
Comment: You should consider rewording the first half of your question because it sounds like you asking for people's opinion, I think the question is good though.
Answer: Essentially it hasn't been introduced by UEFA because the top brass don't support it because of the financial cost. Platini says:

"I prefer to put more money into youth football and infrastructure than spend it on technology when there's a goal in a blue moon that hasn't been seen by a referee."
  "It would cost around 54 million euros (£46m) over five years for this technology, so it's quite expensive for the sort of mistake which happens once every 40 years," he said.
  "In the Champions League, I'm very happy with the results (of a five-man team). Practically no mistakes have been made and the referees see practically everything that happens on the pitch."

That interview was from around 2013, I believe he is open to the idea of it being used at Euro 2016. FIFA seem to have the seem view of the issue. Some pundits and ex-footballers believe it would also slow down the flow of the game but the main reason for it not being implemented seems to be financial and logistical. I would guess that we will say goal line technology being used in only major tournaments such as the World Cup and major leagues like the English Premier League where the technology is currently being used.
Here is an interesting article on Fifa's attitude towards it.
Answer: Platini has said:"I'm totally against the goal-line technology and let us appreciate the humanized football game continually. I insist on my point due to this is not only a problem of technology. What frightens me is that goal-line technology is able to be the beginning of football technicalization "
As a confrontational sport, football contains protean situations in confrontation.So the rules is impossible to be rigorous completely.The game need scales which are not need to remain consistency in the different games, but just need both sides in a same game not to violate the rules. Therefore, so far, the refereeing work is done by humans completely. It is only referees can choose and maintain a certain scale according to the different situations in the football games.
Answer: http://www.bbc.co.uk/newsbeat/23629154 to help Ciaran out. It has been used effectively in the Premier League all season and a cost of £250k to install seems well within the reach of top tier European clubs
Comment: I think Platini is referring to the cost of maintaining and installing this for every ground in the top flight leagues in football, I would imagine it would cost a lot but it should really be used for major competitions
Comment: Completely agree RoB - every top flight club with the revenue they generate should be able to afford it. I think it's a stretch too far for any club below Europe's top leagues though.
Comment: I would like to see it tried at tournaments like the World Cup or Champions League which have a higher profile and more revenue behind them before it would be introduced for a full season in a league. I can't even think of more than a couple of occasions where a ref has used it in England or maybe I'm wrong?
Comment: I think that is the point - it shouldn't come into play but should be there in the rare event that there is a dispute over whether the ball has crossed the line or not.
Answer: As I understand it; the costs of technology isn't the big issue. Rather, the problem lies in the very definition of what football is. I haven't got any quotes to lean on, but the fundamental idea of football is that "it's a 90 minute constant flow of events". Thus, having any sort of break in the game (exept for injuries and substitutions, of course) would be in violation to the fundamentals, and therefor unacceptable. Some goalline technology may pass this test, but should it come more technology to the sport, it would change it completely - and not in a positive manner!
Answer: The English Premier League have brought goal line technology in for the first time this season. All referees have a special watch that gives them a signal if the ball has crossed the line.
Comment: Why don't you throw in a few references?

Goal Line Technology in football


How can use session

Question: I created a html page and create form on it . I want send data of my form to a form that I create . I use session but when I use session and refresh page , other session was delete or when i use session_unset(); the sessions in 4 or 5 time refresh age was show .
What is the right way for save session for anonymous users?
I use $_SESSION['test'] but I think that it's not good way .
Answer: If you get form content that is outdated since 4 or 5 refreshes this could be a caching issue. You can try to add a cache context to the form:
$form['#cache']['contexts'][] = 'session';

Or if this doesn't help disable the render cache for the form:
$form['#cache']['max-age'] = 0;

Comment: This is one session, it doesn't matter how much data you store.
Comment: Thanks . But I have many sessions . So how can I use that ? $_SESSION['a'][$loop] , $_SESSION['b'][$loop]  , $_SESSION['c'][$loop] . And $loop = 0 and with for I crease that ( $loop++).

How can use session


find radius and interval of convergence for $\sum_{n=1}^{\infty}\frac{3^n+(-2)^n}{n}(x+1)^n$

Question: The series:
$$\sum_{n=1}^{\infty}\frac{3^n+(-2)^n}{n}(x+1)^n$$
I just do not know where to start here, I trues to substitute $t=x+1$ but it did not help much
Answer: HINT:
$$\frac{3^n+(-2)^n}{n}(x+1)^n=\dfrac{\{3(x+1)\}^n}n+\dfrac{\{-2(x+1)\}^n}n$$
As $\ln(1+x)=-\sum_{r=0}^\infty\dfrac{(-x)^r}r$ is convergent for $-1<x\le1$
we can use Taylor series for $\log(1+x)$ and its convergence OR What is the correct radius of convergence for $\ln(1+x)$?


Is grabbing the shoulder pads defensive holding?

Question: During the Monday night game between Green Bay and Detroit on 14 October 2019, Detroit's Trey Flowers was twice penalized for "Illegal Hands to the Face" when he wasn't really touching the face but was instead grabbing the shoulder pads under the chin. The result of the penalties was a first down, both happening at crucial moments for the Packers.
The ESPN announcer made a big deal about how the calls were wrong and that it affected the outcome of the game. However, a friend of mine pointed out that although "Illegal Hands to the Face" is not the correct call, it should have been penalized as "Defensive Holding," which results in the same 5 yards and First Down.
Is this true? Is grabbing and holding the shoulder pads under someone's chin considered Defensive Holding?
Comment: Booger mentioned that hands to the neck is covered under the umbrella of this penalty.  I assume in the eyes of the officials it appeared that Flowers' hand was close to the neck in either case.
Answer: Using equipment to pressure the face or neck is hands to the face.  It is quite common to see a defensive lineman get a hands to the face penalty when hitting the players neck.   The other option the ref has is to give him a misconduct which is 15.
Also both lines can hold the shoulder pads in the front chest area.  I don't think you can find this in a rulebook but it is in referee guides.   However neither side can pull the other using the pads or anything else and this is why holding - on either side - is called 98% of the time.
In summary this situation didn't seem like defensive holding.   It seemed like a personal foul - hands to face.   In that umbrella it is touching the face mask, chin, or neck or using any equipment to aid in that.   Again I am not sure you will see all of this referenced in a rulebook unless it has been annotated recently.   This is something you will see in referee guides, especially in the NFL where they have a rulebook that covers the main things and then half of the other stuff - like this - is secretly given to their referee crews and is generally non-public (think the catch rule and current PI calls).


Find rotation period of a four-body gravitational system on a square.

Question: Four point bodies, each of mass $m$, attract each other due to the interaction
gravitational. Such bodies are at the vertices of a $L$-side square rotating around their
center with constant angular velocity. $G$ being the universal gravitational constant, the period of this
rotation is given by:
What I tried:
$ F_R = F '+ F_ {DB} $
vector
so, $ F_{DB}= \frac{GM^2}{(L\sqrt{2})^2}$
answer:
$2\pi \sqrt{\frac{L^3}{GM}(\frac{4-\sqrt{2}}{7})}$
Comment: Please define your variables.  It looks like $F_{DB}$ is the force from the diagonally opposite mass, but you have ignored the forces from the other two.  You then leave out all the steps to the period.  What is the question?
Answer: The net attraction force on each mass is the sum of the gravitational attractions from the other three masses and points to the center of the square,
$$F=\frac{Gm^2}{(\sqrt2 L)^2}+2\cdot\cos 45\cdot\frac{Gm^2}{L^2}=\left(\frac12+\sqrt2\right)\frac{Gm^2}{L^2}\tag{1}$$
Additionally, from the Newton's law for the angular rotation,
$$F=mr\omega^2=\frac{\sqrt2}{2}mL\omega^2\tag{2}$$
where $r=\frac{\sqrt2}{2}L$ is the radius of the rotation. Combine (1) and (2) to obtain the period of the rotation,
$$T=\frac{2\pi}{\omega}=2\pi\sqrt{\frac{L^3}{Gm}\left(\frac{4-\sqrt{2}}{7}\right)}$$


Understanding and interpreting quantile regression

Question: I am trying to better understand what quantile regressions are and how we can interpret them. I know that quantile regressions are used to model a specific conditional quantile $\tau$ of a response variable $y$ given some covariates $X$ as opposed to modeling the conditional mean of $y$ in an OLS setting. Now I've been studying some of the standard definitions that are easily found online and in standard textbooks. However, I was specifically wondering why the approach I will outline below is not correct.
I understand estimating a quantile regression as estimating a changing relationship between $y$ and $X$ that varies over the distribution of $y$. In more formal terms, I have been thinking about a hypothetical quantile regression data generating process as
$y_{\tau} = X_{\tau}\beta_{\tau} + \epsilon_{\tau}$
where $y_{\tau}$ is an estimate of $y$ in quantile $\tau$ and $\epsilon$ is an error term. The main point is that the coefficient $\beta_{\tau}$ varies over the quantiles of $y$. In more simple terms, each quantile $\tau$ has its own coefficient $\beta_{\tau}.$
I implemented the DGP above and simulated 100 data points (representing 100 percentiles) increasing $\beta_\tau$ gradually. Then, I estimated a quantile regression for all percentiles of $y$. This exercise basically returns the OLS coefficient for all percentiles. I was expecting a lot of noise due to only 1 observation per percentile. However, I did also expect some evidence pointing into the direction of the true values of $\beta_\tau$. Now I am wondering where I went wrong and how the setup above differs from traditional quantile regression.
Edit:
To add some clarification after David's comment: Seemingly, the interpretation of quantile regressions is "a unit change in $X$ changes quantile $\tau$ of $y$ by $\beta_{\tau}$" where we estimate different $\beta_{\tau}$ for each quantile. If that is not the correct interpretation, this would already solve my question. If that is the correct interpretation, I do not understand why the DGP I outlined above, interpreted as "each quantile $\tau$ has its own coefficient $\beta_{\tau}$" does not give similar results.
My Code (in R):
<code>X    = rnorm(100)
beta = seq(0,5,length.out = 100)
y = c()
for(tt in 1:100){

 y[tt] = X[tt] * beta[tt] + rnorm(1)

}

plot(y,X)

olsres = summary(lm(y~X-1))

qrres = quantreg::rq(y~X-1,tau = seq(0,1,length.out=100))
</code>
Comment: Please post your code. The idea of quantile regression is that, for each vector of predictors, there is some conditional distribution (same as OLS), and quantile regression estimates the specified quantile of that distribution. What you describe does not seem at all related to that. Your code will reveal what it is that you did.
Answer: I suspect you mistake Quantile Regression for some sort of piece-wise linear regression, where a normal OLS line is fitted to subsets of the observation space (note that if you think about this, it can be quite complicated to determine how to subset this data in a multivariate case if you only have a single parameter $\tau$).
Quantile regression is something different, where the conditional median is estimated (for $\tau = 0.5$) or at any other percentile of interest. Which percentile depends on the value of $\tau$ you specify: you specifically are calculating the conditional median at every percentile.  It is usually applied in cases where certain assumptions do not hold (for example if there is a multimodal structure to your data and a conditional mean is not very informative). It is not re-estimating the OLS line based on bins of your independent variable (as you seem to suspect). Note also that if it was doing this, you would have a single observation in every bin and it would not even be possible to estimate a slope $\hat{\beta}$. In other words: Quantile Regression does not subset the number of observations used or anything like that.
Comment: What do you mean by *you specifically are calculating the conditional median at every percentile*?
Comment: I'm just referring to what the question asker is doing in his/her code.


If G is a bipartite graph with 6 vertices and 9 edges then the chromatic number of G bar?

Question: Here for bipartite graph chromatic number will be the 2 but if we have 6 vertices and 9 edges then it will become complete bipartite graph so its complement graph will be the null graph. So, its chromatic number will be 1, Right? I'm little bit confused here because i got this question and answer mentioned 3. So, what is the right answer?
Comment: A complete graph on 6 vertices has $\binom62=15$ vertices, so your graph will have $15-9=6$ vertices.
Answer: It is a complete bipartite graph, but its complement is not empty! Note that in the complement you will have edges between the 3 vertices on each side, so $\bar{G}$ is the union of two $K_3$s. Each $K_n$ has chromatic number $n$, so the answer is 3.
Comment: Yes undersood! means its both side contains K3 so, chromatic number for K3 is 3 So, answer will be 3.


Bayesian updating with conjugate prior (specific example)

Question: This question deals with Bayesian updating with conjugate prior.Suppose we have a prior distribution of N~(5, 3) and then we observe 5 data points (8, 9, 10, 8, 7) (assumed to be taken randomly from a N~(9, 3) distribution). What would be the posterior after these observations in the form of N~(x, y)? I read the Wikipedia article on conjugate priors, but I want to have a more precise understanding of how to solve this specific problem. If there is no way to solve it without assuming some things, can you please explain what needs to be known and solve under very simple assumptions? Thank you in advance.
Comment: Where is the likelihood? and what are the parameters?
Comment: @Kelly if you do not know the distribution of your data (i.e. you are not willing to assume it), then you cannot use classical Bayesian inference since for this you would need to assume a likelihood function (i.e. distribution) for your data. If you do not assume distribution for your data then what parameters you want to estimate? N(a,b) is prior for what?
Comment: If the information is all that is given to work with, is there any way to approximate the posterior?
Comment: @Kelly if you know that the data comes from N(9, 3) distribution, then there is no need to estimate anything since you know the parameters in advance (9, 3). What niandra82 was saying is that you need to specify priors and likelihood, what you did is you only specified prior for mean.
Comment: When you say N~(a,b) presumably that indicates a normal distribution with mean a ... but is b the variance or the standard deviation in this notation?
Comment: @ Glen_b: b is the variance. @Tim: Right, I should have been clearer. What I meant about where the data comes from is some hypothetical distribution that exists but that the agents have no idea what. Example would be someone trying to estimate the distribution of the height of everyone who lives in Town X. The person has some prior, and there is actual data about the distribution of height, but the person only observes the five data points. I'm not quite sure what the likelihood could be for this specific situation.
Comment: Would the question make more sense if I assumed that the likelihood of observing those five data points given the prior is 0.2?
Comment: I'm willing to assume that the distribution of my data is normal. N~(5, 3) would be the prior distribution with mean 5 and variance 3, and after observing 5 data points, I would like to update my mean and variance to x and y. Am I missing something?
Comment: Yes. In simple cases the idea is to perform inference on the *parameters* of the distribution the data are drawn from. Your priors are priors on the distribution of one or more parameters. I was assuming you were giving a prior on $\mu$.
Comment: I'm sorry that I misunderstood, I hadn't realized that a host of other assumptions were necessary for this seemingly simple problem. So in the example I gave about estimating the height of people in Town X, is there another statistical method typically used to formalize how a person's estimate of the mean would change after the observations? It seems like there must be some formal way of characterizing the belief update for these simple situations.
Comment: You can make much weaker assumptions in various ways but the explanation of how to do it becomes more complicated.
Answer: I am afraid that you are misunderstanding what Bayesian inference is about in general. Bayes theorem is
$$ \underbrace{p(\theta \mid X)}_\text{posterior} = \frac{\overbrace{p(X \mid \theta)}^\text{likelihood} \, \overbrace{p(\theta)}^\text{prior}}{\underbrace{p(X)}_\text{normalizing constant}} $$
To make it more concrete, you can estimate $\mu$ and $\sigma^2$ parameters from normal distribution (i.e. normal likelihood) using data $X$, assuming normal prior for $\mu$ with hyperparameters $\mu_0$ and $\sigma^2_0$, and uniform prior for $\sigma^2$ with hyperparameters $a$ and $b$, to obtain posterior distributions for $\mu$ and $\sigma^2$:
$$
X \sim \mathrm{Normal}(\mu, \sigma^2) \\
\mu \sim \mathrm{Normal}(\mu_0, \sigma^2_0) \\
\sigma^2 \sim \mathrm{Uniform}(a,b)
$$
So priors are assigned to parameters of interest, not to data. You also have to specify assumed distribution of your data (likelihood). Finally, posterior is distribution over estimated parameters, so you need to specify what you are actually estimating.


Sistema de cadastro em C salvando em arquivo

Question: Eis aqui meu código. Acredito que os procedimentos de receber strings, receber as informações e inserir no vetor global estejam funcionando, mas o programa para de funcionar ao selecionar a opção listar, assim que é adicionado uma estrutura no vetor global.
O programa funcionava normalmente antes de implementar a função removerQuebraLinha (que funciona normalmente em outros dois programas que testei), mas mesmo retirando ela e recebendo dados diretamente pelo fgets não voltou a funcionar mais.
Sem a função removerQuebraLinha, salvando num arquivo binário as saídas começam aparecer repetidas, ou somadas com outras partes do vetor.
Quando não há usuários / consoles / jogos cadastrados ele lista 0 normalmente. Alguém pode me dar uma luz de onde está o erro? Já tenho revirado esse código há uma semana.
edit: Faltou espaço para as funções gravarVetor() e lerVetor(), então as removi.
edit2: Removi 1/3 das funções, deixando só as interações de cliente no código, já que conseguindo sanar este bug, os demais serão idênticos.
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

//Declaração das Estruturas a serem utilizadas
typedef struct enderecoStruct
{
char rua[40];
char bairro[30];
char numero[6];
char cep[8];
char cidade[20];
char estado[20];
} Endereco;

typedef struct CadastroPessoaStruct
{
char nome[40];
char reg[12];
char rg[10];
char telefone[11];
Endereco end;
} Pessoa;

//Declaração das variáveis
int input = 0;
int qtdClientes;
int tamClientes;
Pessoa *clientes;
FILE *arquivo;
char clientes_dir[] = "Clientes.bin";

//Declaração dos Procedimentos e Funções a serem utilizados
void menuPrincipal();
void menuClientes();
void menuAlterarCliente();
void removerQuebraLinha();
Pessoa receberCliente();
Endereco receberEndereco();
void inserirCliente();
Pessoa *buscarCliente();
void alterarEndereco();
void alterarNomeCliente();
void alterarTelefoneCliente();
int removerCliente();
void listarClientes();

//Início do main
int main(int argc, char** argv)
{
    menuPrincipal();
    return 0;
}

void erro(char *nome_arquivo)
{
    printf("Nao foi possivel abrir o arquivo %s\n", nome_arquivo);
}

void sucesso()
{
    system("cls");
    printf ("Operacao realizada com sucesso!");
}

void menuPrincipal()
{
    do
    {
        printf("\n");
        printf ("\t\t\t\t  H&R GAMES!\n");
        printf ("\t\t\t===============================\n");
        printf ("\t\t\t|\t                      |\n");
        printf("\t\t\t|\t 1 - Cliente          |\n");
        printf("\t\t\t|\t 2 - Consoles         |\n");
        printf("\t\t\t|\t 3 - Jogos            |\n");
        printf("\t\t\t|\t 4 - Vendas           |\n");
        printf("\t\t\t|\t 0 - Sair             |\n");
        printf ("\t\t\t|\t                      |\n");
        printf ("\t\t\t===============================\n");
        printf ("\n\n");
        printf("\t\t\tPor favor, selecione uma opcao: ");
        fflush(stdin);
        scanf("%d", &input);
        system("cls");
        switch(input)
        {
        case 1:
            menuClientes();
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 0:
            exit(EXIT_SUCCESS);
        default:
            printf ("\n\t\t\tOpcao invalida!\n\n");
            fflush(stdin);
        }
    }
    while(input != 0);
    system("cls");
}

void menuClientes()
{
    do
    {
        printf("\n");
        printf ("\t\t\t\t  H&R GAMES!\n");
        printf ("\t\t\t===============================\n");
        printf ("\t\t\t|\t                      |\n");
        printf("\t\t\t|    1 - Listar Clientes      |\n");
        printf("\t\t\t|    2 - Buscar Cliente       |\n");
        printf("\t\t\t|    3 - Cadastrar Cliente    |\n");
        printf("\t\t\t|    4 - Alterar Cadastro     |\n");
        printf("\t\t\t|    5 - Excluir Cadastro     |\n");
        printf("\t\t\t|    6 - Menu Principal       |\n");
        printf("\t\t\t|    0 - Sair                 |\n");
        printf ("\t\t\t|                             |\n");
        printf ("\t\t\t===============================\n");
        printf ("\n\n");
        printf("\t\t\tPor favor, selecione uma opcao: ");
        fflush(stdin);
        scanf("%d", &input);
        system("cls");
        switch(input)
        {
        case 1:
            listarClientes();
            break;
        case 2:
            buscarCliente();
            break;
        case 3:
            fflush(stdin);
            inserirCliente(receberCliente());
            break;
        case 4:
            menuAlterarCliente();
            break;
        case 5:
            removerCliente(qtdClientes, clientes_dir);
            break;
        case 6:
            menuPrincipal();
            break;
        case 0:
            exit(EXIT_SUCCESS);
        default:
            printf ("\n\t\t\tOpcao invalida!\n\n");
            fflush(stdin);
        }
    }
    while(input != 0);
    system("cls");
}

void menuAlterarCliente()
{
    do
    {
        printf("\n");
        printf ("\t\t\t\t  H&R GAMES!\n");
        printf ("\t\t\t===============================\n");
        printf ("\t\t\t|\t                      |\n");
        printf("\t\t\t|    1 - Alterar Nome         |\n");
        printf("\t\t\t|    2 - Alterar Telefone     |\n");
        printf("\t\t\t|    3 - Alterar Endereco     |\n");
        printf("\t\t\t|    4 - Menu Principal       |\n");
        printf("\t\t\t|    0 - Sair                 |\n");
        printf ("\t\t\t|                             |\n");
        printf ("\t\t\t===============================\n");
        printf ("\n\n");
        printf("\t\t\tPor favor, selecione uma opcao: ");
        fflush(stdin);
        scanf("%d", &input);
        system("cls");
        switch(input)
        {
        case 1:
            alterarNomeCliente();
            break;
        case 2:
            alterarTelefoneCliente();
            break;
        case 3:
            alterarEndereco();
            break;
        case 4:
            menuPrincipal();
            break;
        case 0:
            exit(EXIT_SUCCESS);
        default:
            printf ("\n\t\t\tOpcao invalida!\n\n");
            fflush(stdin);
        }
    }
    while(input != 0);
    system("cls");
}

void removerQuebraLinha(char *string)
{
    if(string != NULL && strlen(string) > 0)
    {
        short tamanho = strlen(string);
        if(string[tamanho-1] == '\n')
        {
            string[tamanho-1] = '\0';
        }
    }
}

void receberString(char *string_destino, int quantidade_caracteres)
{
    fgets(string_destino, quantidade_caracteres, stdin);
    removerQuebraLinha(string_destino);
}

Pessoa receberCliente()
{
    Pessoa p;

    printf("Nome: ");
    receberString(p.nome, 39);
    printf ("CPF: ");
    receberString(p.reg, 13);
    fflush(stdin);
    printf ("RG: ");
    receberString(p.rg, 11);
    fflush(stdin);
    printf ("Telefone: ");
    receberString(p.telefone, 12);
    fflush(stdin);

    p.end = receberEndereco();

    sucesso();
    return p;
}

Endereco receberEndereco()
{
    Endereco e;

    printf ("Rua: ");
    receberString(e.rua, 39);
    fflush(stdin);
    printf ("Numero: ");
    receberString(e.numero, 5);
    fflush(stdin);
    printf ("CEP: ");
    receberString(e.cep, 9);
    fflush(stdin);
    printf ("Bairro: ");
    receberString(e.bairro, 29);
    printf ("Cidade: ");
    receberString(e.cidade, 20);
    printf ("Estado: ");
    receberString(e.estado, 20);

    sucesso();
    return e;
}

void inserirCliente(Pessoa p)
{
    if(qtdClientes == tamClientes)
    {
        tamClientes *= 1.5;
        clientes = realloc(clientes, tamClientes*sizeof(Pessoa));
    }
    clientes[qtdClientes] = p;
    qtdClientes++;
}

void listarClientes()
{
    int c;
    printf("\nListando %d clientes cadastrados\n", qtdClientes);
    for(c=0; c < qtdClientes; c++)
    {
        printf("-----------------------------------\n");
        printf("(%d)\n", c+1);
        printf("Nome  = %s\n", clientes[c].nome);
        printf("CPF = %s\n", clientes[c].reg);
        printf("RG = %s\n", clientes[c].rg);
        printf("Telefone = %s\n", clientes[c].telefone);
        printf("Endereco = %s", clientes[c].end.rua);
        printf(" - %s,", clientes[c].end.numero);
        printf(" %s,", clientes[c].end.bairro);
        printf(" %s", clientes[c].end.cidade);
        printf(" - %s.", clientes[c].end.estado);
    }
}

void alterarNomeCliente()
{
    int i;
    char cpf[11], nome[40];
    printf ("Digite o CPF do cliente a ter o nome alterado: ");
    fgets(cpf, 11, stdin);
    printf ("Digite o novo nome: ");
    fgets(nome, 39, stdin);
    for (i=0; i < qtdClientes; i++)
    {
        if(strcmp(clientes[i].reg, cpf) == 0)
        {
            strcpy(clientes[i].nome, nome);
            break;
        }
    }
}

void alterarTelefoneCliente()
{
    int i;
    char cpf[11], fone[11];
    printf ("Digite o CPF do cliente a ter o telefone alterado: ");
    fgets(cpf, 11, stdin);
    printf ("Digite o novo numero: ");
    fgets(fone, 11, stdin);
    for (i=0; i < qtdClientes; i++)
    {
        if(strcmp(clientes[i].reg, cpf) == 0)
        {
            strcpy(clientes[i].telefone, fone);
            break;
        }
    }
}

void alterarEndereco()
{
    int i;
    char cpf[12];
    printf ("Digite o CPF do cliente a ter o endereco alterado: ");
    fgets(cpf, 11, stdin);
    Endereco e;
    e = receberEndereco();
    for (i=0; i < qtdClientes; i++)
    {
        if(strcmp(clientes[i].reg, cpf) == 0)
        {
            clientes[i].end = e;
            break;
        }
    }
}

Pessoa *buscarCliente()
{
    Pessoa *p = NULL;
    int i;
    char cpf[12];
    printf ("Digite o CPF do cliente a ser buscado: ");
    fgets(cpf, 11, stdin);
    for (i=0; i < qtdClientes; i++)
    {
        if(strcmp(clientes[i].reg, cpf) == 0)
        {
            p = &clientes[i];
            break;
        }
    }
    return p;
}

int removerCliente(int qtd, char *dir)
{
int i, CPF, sucess = 0;
printf("Digite o CPF do cliente que deseja remover: ");
scanf ("%d", &CPF);
for (i=0; i < qtd; i++)
{
    if((clientes[i].reg - CPF) == 0)
    {
        while(i < qtd-1)
        {
            clientes[i] = clientes[i+1];
            i++;
        }
        qtd--;
        sucess = 1;
        break;
    }
    else
    {
        erro(dir);
    }
}
return sucess;
}

Comment: xiiiiiiiii ... tenta reduzir o código para um pedaço pequeno que tenha o erro. Se eu tivesse que adivinhar diria que o erro vem da mistura de `scanf()` e `fgets()`.
Comment: Deixei o código em função apenas do ponteiro de Clientes. Já que para as outras estruturas o conceito será o mesmo, resolvendo o bug em um conjunto de funções, basta fazer o mesmo nos demais...
Answer: Que tal usar o utilitário gdb para debugar seu programa em tempo de execução ?
Trata-se de uma ferramenta largamente utilizada para se depurar leaks de memória em programas grandes e complexos.
Assumindo que você está tentando encontrar o leak de memória no código abaixo, segue um exemplo passo-a-passo de como usar o utilitário.
1) Listagem do código a ser depurado:
/* programa.c */

#include <stdio.h>

int main( void )
{
    printf("Antes do leak!\n");

    printf("leak: %s\n", (char*) 1234 );

    printf("Apos o leak!\n");

    return 0;
}

/* fim-de-arquivo */

2) Compilando seu programa com o gcc em modo debug (opção -g):
$ gcc -g programa.c -o programa

3) Debugando com o gdb:
$ gdb ./programa
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-80.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /tmp/programa...done.

4) No console do gdb, utilize o comando run para executar o programa e simular a falha:
(gdb) run
Starting program: /tmp/./programa
Antes do leak!

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a61ab4 in vfprintf () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.17-106.el7_2.6.x86_64

5) Agora, é só usar o comando backtrace para se identificar onde exatamente aconteceu o leak:
(gdb) backtrace
#0  0x00007ffff7a61ab4 in vfprintf () from /lib64/libc.so.6
#1  0x00007ffff7a6ac99 in printf () from /lib64/libc.so.6
#2  0x00000000004005a2 in main () at programa.c:7

Pronto! Segundo o gdb, o leak de memória ocorreu na linha 7 do arquivo programa.c dentro da função main().
Referências:
Wikipedia: https://pt.wikipedia.org/wiki/GNU_Debugger
Página do Projeto: https://www.gnu.org/software/gdb/
Espero ter ajudado!


C# WPF Websocket - Cannot established handshake to NGINX

Question: Been using this client library: [Websocket Sharp][1] to implement my websocket client for WPF application.
The problem is, if I used the SetProxy method of this library to connect with NGINX with no basic auth, I cannot establish a connection even if the NGINX server doesn't have a basic auth configured.
But all is working fine when I connect the client directly to backend server witout passing NGINX.
Here is my client code:
 using (var ws = new WebSocket("ws://localhost:5555/testnginx"))
            {
                ws.SetProxy("http://localhost:5555", null, null);
                ws.OnMessage += (sender, e) => {
                    Debug.WriteLine("Received a message: " + e.Data);
                };

And here is my NGINX setting:
location /testnginx{
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header HOST $host;
            proxy_set_header X_Forwarded_For $remote_addr;
            proxy_pass http://socketservers;
            #proxy_pass http://localhost:7777/gs-guide-websocket;
            proxy_redirect default;
            client_max_body_size 1000m;
        }

Or is there any alternative to connect to spring websocket server that will support NGINX?
[1]: https://github.com/sta/websocket-sharp
Answer: Solved the issue.
Just need to make sure that the route in the NGINX and endpoint configured in stomp must be exactly the same.
For example
NGINX Route:
location /testnginx{
}

Spring stomp websocket endpoint:
@Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/testnginx").setAllowedOrigins("*").withSockJS();
    }


How a router decides destination of packet?

Question: I have a basic networking question. Two PCs are communicating on a WLAN. Both the PCs are behind routers or modems.
Both the PCs have the same public IP. That public IP is most of the time is either of the router or of the modem. There can be more then one PC behind those routers and modems. How are the PCs are communicating? I can understand that packets can reach those routers or modems and then the Internet, but what after that? In the packet, the destination IP is set as the public IP. When the packet returns, how does the router or modem decide where to send the packet?
Can anybody explain me this please.
Answer: Most of the mechanics behind this mystery is NAT-masquerading:

Your PC communicates with outside

for instance your PC is on a private network, with IP 10.1.2.3.
Your router remembers the PC IP/port, and that it initialized a connection to a remote IP/port (WAN). Thus, when there is a reply on the same channel, the router forwards back the packet to the PC, on its LAN side.

An external device communicates with your PC

In this case, the ext. device initializes the connection, and thus the router doesn't know to which 10.x.x.x client it is supposed to forward the packet.
This is why the router needs a specific settings that says, for instance if your PC happens to be a Web server (http port)
<code>  HTTP (WAN) ==> (LAN) 10.1.2.3:80
</code>
that says, the packets coming on the HTTP port go to the internal LAN address 10.1.2.3 (your PC) on port 80.
Comment: Thanks for your help. Can you please tell me about any rfc related to this?
Comment: Please refer to the Wikipedia link at the top of the answer. A few RFCs can be reached from the article.
Answer: In a basic setup a device always only knows the next hop. This one also just knows the next hop to the desired destination. if the destination is not local to the host/router it sends the packet to the default gateway and so on and so forth.
in more complex situations you can have static routes pointing to an explicit gateway for a certain network destination.
if it gets too complex to manage those routes manually, dynamic route distribution protocols are used like  RIP
even more elaborate protocols know more about the global distribution of ip blocks (via AS Numbers) and try to make decisions on where to route packets on cost and distance. examples are BGP and OSPF
Comment: sorry for -1, but this is not what I asked.
Comment: as far as i can see your only sentence with a questionmark is: "Then how the router or modem decides where to send the packet?" and I thin i answered that!
Comment: Read the question again.I am talking about transfer of packet from router or modem to internal lan. No concept of hops here.
Answer: I'm a bit puzzled by your "most of the time": I think it's either always or never.
If the public IP of a PC is always coinciding with its router/modem, then the local network is using NAT to give access to the PC from outside the local network. => see first answer
If the public IP of a PC doesn't coincide with its router/modem, then the local network is directly accessible from the WAN (if we ignore possible firewalls that can block the way). => see second answer
If you are just starting to understand how routing works, you should probably first concentrate on the latter scenario, and maybe also simplify it as:
<code>PC1 <-LAN1-> Router1 <-WAN_link-> Router2 <-LAN2-> PC2
</code>
(like there would not be anything else around)
Answer: Good question. There are actually a few strategies, but essentially the router keeps a table of all NAT'd connections (client port, client IP, destination IP, destination port). When a return packet comes back it matches which client to forward it to based on where the packet has come from, and from which port.
Most NAT implementations are also PAT (port address translation implementations), to prevent two clients picking the same source port.
Some routers, e.g. Cisco devices do sequence number randomization for security reasons - it can help hide how many and what types of clients are behind the NAT. It can also try to protect the clients against types attacks that target the sequence number, or guess the next sequence number for the next connection.